request.getParameterValues 出现 [Ljava.lang.String;@ 错误

时间:2023-12-19 22:45:32

在实现简单的本地登录系统时,需要把page1.jsp的表单显示在page2.jsp中。

其中获取page1.jsp表单的办法就是在页面1的<form>中加入action="page2.jsp"

like this:

<form name="users_name" action="page2.jsp">
<p>
<label>Name</label>
<input type="text" name="name">
</p>
<p>
<label>Gender</label>
<input type="radio" name="gender" value="Male">Male
<input type="radio" name="gender" value="Female">Female
</p> <p>
<label>Interest</label>
<input type="checkbox" name="interest" value="Sing">Sing
<input type="checkbox" name="interest" value="Dance">Dance
<input type="checkbox" name="interest" value="Play balls">Play balls
<input type="checkbox" name="interest" value="Play games">Play games
</p>
<p id="buttons">
<input type="submit" value="Submit">
</p>

前两种轻松用request.getParameter()解决

    Name:<%=request.getParameter("name")%><br>
Sex:<%=request.getParameter("gender")%><br>

而在获取checkbox 时用request.getParameterValues()却出现了问题

Habits:<%= request.getParameterValues("interest")%><br>

输出时输出了:Habits:[Ljava.lang.String;@5f0df6e9

在网上搜索后发现:getParamerterValues()为获取数组的方法

getParamerterValues与request.getParameter的区别:    https://blog.csdn.net/moreorless/article/details/4199677

然后改成for循环输出:

Habits:<%
String[] habits = request.getParameterValues("interest");
for( int i = 0; i<habits.length; i++)
System.out.println(habits[i]);
%><br>

结果被抛出异常:


Type Exception Report

Message An exception occurred processing JSP page /printinfo.jsp at line 20

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

org.apache.jasper.JasperException: An exception occurred processing JSP page /printinfo.jsp at line 20

17:     Sex:<%=request.getParameter("gender")%><br>
18: Habits:<%
19: String[] habits = request.getParameterValues("interest");
20: for( int i = 0; i<habits.length; i++)
21: System.out.println(habits[i]);
22: %><br>
23: </body> Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:613)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:510)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

Root Cause

java.lang.NullPointerException
org.apache.jsp.printinfo_jsp._jspService(printinfo_jsp.java:101)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:472)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

Note The full stack trace of the root cause is available in the server logs.


脑瓜疼。。

说明

habits根本就没收到来自request.getParameterValues("interest")传值
经过多番查找,找到了问题所在
Habits:<%
String[] habits = request.getParameterValues("interest");
if(habits!=null)
{
for(int i=0;i<habits.length;i++)
{
out.print(habits[i]);
}
}
%><br>

原来犯了个非常低级的错误,在获取值的时候没有判断来的值是否为空。。。。

以后千万长记性嗷!!