IGeekShop案例10-用户登录基本功能的实现

ゝ一纸荒年。 2024-04-17 14:11 161阅读 0赞

IGeekShop案例10-用户登录基本功能的实现

1 login.jsp代码

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7. <title>会员登录</title>
  8. <link rel="stylesheet" href="css/bootstrap.min.css" type="text/css" />
  9. <script src="js/jquery-1.11.3.min.js" type="text/javascript"></script>
  10. <script src="js/bootstrap.min.js" type="text/javascript"></script>
  11. <!-- 引入自定义css文件 style.css -->
  12. <link rel="stylesheet" href="css/style.css" type="text/css" />
  13. <style>
  14. body {
  15. margin-top: 20px;
  16. margin: 0 auto;
  17. }
  18. .carousel-inner .item img {
  19. width: 100%;
  20. height: 300px;
  21. }
  22. .container .row div {
  23. /* position:relative;
  24. float:left; */
  25. }
  26. font {
  27. color: #666;
  28. font-size: 22px;
  29. font-weight: normal;
  30. padding-right: 17px;
  31. }
  32. </style>
  33. <script type="text/javascript">
  34. function changeImg(obj) {
  35. obj.src="${pageContext.request.contextPath }/checkImg?time="+new Date().getTime();
  36. }
  37. </script>
  38. </head>
  39. <body>
  40. <!-- 引入header.jsp -->
  41. <jsp:include page="/header.jsp"></jsp:include>
  42. <div class="container"
  43. style="width: 100%; height: 460px; background: #FF2C4C url('images/loginbg.jpg') no-repeat;">
  44. <div class="row">
  45. <div class="col-md-7">
  46. <!--<img src="./image/login.jpg" width="500" height="330" alt="会员登录" title="会员登录">-->
  47. </div>
  48. <div class="col-md-5">
  49. <div
  50. style="width: 440px; border: 1px solid #E7E7E7; padding: 20px 0 20px 30px; border-radius: 5px; margin-top: 60px; background: #fff;">
  51. <font>会员登录</font>USER LOGIN
  52. <div id="checkCodeInfo" style="color:red">${loginInfo}</div>
  53. <form class="form-horizontal" action="${pageContext.request.contextPath }/login" method="post">
  54. <div class="form-group">
  55. <label for="username" class="col-sm-2 control-label">用户名</label>
  56. <div class="col-sm-6">
  57. <input type="text" class="form-control" id="username" name="username"
  58. placeholder="请输入用户名">
  59. </div>
  60. </div>
  61. <div class="form-group">
  62. <label for="inputPassword3" class="col-sm-2 control-label">密码</label>
  63. <div class="col-sm-6">
  64. <input type="password" class="form-control" id="inputPassword3" name="password"
  65. placeholder="请输入密码">
  66. </div>
  67. </div>
  68. <div class="form-group">
  69. <label for="inputPassword3" class="col-sm-2 control-label">验证码</label>
  70. <div class="col-sm-3">
  71. <input type="text" class="form-control" id="inputPassword3" name="checkCode"
  72. placeholder="请输入验证码">
  73. </div>
  74. <div class="col-sm-3">
  75. <img src="${pageContext.request.contextPath }/checkImg" onclick="changeImg(this)"/>
  76. </div>
  77. </div>
  78. <div class="form-group">
  79. <div class="col-sm-offset-2 col-sm-10">
  80. <div class="checkbox">
  81. <label> <input type="checkbox"> 自动登录
  82. </label> <label> <input
  83. type="checkbox"> 记住用户名
  84. </label>
  85. </div>
  86. </div>
  87. </div>
  88. <div class="form-group">
  89. <div class="col-sm-offset-2 col-sm-10">
  90. <input type="submit" width="100" value="登录" name="submit"
  91. style="background: url('./images/login.gif') no-repeat scroll 0 0 rgba(0, 0, 0, 0); height: 35px; width: 100px; color: white;">
  92. </div>
  93. </div>
  94. </form>
  95. </div>
  96. </div>
  97. </div>
  98. </div>
  99. <!-- 引入footer.jsp -->
  100. <jsp:include page="/footer.jsp"></jsp:include>
  101. </body>
  102. </html>

2 web层LoginServlet

  1. package www.test.web.servlet;
  2. import java.io.IOException;
  3. import java.sql.SQLException;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import www.test.domain.User;
  9. import www.test.service.LoginService;
  10. public class LoginServlet extends HttpServlet {
  11. public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  12. // 解决乱码问题
  13. request.setCharacterEncoding("UTF-8");
  14. // 获得页面输入的验证
  15. String checkCode_client = request.getParameter("checkCode");
  16. // 获得生成图片的文字的验证码
  17. String checkCode_session = (String) request.getSession().getAttribute("checkcode_session");
  18. // 比对页面的和生成图片的文字的验证码是否一致
  19. if (!checkCode_session.equals(checkCode_client)) {
  20. request.setAttribute("loginInfo", "您的验证码不正确");
  21. request.getRequestDispatcher("/login.jsp").forward(request, response);
  22. return;
  23. }
  24. // 获取用户输入的数据
  25. String username = request.getParameter("username");
  26. String password = request.getParameter("password");
  27. LoginService service = new LoginService();
  28. User user = null;
  29. try {
  30. user = service.findUser(username, password);
  31. } catch (SQLException e) {
  32. e.printStackTrace();
  33. }
  34. if (user != null) {
  35. response.sendRedirect("/WEBTest24/index.jsp");
  36. } else {
  37. request.setAttribute("loginInfo", "密码或者用户名不正确");
  38. request.getRequestDispatcher("/login.jsp").forward(request, response);
  39. }
  40. }
  41. public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  42. doGet(request, response);
  43. }
  44. }

3 service层

  1. package www.test.service;
  2. import java.sql.SQLException;
  3. import www.test.dao.LoginDao;
  4. import www.test.domain.User;
  5. public class LoginService {
  6. public User findUser(String username, String password) throws SQLException {
  7. LoginDao dao = new LoginDao();
  8. return dao.findUser(username,password);
  9. }
  10. }

4 dao层

  1. package www.test.dao;
  2. import java.sql.SQLException;
  3. import org.apache.commons.dbutils.QueryRunner;
  4. import org.apache.commons.dbutils.handlers.BeanHandler;
  5. import www.test.domain.User;
  6. import www.test.utils.C3P0Utils;
  7. public class LoginDao {
  8. public User findUser(String username, String password) throws SQLException {
  9. QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
  10. String sql = "select * from user where username =? and password = ?";
  11. return qr.query(sql, new BeanHandler<User>(User.class), username,password);
  12. }
  13. }

发表评论

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

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

相关阅读