I have this peace of code:
<%
ArrayList<Utente> lista=null;
try{
lista= (ArrayList<Utente>)request.getAttribute("lista");
}catch(Exception e){
e.printStackTrace();
}
if(lista!=null){
for(int i=0;i<lista.size();i++){ out.print("<tr>");
out.print("<td>"+lista.get(i).getNome()+"</td>");
out.print("<td>"+lista.get(i).getCognome()+"</td>");
out.print("<td>"+lista.get(i).getPosizione()+"</td>");
out.print("<td>"+lista.get(i).getTelefono()+"</td>");
out.print("<td><img src='imm/view.png'> "+
"<a href='' id='"+lista.get(i).getIdUtente()+"' class='view'>Vedi</a>   "
+"<img src='imm/mod.png'> "+
"<a href='InfoUtente&id="+lista.get(i).getIdUtente()+"' id='"+lista.get(i).getIdUtente()+"' class='mod'>Modifica </a>   "+
"<img src='imm/del.png'> "+
"<a href='' id='"+lista.get(i).getIdUtente()+"' class='del' name='"+lista.get(i).getNome()+" "+lista.get(i).getCognome()+"'>Elimina </a></td>");
}
}
%>
What I want to do now is to call the servlet InfoUtente when I click on the link Modifica on a certain index. And so I want to pass the index parameter to the servlet.
How can I do?
Just as you did, except the query string must start with a
?and not with a&:&is used to separate the parameters inside the query string. And it must be HTML-escaped. So if you had a second parameter to pass, you would need to generate the following URL:Note that generating HTML from Java code is not a good practice. It leads to hard-to read, unmaintainable code. Scriptlets should be avoided. You should do that using JSTL tags and the JSP EL:
Note how the structure is much more readable. Also note that
and not <c:out>allows escaping special characters. So if any of the attributes contains a<or a>or a&for example, it won’t lead to invalid HTML (or worse: to an XSS attack)<c:url>allows using absolute paths instead of relative paths without hard-coding the context path, and it URL-encodes the parameters.