0

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> 
Jagger
  • 10,350
  • 9
  • 51
  • 93
A.B.V.
  • 3
  • 1
  • 2

1 Answers1

2

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&param2=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");
Viraj Nalawade
  • 3,137
  • 3
  • 28
  • 44
  • It worked! Thank you for your support! It was my first time in programming with jsp pages and servlets and in general I have not yet any practice in distributed systems. – A.B.V. Apr 02 '15 at 13:13
  • @A.B.V. helping you was pleasure.. Can you accept the answer if the solution worked for you?? – Viraj Nalawade Apr 03 '15 at 09:29