1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| package ruanko.newspublish.action.login;
import ruanko.newspublish.biz.UsersBiz; import ruanko.newspublish.entity.Users;
import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.math.BigInteger; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Objects;
public class login extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); Users users = new Users(); users.setUsername(request.getParameter("username")); String password = request.getParameter("password");
String hashPassword = md5(password);
users.setPassword(hashPassword);
UsersBiz usersBiz = new UsersBiz(); if (usersBiz.login(users.getUsername())!=null){ System.out.println("MysqlPasswordMd5 = "+usersBiz.login(users.getUsername()).getPassword()); if (Objects.equals(usersBiz.login(users.getUsername()).getPassword(), hashPassword)){ System.out.println("登入成功,跳转页面至 index.jsp"); request.getRequestDispatcher("index.jsp").forward(request,response); } }else { System.out.println("登入失败,返回login.jsp"); request.setAttribute("message","登入失败,请检查用户名和密码是否正确!"); request.getRequestDispatcher("login.jsp").forward(request,response); }
} public static String md5(String str){ MessageDigest messageDigest = null; try { messageDigest = MessageDigest.getInstance("MD5"); messageDigest.update(str.getBytes()); return new BigInteger(1,messageDigest.digest()).toString(16); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return null; } }
|