登录时报错
servlet层 protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); System.out.println(username+password); User user = userService.login(username, password); System.out.println("user:"+user); if (null != user){ request.getSession().setAttribute("user", user); request.getRequestDispatcher("/JDBCMessageBoard/message/list.do").forward(request, response); } else { request.getRequestDispatcher("/login.do").forward(request, response); } }
service层 public User login(String username,String password){ return userDao.login(username, password); }
dao层 public User login(String username,String password){ Connection conn = ConnectionUtil.getConnection(); String sql = "select * from user where username = ? and password = ?"; PreparedStatement stmt = null; ResultSet rs = null; User user = null; try { stmt = conn.prepareStatement(sql); stmt.setString(1, username); stmt.setString(2, password); rs = stmt.executeQuery(); while(rs.next()){ user = new User(); user.setId(rs.getLong("id")); user.setName(rs.getString("username")); user.setPassword(rs.getString("password")); user.setRealName(rs.getString("real_name")); user.setBirthday(rs.getDate("birthday")); user.setPhone(rs.getString("phone")); user.setAddress(rs.getString("address")); } } catch (SQLException e) { System.out.println("登录失败!"); e.printStackTrace(); } finally{ ConnectionUtil.release(rs, stmt, conn); } return user; }
运行登录页面报错
严重: Servlet.service() for servlet [LoginServlet] in context with path [/JDBCMessageBoard] threw exception
java.lang.NullPointerException
at com.imooc.jdbc.servlet.LoginServlet.service(LoginServlet.java:34)
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:220)
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:170)
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:423)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
正在回答
如下图所示,测试了你的代码,是不会出现空指针,建议你清空一下tomcat
因为你的代码逻辑是没有问题的,我这头代入老师的源码中测试也是可以打印出user的,建议你重新Rebuild Project试试,祝学习愉快~
package com.imooc.jdbc.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.imooc.jdbc.bean.User; import com.imooc.jdbc.service.UserService; /** * 用户登录的servlet * @author Administrator * */ public class LoginServlet extends HttpServlet { private UserService userService; public void init() throws ServletException { // Put your code here userService = new UserService(); } @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); System.out.println(username+password); User user = userService.login(username, password); System.out.println(user); if (null != user){ request.getSession().setAttribute("user", user); request.getRequestDispatcher("/JDBCMessageBoard/message/list.do").forward(request, response); } else { request.getRequestDispatcher("/login.do").forward(request, response); } } public void destroy() { super.destroy(); // Just puts "destroy" string in log } }
你好同学,应该在Servlet中的init初始化方法中创建userService,而不是在destroy()中创建哦,根据报错提示的描述,就是你的userService为null,因为你只声明了userService,并没有创建实例,所以会出现空指针异常,祝学习愉快~
servlet层 package com.imooc.jdbc.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.imooc.jdbc.bean.User; import com.imooc.jdbc.service.UserService; /** * 用户登录的servlet * @author Administrator * */ public class LoginServlet extends HttpServlet { private UserService userService; public void destroy() { super.destroy(); // Just puts "destroy" string in log userService = new UserService(); } @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); System.out.println(username+password); User user = userService.login(username, password);//报错的地方!!!!! System.out.println("user:"+user); if (null != user){ request.getSession().setAttribute("user", user); request.getRequestDispatcher("/JDBCMessageBoard/message/list.do").forward(request, response); } else { request.getRequestDispatcher("/login.do").forward(request, response); } } public void init() throws ServletException { // Put your code here } }
你好同学,将你的代码粘贴到老师的源码中,并没有出现空指针异常,LoginServlet类中的第34行的代码是什么?建议你将LoginServlet完成的代码粘贴一下,方便我们具有针对性的为你解答,要将代码粘贴到回答中哦,否则会失去代码格式,祝学习愉快~
- 参与学习 人
- 提交作业 277 份
- 解答问题 4297 个
Java数据库开发的必备技能,从流行的MySQL数据库开始,到Java原生的数据库管理接口JDBC的使用,再到常用的数据持久化框架MyBatis,让你向Java工程师的目标又迈进了一步!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星