一个示例程序,用于从JSP表单中获取名称字段数组,并在servlet页面上获取它。我无法弄清楚如何从表单检索数组到servlet。请帮助我完成这一尝试。
<body>
<%!
String name[]= new String[2];/*declares a string for storing two name fields*/
%>
<form action="page2">
<!--I need to send these name fields to the MyServlet page.so i used a String array to store both the values?-->
<%
for(int i=1;i<=2;i++) {%>
Username<%= i %> : <input type="text" name="<%=name%>">
<% } %>
<input type="submit" value="ClickHere">
</form><!-- Upon submit goes to MyServlet page-->
</body>
out.println("<body>");
String name[] = new String[2];
for(int i=1;i<2;i++)
{
/*what should i enclose in getParameter(..?.) to obtain values from jsp*/
String name[i]=request.getParameter();//get the values from the form
}
out.println("</body>");
请建议在代码中进行改进
答案 0 :(得分:0)
您可以迭代所有请求参数,如下所示:
Enumeration<String> en = request.getParameterNames();
while (en.hasMoreElements()) {
String paramName = en.nextElement();
String paramValue = request.getParameter(paramName);
}