servlet+jsp用户登录实现

淡淡的烟草味﹌ 2022-05-27 12:13 279阅读 0赞

项目结构如下:

20180418163033324

登录界面

20180418163141330

登录成功

20180418163254854

登录失败

20180418163313378

user代码段

  1. package bean;
  2. import java.sql.Connection;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import util.DB;
  7. public class user {
  8. private String username;
  9. private String password;
  10. public String getUsername() {
  11. return username;
  12. }
  13. public void setUsername(String username) {
  14. this.username = username;
  15. }
  16. public String getPassword() {
  17. return password;
  18. }
  19. public void setPassword(String password) {
  20. this.password = password;
  21. }
  22. public boolean Check(String username,String password)throws Exception{
  23. Connection conn=null;
  24. ResultSet rest=null;
  25. PreparedStatement prst=null;
  26. String sql="select * from user where username=?";
  27. boolean flag=false;
  28. try{
  29. conn=DB.getConn();
  30. prst=conn.prepareStatement(sql);
  31. prst.setString(1, username);
  32. rest=prst.executeQuery();
  33. if(rest.next()){
  34. if(password.equals(rest.getString("password"))){
  35. flag=true;
  36. }else{
  37. throw new Exception("密码不对哦");
  38. }
  39. }else{
  40. throw new Exception("用户名不存在");
  41. }
  42. }catch(Exception e){
  43. e.printStackTrace();
  44. }finally{
  45. try {
  46. rest.close();
  47. } catch (SQLException e) {
  48. // TODO Auto-generated catch block
  49. e.printStackTrace();
  50. }
  51. try {
  52. prst.close();
  53. } catch (SQLException e) {
  54. // TODO Auto-generated catch block
  55. e.printStackTrace();
  56. }
  57. try {
  58. conn.close();
  59. } catch (SQLException e) {
  60. // TODO Auto-generated catch block
  61. e.printStackTrace();
  62. }
  63. }
  64. return flag;
  65. }
  66. }

DB代码段

  1. package util;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. public class DB {
  5. public static Connection getConn(){
  6. Connection conn=null;
  7. try{
  8. String url="jdbc:mysql://localhost/student";
  9. String user="root";
  10. String password="root";
  11. Class.forName("com.mysql.jdbc.Driver");
  12. conn=DriverManager.getConnection(url, user, password);
  13. if(conn!=null){
  14. System.out.println("数据库连接成功!");
  15. }
  16. }catch(Exception e){
  17. e.printStackTrace();
  18. }
  19. return conn;
  20. }
  21. }

userServlet代码段

  1. package servlet;
  2. import java.io.IOException;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.annotation.WebServlet;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import bean.user;
  9. /**
  10. * Servlet implementation class userServlet
  11. */
  12. @WebServlet(
  13. urlPatterns={"/userServlet"},name="userServlet"
  14. )
  15. public class userServlet extends HttpServlet {
  16. private static final long serialVersionUID = 1L;
  17. /**
  18. * @see HttpServlet#HttpServlet()
  19. */
  20. public userServlet() {
  21. super();
  22. // TODO Auto-generated constructor stub
  23. }
  24. /**
  25. * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  26. */
  27. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  28. // TODO Auto-generated method stub
  29. doPost(request,response);
  30. }
  31. /**
  32. * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  33. */
  34. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  35. // TODO Auto-generated method stub
  36. request.setCharacterEncoding("utf-8");//处理表单中文乱码
  37. user u=new user();
  38. String username,password;
  39. try{
  40. username=request.getParameter("username");
  41. password=request.getParameter("password");
  42. if(u.Check(username, password)){
  43. u.setUsername(username);
  44. u.setPassword(password);
  45. request.getSession().setAttribute("use", u);
  46. request.getRequestDispatcher("/Success.jsp").forward(request, response);
  47. }else{
  48. request.getRequestDispatcher("/fail.jsp").forward(request, response);
  49. }
  50. }catch(Exception e){
  51. e.printStackTrace();
  52. }
  53. }
  54. }

login.jsp代码段

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="<%=basePath%>">
  10. <title>用户登录</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <meta http-equiv="description" content="This is my page">
  16. <!--
  17. <link rel="stylesheet" type="text/css" href="styles.css">
  18. -->
  19. </head>
  20. <body>
  21. <center>
  22. <form action="<%=request.getContextPath() %>/userServlet" method="post">
  23. <table>
  24. <tr>
  25. <td>用户名:</td>
  26. <td><input type="text" name="username"></td>
  27. </tr>
  28. <tr>
  29. <td>密码:</td>
  30. <td><input type="password" name="password"></td>
  31. </tr>
  32. <tr>
  33. <td></td>
  34. <td><input type="submit" value="登录"></td>
  35. </tr>
  36. </table>
  37. </form>
  38. </center>
  39. </body>
  40. </html>

success.jsp代码段

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <jsp:useBean id="use" class="bean.user" scope="session"></jsp:useBean>
  3. <%
  4. String path = request.getContextPath();
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  6. %>
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  8. <html>
  9. <head>
  10. <base href="<%=basePath%>">
  11. <title>登录成功</title>
  12. <meta http-equiv="pragma" content="no-cache">
  13. <meta http-equiv="cache-control" content="no-cache">
  14. <meta http-equiv="expires" content="0">
  15. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  16. <meta http-equiv="description" content="This is my page">
  17. <!--
  18. <link rel="stylesheet" type="text/css" href="styles.css">
  19. -->
  20. </head>
  21. <body>
  22. <table>
  23. <tr>
  24. <td>用户名:</td>
  25. <td><jsp:getProperty name="use" property="username"/></td>
  26. </tr>
  27. <tr>
  28. <td>密码:</td>
  29. <td><jsp:getProperty name="use" property="password"/></td>
  30. </tr>
  31. </table>
  32. </body>
  33. </html>

fail.jsp代码段

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="<%=basePath%>">
  10. <title>登录失败</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <meta http-equiv="description" content="This is my page">
  16. <!--
  17. <link rel="stylesheet" type="text/css" href="styles.css">
  18. -->
  19. </head>
  20. <body>
  21. 登录失败!<a href="login.jsp">返回登录</a>
  22. </body>
  23. </html>

发表评论

表情:
评论列表 (有 0 条评论,279人围观)

还没有评论,来说两句吧...

相关阅读

    相关 JavaBean实现用户登录界面

    应用 JavaBean 方式实现用户的登录验证,当用户在表单中填写正确的用户名和密码后,提示成功登录。若输入的密码错误,则提示“密码错误,请输入正确密码!”。