HTTPStatus500不知道哪边有问题。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <%@ page import = "java.util.Map" %> <%@ page import = "com.imooc.bean.Emp" %> <%@ page import = "com.imooc.db.DBUtil" %> <%@ page contentType= "text/html;charset=UTF-8" language= "java" %> <html> <head> <title>Title</title> </head> <body> <%=request.getParameter( "account" )%> <%=request.getParameter( "name" )%> <%=request.getParameter( "email" )%> <% Map<String,Emp> map = DBUtil.map; Emp emp = map.get(request.getParameter( "account" )); emp.setName(request.getParameter( "name" )); emp.setEmail(request.getParameter( "email" )); %> </body> </html> |
其中,这三行单独运行,是正常可以显示数据的。
<%=request.getParameter("account")%> <%=request.getParameter("name")%> <%=request.getParameter("email")%>
然后,如果把后面修改也带上,就报错了。不知道哪边错了。
看了tomcat日志,是如下信息:
Jan 22, 2018 8:19:56 PM org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet [jsp] in context with path [/imooc] threw exception [An exception occurred processing JSP page /update_control.jsp at line 22
19: <%
20: Map<String,Emp> map = DBUtil.map;
21: Emp emp = map.get(request.getParameter("account"));
22: emp.setName(request.getParameter("name"));
23: emp.setEmail(request.getParameter("email"));
24: %>
25: </body>
Stacktrace:] with root cause
java.lang.NullPointerException
at org.apache.jsp.update_005fcontrol_jsp._jspService(update_005fcontrol_jsp.java:104)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:442)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1083)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:640)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
正在回答
从错误提示上看,代码的第22行出现了空指针异常,然后排查第22行是 emp.setName(request.getParameter("name"));空指针异常是因为一个为null的对象去调用方法导致的,所以很有可能emp是为null的。
那么继续排查emp是怎么获取到的,Emp emp = map.get(request.getParameter("account"));这是获得emp对象的语句,确认一下在将数据添加到map时,是否accout的值作为key值的,还有前一个页面中accout这个文本框中输入的内容是否是map的key值。这两个愿意都可能造成查找不到数据emp为null。
如果排查不出来,可以把向map添加数据的代码,以及accout文本框所在的jsp页面的代码贴出来。
祝学习愉快!
package com.imooc.db; import com.imooc.bean.Emp; import java.util.HashMap; import java.util.Map; public class DBUtil { public static Map<String, Emp> map = new HashMap<String, Emp>(); static { map.put("101", new Emp("101", "AA", "1234", "aa@aa.com")); map.put("102", new Emp("1002", "BB", "1234", "bb@bb.com")); map.put("103", new Emp("1003", "CC", "1234", "cc@cc.com")); map.put("104", new Emp("1004", "DD", "1234", "dd@dd.com")); } public static boolean selectEmpByAccountAndPassword(Emp emp) { boolean flag = false; for (String key : map.keySet()) { Emp e = map.get(key); if (emp.getAccount().equals(e.getAccount()) && emp.getPassword().equals(e.getPassword())) { flag = true; break; } } return flag; } }
可能是Map中101和1001错误。
我修改1001至101就正确了。但我不知道原因。
- 参与学习 716 人
- 提交作业 185 份
- 解答问题 1363 个
会Java?懂前端基础?想学后台开发?那么,赶快来学习《Java Web入门》路径吧。本路径主要介绍Java Web的基础知识,并配有大量案例,定会让你收获多多!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星