Martina
Martina

Reputation: 1918

put a parameter in the servlet url

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'>&nbsp"+
                                "<a href='' id='"+lista.get(i).getIdUtente()+"' class='view'>Vedi</a>&nbsp &nbsp" 
                                +"<img src='imm/mod.png'>&nbsp"+
                                "<a href='InfoUtente&id="+lista.get(i).getIdUtente()+"' id='"+lista.get(i).getIdUtente()+"' class='mod'>Modifica   </a>&nbsp &nbsp"+
                                 "<img src='imm/del.png'>&nbsp"+
                                "<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?

Upvotes: 1

Views: 1775

Answers (3)

JB Nizet
JB Nizet

Reputation: 691635

Just as you did, except the query string must start with a ? and not with a &:

<a href='InfoUtente?id="+lista.get(i).getIdUtente()
                   ^-- here

& 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:

<a href='InfoUtente?id=" + theId + "&amp;foo=bar"

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:

<c:forEach var="utente" items="${lista}">
    <tr>
        <td><c:out value="${utente.nome}"/></td>
        <td><c:out value="${utente.cognome}"/></td>
        <td><c:out value="${utente.posizione}"/></td>
        <td><c:out value="${utente.telefono}"/></td>
        <td>
           <img src="imm/view.png"/>&nbsp;
           <a href="" id="${utente.idUtente}" class="view">Vedi</a>
           &nbsp;&nbsp;
           <img src="imm/mod.png"/>&nbsp;
           <a href="<c:url value="InfoUtente">
                        <c:param name="id" value="${utente.idUtente}"/>
                    </c:url>" class="mod">Modifica</a>
           &nbsp;&nbsp;
           <img src="imm/del.png"/>&nbsp;
           <a href="" class="del" name="<c:out value="${utente.nome} ${utente.cognome}"/>">Elimina</a>
        </td>
    </tr>
</c:forEach>

Note how the structure is much more readable. Also note that

  • it's &nbsp; and not &nbsp
  • you can't have several elements with the same ID. This is invalid.
  • <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)
  • using <c:url> allows using absolute paths instead of relative paths without hard-coding the context path, and it URL-encodes the parameters.

Upvotes: 1

zdesam
zdesam

Reputation: 2976

I would recommend you to do this with jsp as below and about the problem you mentioned you have to put ? to start query string and not &

Check the code below

<%
ArrayList<Utente> lista=null;
try{
lista= (ArrayList<Utente>)request.getAttribute("lista");
}catch(Exception e){
e.printStackTrace();
}
if(lista!=null){                    
for(Utente utente : lista){%>                                                                                               
<tr>
    <td><%=utente.getName()%></td>
    <td><%=utente.getCognome()%></td>
    <td><%=utente.getPosizione()%></td>
    <td><%=utente.getTelefono()%></td>
    <td>
          <img src='imm/view.png'>&nbsp
          <a href='' id='<%=utente.getIdUtente()%>' class='view'>Vedi</a>&nbsp &nbsp
           <img src='imm/mod.png'>&nbsp</a>
           <a href='InfoUtente?id=<%=utente.getIdUtente()%>' id='<%=utente.getIdUtente()%>' class='mod'>Modifica </a>&nbsp &nbsp
        <img src='imm/del.png'>&nbsp</a>  <!-- Here you have to put ? instead of & -->
         <a href='' id='<%=utente.getIdUtente()%>' class='del' name='<%=utente.getName()%>'>Elimina</a></td>
</tr>
<%}
}%>

Upvotes: 0

Debobroto Das
Debobroto Das

Reputation: 862

write an java script function and call that function onClick (or onSubmit etc.) of the specific field change. Inside that function build a string like "page.html?param=value". and redirect the page to there.

Upvotes: 0

Related Questions