<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>request内置对象</h1>
<%
request.setCharacterEncoding("utf-8");
%>
用户名:<%=request.getParameter("usename") %><br>
爱好:<%
String[] favorites=request.getParameterValues("favorite");
for(int i=0;i<favorites.length;i++)
{
out.println(favorites[i]+" ");
}
%>
</body>
</html>
Type Exception Report
Message An exception occurred processing JSP page [/request.jsp] at line [14]
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 [/request.jsp] at line [14] 11: 用户名:<%=request.getParameter("usename") %><br> 12: 爱好:<% 13: String[] favorites=request.getParameterValues("favorite"); 14: for(int i=0;i<favorites.length;i++) 15: { 16: out.println(favorites[i]+" "); 17: } Stacktrace: org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:584) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:481) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330) javax.servlet.http.HttpServlet.service(HttpServlet.java:742) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Root Cause
java.lang.NullPointerException org.apache.jsp.request_jsp._jspService(request_jsp.java:126) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:742) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:443) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330) javax.servlet.http.HttpServlet.service(HttpServlet.java:742) 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.
你的上一个跳转页面有信息没有填,造成空指针异常,
空指针异常不能捕捉,解决方法就是将容易为空的字段加上非空判断,阻止此字段为空时运行代码造成报错。
加一句判断非空 if(request.getparameterValues(favorite)!=null) { String[] favorites=request.getParameterValues("favorite"); for(int i=0;i<favorites.length;i++) { out.println(favorites[i]+" "); } }
favorite和表单里用的name名称一样吗
favorites可能为空,先用if判断一下是否为空