我这段代码哪里错了吗?报500
package com.imooc.servlet; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.websocket.RemoteEndpoint; import com.imooc.domain.User; @WebServlet("/LoginServlet") public class LoginServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //接收数据 String username=request.getParameter("username"); String password=request.getParameter("password"); //从ServletContext域中获得用户信息的集合 List<User> list=(List<User>)this.getServletContext().getAttribute("list"); for(User user:list) { //判断用户名是否正确 if(username.equals(user.getUsername())) { //用户名正确 if(password.equals(user.getPassword())) { //密码正确 //登录成功 //判断复选框是否勾选 String remember=request.getParameter("remember"); if("true".equals(remember)) { //记住用户名 Cookie cookie=new Cookie("username", user.getUsername()); //设置路径和时间 cookie.setPath("/reg_login"); cookie.setMaxAge(60*60*24); //将cookie回写到浏览器 response.addCookie(cookie); } //将用户的信息存在session中 request.getSession().setAttribute("user", user); response.sendRedirect("/reg_login/success.jsp"); return; } } } //登录失败 request.setAttribute("msg","用户名或密码错误"); request.getRequestDispatcher("/login.jsp").forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
HTTP Status 500 – Internal Server Error
Type Exception Report
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
java.lang.NullPointerException
com.imooc.servlet.LoginServlet.doGet(LoginServlet.java:28)
javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
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.
Apache Tomcat/8.5.32
正在回答 回答被采纳积分+1
package com.imooc.servlet; import java.util.List; import java.util.ArrayList; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import com.imooc.domain.*; /** * 用户注册的初始化的Servlet */ @WebServlet("/InitServlet") public class InitServlet extends HttpServlet { @Override public void init() throws ServletException { //创建一个list集合保存用户信息 List<User>list=new ArrayList<User>(); this.getServletContext().setAttribute("list", list); } }
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <display-name>reg_login</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <description></description> <display-name>InitServlet</display-name> <servlet-name>InitServlet</servlet-name> <servlet-class>com.imooc.servlet.InitServlet</servlet-class> <load-on-startup>2</load-on-startup> </servlet> </web-app>
<%@page import="com.imooc.utils.CookieUtils"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>登录页面</title> <link rel="stylesheet" href="./css/login.css"> </head> <body> <div class="login"> <div class="header"> <h1> <a href="./login.jsp">登录</a> <a href="./regist.jsp">注册</a> </h1> </div> <% String username=""; //获得从客户端携带过来的所有cookie Cookie[] cookies=request.getCookies(); //从Cookie的数组中查找指定名称的cookie Cookie cookie=CookieUtils.findCookie(cookies, "username"); if(cookie!=null){ username=cookie.getValue(); } if(session.getAttribute("username")!=null){ username=(String)session.getAttribute("username"); } String msg=""; if(request.getAttribute("msg")!=null){ msg=(String)request.getAttribute("msg"); } %> <h3><font color="red"><%=msg %></font></h3> <form action="/reg_login/LoginServlet" method="post" enctype="multipart/form-data"> <table> <tr> <td class="td1">用户名</td> <td><input type="text" class="input1" name="username" value="<%=username %>"></td> </tr> <tr> <td class="td1">密码</td> <td><input type="password" class="input1" name="password"></td> </tr> <tr> <td class="td1" colspan="2"> <input type="checkbox" name="remember" value="true" checked="checked"> 记住用户名</td> </tr> <tr> <td colspan="2"> <div class="btn-red"> <input type="submit" value="登录" id="login-btn"> </div> </td> </tr> </table> </form> </div> </body> </html>
package com.imooc.servlet; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.websocket.RemoteEndpoint; import com.imooc.domain.User; @WebServlet("/LoginServlet") public class LoginServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //接收数据 String username=request.getParameter("username"); String password=request.getParameter("password"); //从ServletContext域中获得用户信息的集合 List<User> list=(List<User>)this.getServletContext().getAttribute("list"); for(User user:list) { //判断用户名是否正确 if(username.equals(user.getUsername())) { //用户名正确 if(password.equals(user.getPassword())) { //密码正确 //登录成功 //判断复选框是否勾选 String remember=request.getParameter("remember"); if("true".equals(remember)) { //记住用户名 Cookie cookie=new Cookie("username", user.getUsername()); //设置路径和时间 cookie.setPath("/reg_login"); cookie.setMaxAge(60*60*24); //将cookie回写到浏览器 response.addCookie(cookie); } //将用户的信息存在session中 request.getSession().setAttribute("user", user); response.sendRedirect("/reg_login/success.jsp"); return; } } } //登录失败 request.setAttribute("msg","用户名或密码错误"); request.getRequestDispatcher("/login.jsp").forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>注册</title> <link rel="stylesheet" href="./css/reg.css"> </head> <body> <div class="reg"> <div class="header"> <h1> <a href="./login.jsp">登录</a> <a href="./regist.jsp">注册</a> </h1> </div> <!-- 文件上传的条件 *表单必须是post提交方式 *表单必须有文件上传项,文件上传项必须有name和值 *表单的enctype属性必须设置为multipart/from-data --> <% String msg=""; if(request.getAttribute("msg")!=null){ msg=(String)request.getAttribute("msg"); } %> <%-- <h3><font color="red"><%=msg %></h3> --%> <form action="/reg_login/RegistServlet" method="post" enctype="multipart/form-data"> <table> <tr> <td class="td1">用户名</td> <td><input type="text" class="input1" name="username"></td> </tr> <tr> <td class="td1">密码</td> <td><input type="password" class="input1" name="password"></td> </tr> <tr> <td class="td1">昵称</td> <td><input type="text" class="input1" name="nickname"></td> </tr> <tr> <td class="td1">性别</td> <td> <input type="radio" name="sex" value="male">男 <input type="radio" name="sex" value="female">女 </td> </tr> <tr> <td class="td1">上传头像</td> <td><input type="file" id="photo" name="upload"></td> </tr> <tr> <td class="td1">兴趣爱好</td> <td><label> <input type="checkbox" name="hobby" value="篮球">篮球 <input type="checkbox" name="hobby" value="足球">足球 <input type="checkbox" name="hobby" value="排球">排球 <input type="checkbox" name="hobby" value="羽毛球">羽毛球 </label></td> </tr> <tr> <td colspan="2"> <div class="btn-red"> <input type="submit" value="注册" id="reg-btn"> </div> </td> </tr> </table> </form> </div> </body> </html>
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星