2015-03-31 118 views
0

我的问题是我在由html页面内的表单填充的servlet类中创建了一个ArrayList。 然后我将我的数组列表传递给打印所有对象的jsp页面。 现在我打印的每个对象都变成了调用servlet的“doGet”方法的“href”。 我需要通过单击链接所选对象的索引。如何将参数从jsp页面传递给servlet?

//这是我的servlet的方法的doGet的实现:

//我知道这是错误的,使用的getAttribute,但我不知道还有什么可以真正发挥作用。

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); 
    } 

这是我的jsp(ES3-的List.jsp)页面打印的对象:

<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> 
+0

的可能重复的形式[如何将数据从JSP转移到servlet中](http://stackoverflow.com/questions/4971877/how-to-transfer-data-from-jsp-to-servlet) – VedX 2015-03-31 12:01:06

+0

你必须通过它的价值附加在url – silentprogrammer 2015-03-31 12:04:47

回答

1

你可以追加参数,以请求的URL。我看你正在使用的doGet所以它只是你可以追加问号后的参数,如

myURL?param1=value1&param2=value2 

上述情况是定位标记的HREF。你将不得不像上面那样创建href。 但这个可以有将提交到的doGet像

<form action="myservlet"> 
<input type="text" name="param1"/> 
<input type="text" name="param2"/>  
</form> 

而且从servlet的在这两种情况下,你可以访问值

request.getParameter("param1"); 
+0

它的工作! 感谢您的支持! 这是我第一次用jsp页面和servlet进行编程,一般来说我还没有任何分布式系统的实践。 – 2015-04-02 13:13:36

+0

@ A.B.V。帮助你很高兴..如果解决方案为你工作,你能接受答案吗? – 2015-04-03 09:29:34