Reputation: 3
My problem is that I create an ArrayList in the servlet class filled by a form inside an html page. Then I pass my array-list to a jsp page that prints all the objects. Now every object i printed becomes a "href" that calls the "doGet" method of the servlet. I need to pass the index of the object selected by clicking on the link.
//This is the implementation of the method doGet of my servlet:
//I know that is wrong to use getAttribute but I don't know what else could really work.
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
**String ciao = request.getAttribute("id");** //this is the error
int k = Integer.parseInt(ciao); // this is because i pass a String object and i'm already sure that it will be a number.
String invio = lista.get(k);
request.setAttribute("key", invio);
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher("/views/es3-item.jsp");
dispatcher.forward(request, response);
}
This is my jsp (es3-list.jsp) page that prints the objects:
<c:forEach var="valore" items="${requestScope.message}" varStatus="theCount">//message is the key to pass the ArrayList "lista" to this jsp page.
<a href ="./es3"> <c:out value="${valore}"/> </a>
<a id="${theCount.index}"></a>
</c:forEach>
Upvotes: 0
Views: 3085
Reputation: 3227
you can Just append parameters to the request url . I see you are using doGet so its just that you can append parameters after question mark like
myURL?param1=value1¶m2=value2
The above case is with href of anchor tag. You will have to create href as above only. But this you can have form which will be submitted to doGEt like
<form action="myservlet">
<input type="text" name="param1"/>
<input type="text" name="param2"/>
</form>
And from servlet in both cases you can access values as
request.getParameter("param1");
Upvotes: 2