Hossam Hassan Hossam Hassan - 5 months ago 13
Javascript Question

how to send data with a link tag from a servlet

In my servlet I do this :

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
String PatientName = request.getParameter("PatientName");

PatientDB pdb = new PatientDB();
ArrayList < Patient > p = new ArrayList < Patient > ();
p = pdb.searchForPatient(PatientName);
System.out.println("1st name: " + p.get(0).getName());
String s = "";
for (int i = 0; i < p.size(); i++) {
String Name = p.get(i).getName();
int ID = p.get(i).getID();
s += "<a href='#' onClick=hideDiv('<%=" + Name + " >','<%=" + ID + " >') >" + p.get(i).getName() + "</a><br>";
// (p.get(i).getID() + " , " + p.get(i).getName() + "<br>");
}
out.println(s);
} finally {
out.close();
}
}


but i get corrupted output like that
output

Answer

If you take out the string concatination and look at the tag that you are building

s += "<a href='#' onclick=hideDiv('')></a><br>";   

You can see that you are not enclosing the value of the onclick attribute properly in qoutes.

"<a href='#' onclick=\"hideDiv('')\"></a><br>">"

So with your string concatenation it would look like this:

s += "<a href='#' onclick=\"hideDiv('<%=" + Name + " >','<%=" + ID + " >')\"  >" + p.get(i).getName() + "</a><br>";

also not that the attribute name is onclick, not onClick

Comments