5

我在 jsp 页面的隐藏字段中有一些数据

<input type=hidden id="thisField" name="inputName">

如何在提交时访问或传递此字段到另一个页面?

4

2 回答 2

13

要传递值,您必须value="hiddenValue"<input>语句中包含隐藏值,如下所示:

<input type="hidden" id="thisField" name="inputName" value="hiddenValue">

然后通过访问请求对象的参数,以与恢复可见输入字段的值相同的方式恢复隐藏的表单值。这是一个例子:

此代码位于您要隐藏值的页面上。

<form action="anotherPage.jsp" method="GET">
    <input type="hidden" id="thisField" name="inputName" value="hiddenValue">
<input type="submit">   
</form>

然后在“anotherPage.jsp”页面上,通过调用getParameter(String name)隐式request对象的方法来恢复值,如下所示:

<% String hidden = request.getParameter("inputName"); %>
The Hidden Value is <%=hidden %>

上述脚本的输出将是:

The Hidden Value is hiddenValue 
于 2013-06-17T19:18:44.627 回答
0

Alex 的代码效果很好。请注意,当您使用 request.getParameter 时,您必须使用请求调度程序

//Pass results back to the client
RequestDispatcher dispatcher =   getServletContext().getRequestDispatcher("TestPages/ServiceServlet.jsp");
dispatcher.forward(request, response);
于 2014-10-03T18:34:53.520 回答