简单的登录页面 一次请求为什么servlet执行了两次
package com.neusoft.web; import java.io.IOException; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.neusoft.services.impl.LoginServices; /** * Servlet implementation class LoginServlet */ @WebServlet("/login.html") public class LoginServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String path="/login.jsp"; try { //1.获取页面数据 String uname=request.getParameter("uname"); String pwd=request.getParameter("pwd"); //2.实例化Services LoginServices services=new LoginServices(); //3.获取用户信息 Map<String,String> userinfo=services.checkUser(uname, pwd); System.out.println("********************************"); if(userinfo!=null) { //System.out.println(userinfo); //将用户数据存入session request.getSession().setAttribute("USERINFO", userinfo); //跳转到系统主页 path="/Main.jsp"; } else { request.setAttribute("msg", "用户名或密码错误!"); } } catch(Exception ex) { ex.printStackTrace(); } request.getRequestDispatcher(path).forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }
jsp代码
<%@ page language="java" pageEncoding="GBK"%> <html> <head> <title>Insert title here</title> <style type="text/css"> body {background-color: gray} </style> </head> <body> <pre> </pre> <form action="<%=request.getContextPath()%>/login.html" method="post"> <table border="1" width="45%" align="center"> <tr> <td colspan="100"> ${msg } </td> </tr> <tr> <td>登录名:</td> <td> <input type="text" name="uname" required="required" autofocus="autofocus"> </td> </tr> <tr> <td>密码:</td> <td> <input type="password" name="pwd" required="required"> </td> </tr> <tr> <td colspan="100" align="center"> <input type="submit" name="next" value="登录"> </td> </tr> </table> </form> </body> </html>
代码运行可能用到
LoginServices里的
checkUser方法 ,就是验证账号密码的,
第一次进入是可以获取页面的账号密码pwd与uname是有值的,进行一次验证,然后他会再次调用访问一次,这是pwd
uname是null,而获取不到账号密码,然后
密码调用md5加密方法的时候就会空指针异常。
求大佬解惑
出问题的jsp。
<%@ page language="java" pageEncoding="GBK"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%String path=request.getContextPath();%> <html> <head> <title>用户登录</title> </head> <body> <pre> </pre> <form action="<%=path%>/login" method="post"> <table border="1" align="center" width="30%"> <tr> <td>账号:</td> <td> <input type="text" name="uacount" required="required"> </td> </tr> <tr> <td>密码:</td> <td> <input type="password" name="upwd" required="required"> </td> </tr> <tr> <td colspan="100" align="center"> <input type="submit" name="next" value="登录"> </td> </tr> </table> </form> </body> </html>
<%@ page language="java" pageEncoding="GBK"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%String path=request.getContextPath();%> <html> <head> <title>用户登录</title> </head> <body> <pre> </pre> <form action="<%=path%>/login" method="post"> <table border="1" align="center" width="30%"> <tr> <td>账号:</td> <td> <input type="text" name="uacount" required="required"> </td> </tr> <tr> <td>密码:</td> <td> <input type="password" name="upwd" required="required"> </td> </tr> <tr> <td colspan="100" align="center"> <input type="submit" name="next" value="登录"> </td> </tr> </table> </form> </body> </html>
页面get请求和post请求用的是一个servlet吧,应该分开吧。
这个没影响吧 post提交在里边调用doget方法。
三次握手啊