老师,我的session第一次传递正确,第二次返回时输出为null?怎么解决?代码如下
还是这个错误,但是我的思路时用session存储值,然后返回上一个页面:
org.apache.jasper.JasperException: An exception occurred processing [show_main.jsp] at line [22]
19: String pwd = (String)session.getAttribute("password");
20: System.out.println("pwd "+pwd);
21: Emp emp = new Emp(name,pwd);
22: boolean flag = DBUtil.selectEmpByAccountAndPassword(emp);
23: Map<String,ShowMessage> map1 = DBUtil.map1;
24:
25: if (flag == true) {
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:593)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:482)
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:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Root Cause
java.lang.NullPointerException
com.imooc.db.DBUtil.selectEmpByAccountAndPassword(DBUtil.java:38)
org.apache.jsp.show_005fmain_jsp._jspService(show_005fmain_jsp.java:136)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:444)
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:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Note The full stack trace of the root cause is available in the server logs.
(1)这是公告信息主页面的代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="com.imooc.db.*, com.imooc.bean.*, java.util.*"%> <title>公告列表</title> </head> <body> <% String nickname = request.getParameter("nickname"); System.out.println("nickname "+nickname); String password = request.getParameter("password"); System.out.println("password "+password); session.setAttribute("nickname", nickname); session.setAttribute("password", password); String name = (String)session.getAttribute("nickname"); System.out.println("name "+name); String pwd = (String)session.getAttribute("password"); System.out.println("pwd "+pwd); Emp emp = new Emp(name,pwd); boolean flag = DBUtil.selectEmpByAccountAndPassword(emp); Map<String,ShowMessage> map1 = DBUtil.map1; if (flag == true) { %> <h3>公告列表为: </h3> <hr> <form action="select.jsp"> <table align="center" border="1" width="500px"> <tr> <td> 公告编号: </td> <td> <input type="text" name="num" value="" placeholder="请输入编号"/> </td> <td> <input type="submit" value="select"/> </td> </tr> </table> </form> <div height="50px"></div> <form action=""> <table align="center" border="1" width="500px"> <tr> <td> 编号 </td> <td> 名称 </td> <td> 内容 </td> <td> 删除 </td> <td> 修改 </td> </tr> <% for (String key : map1.keySet()) { ShowMessage e = map1.get(key); %> <tr> <td> <%= e.getNum() %> </td> <td> <%= e.getName() %> </td> <td> <%= e.getContent() %> </td> <td> <a href="delete.jsp?num=<%= e.getNum() %> &name=<%= e.getName() %>&content=<%= e.getContent() %>">删除</a> </td> <td> <a href="update.jsp?num=<%= e.getNum() %> &name=<%= e.getName() %>&content=<%= e.getContent() %>">修改</a> </td> </tr> <% } %> </table> </form> <% } else { } %> </body> </html>
(2)这是点击select后的界面代码(有back按钮的界面)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="com.imooc.db.*, com.imooc.bean.*, java.util.*"%> <title>公告列表</title> </head> <body> <h3>公告列表为: </h3> <p>用户名:<%=request.getSession().getAttribute("nickname") %>, 密码:<%=request.getSession().getAttribute("password") %></p> <% request.setAttribute("nickname", session.getAttribute("nickname")); request.setAttribute("password", session.getAttribute("password")); out.println(session.getId()); %> <hr> <form action="show_main.jsp"> <table align="center" border="1" width="500px"> <tr> <td> 公告编号: </td> <td> <input type="text" name="num" value="<%= request.getParameter("num") %>"/> </td> <td> <a href="show_main.jsp">back</a> ">Back</a> </td> </tr> </table> </form> <form action=""> <table align="center" border="1" width="500px"> <tr> <td> 编号 </td> <td> 名称 </td> <td> 内容 </td> <td> 删除 </td> <td> 修改 </td> </tr> <% Map<String,ShowMessage> map1 = DBUtil.map1; ShowMessage e1 = map1.get(request.getParameter("num")); for (String key : map1.keySet()) { ShowMessage e2 = map1.get(key); if (e1.getNum().equals(e2.getNum())) { %> <tr> <td> <%= e1.getNum() %> </td> <td> <%= e1.getName() %> </td> <td> <%= e1.getContent() %> </td> <td> <a href="delete.jsp?num=<%= e1.getNum() %> &name=<%= e1.getName() %>&content=<%= e1.getContent() %>">Delete</a> </td> <td> <a href="">Update</a> </td> </tr> <% } } %> </table> </form> </body> </html>
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星