如何将参数从 jsp 页面传递给 servlet?
How to pass parameter from a jsp page to a servlet?
我的问题是我在 servlet class 中创建了一个 ArrayList,由 html 页面中的表单填充。
然后我将我的数组列表传递给打印所有对象的 jsp 页面。
现在我打印的每个对象都变成了一个 "href" 调用 servlet 的 "doGet" 方法。
我需要传递通过单击 link 选择的对象的索引。
//这是我的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>
您可以将参数附加到请求中 url。我看到您正在使用 doGet,所以您可以在问号后附加参数,例如
myURL?param1=value1¶m2=value2
上面的例子是锚标签的href。您只需要如上所述创建 href。
但这你可以有表格将提交给 doGEt 就像
<form action="myservlet">
<input type="text" name="param1"/>
<input type="text" name="param2"/>
</form>
在这两种情况下,您都可以从 servlet 访问值
request.getParameter("param1");
我的问题是我在 servlet class 中创建了一个 ArrayList,由 html 页面中的表单填充。 然后我将我的数组列表传递给打印所有对象的 jsp 页面。 现在我打印的每个对象都变成了一个 "href" 调用 servlet 的 "doGet" 方法。 我需要传递通过单击 link 选择的对象的索引。
//这是我的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>
您可以将参数附加到请求中 url。我看到您正在使用 doGet,所以您可以在问号后附加参数,例如
myURL?param1=value1¶m2=value2
上面的例子是锚标签的href。您只需要如上所述创建 href。 但这你可以有表格将提交给 doGEt 就像
<form action="myservlet">
<input type="text" name="param1"/>
<input type="text" name="param2"/>
</form>
在这两种情况下,您都可以从 servlet 访问值
request.getParameter("param1");