案例10-用户登录基本功能的实现
1 login.jsp代码
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>会员登录</title> <link rel="stylesheet" href="css/bootstrap.min.css" type="text/css" /> <script src="js/jquery-1.11.3.min.js" type="text/javascript"></script> <script src="js/bootstrap.min.js" type="text/javascript"></script> <!-- 引入自定义css文件 style.css --> <link rel="stylesheet" href="css/style.css" type="text/css" /> <style> body { margin-top: 20px; margin: 0 auto; } .carousel-inner .item img { width: 100%; height: 300px; } .container .row div { /* position:relative; float:left; */ } font { color: #666; font-size: 22px; font-weight: normal; padding-right: 17px; } </style> <script type="text/javascript"> function changeImg(obj) { obj.src="${pageContext.request.contextPath }/checkImg?time="+new Date().getTime(); } </script> </head> <body> <!-- 引入header.jsp --> <jsp:include page="/header.jsp"></jsp:include> <div class="container" style="width: 100%; height: 460px; background: #FF2C4C url('images/loginbg.jpg') no-repeat;"> <div class="row"> <div class="col-md-7"> <!--<img src="./image/login.jpg" width="500" height="330" alt="会员登录" title="会员登录">--> </div> <div class="col-md-5"> <div style="width: 440px; border: 1px solid #E7E7E7; padding: 20px 0 20px 30px; border-radius: 5px; margin-top: 60px; background: #fff;"> <font>会员登录</font>USER LOGIN <div id="checkCodeInfo" style="color:red">${loginInfo}</div> <form class="form-horizontal" action="${pageContext.request.contextPath }/login" method="post"> <div class="form-group"> <label for="username" class="col-sm-2 control-label">用户名</label> <div class="col-sm-6"> <input type="text" class="form-control" id="username" name="username" placeholder="请输入用户名"> </div> </div> <div class="form-group"> <label for="inputPassword3" class="col-sm-2 control-label">密码</label> <div class="col-sm-6"> <input type="password" class="form-control" id="inputPassword3" name="password" placeholder="请输入密码"> </div> </div> <div class="form-group"> <label for="inputPassword3" class="col-sm-2 control-label">验证码</label> <div class="col-sm-3"> <input type="text" class="form-control" id="inputPassword3" name="checkCode" placeholder="请输入验证码"> </div> <div class="col-sm-3"> <img src="${pageContext.request.contextPath }/checkImg" onclick="changeImg(this)"/> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <div class="checkbox"> <label> <input type="checkbox"> 自动登录 </label> <label> <input type="checkbox"> 记住用户名 </label> </div> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <input type="submit" width="100" value="登录" name="submit" style="background: url('./images/login.gif') no-repeat scroll 0 0 rgba(0, 0, 0, 0); height: 35px; width: 100px; color: white;"> </div> </div> </form> </div> </div> </div> </div> <!-- 引入footer.jsp --> <jsp:include page="/footer.jsp"></jsp:include> </body> </html>
2 web层LoginServlet
package www.test.web.servlet; import java.io.IOException; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import www.test.domain.User; import www.test.service.LoginService; public class LoginServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 解决乱码问题 request.setCharacterEncoding("UTF-8"); // 获得页面输入的验证 String checkCode_client = request.getParameter("checkCode"); // 获得生成图片的文字的验证码 String checkCode_session = (String) request.getSession().getAttribute("checkcode_session"); // 比对页面的和生成图片的文字的验证码是否一致 if (!checkCode_session.equals(checkCode_client)) { request.setAttribute("loginInfo", "您的验证码不正确"); request.getRequestDispatcher("/login.jsp").forward(request, response); return; } // 获取用户输入的数据 String username = request.getParameter("username"); String password = request.getParameter("password"); LoginService service = new LoginService(); User user = null; try { user = service.findUser(username, password); } catch (SQLException e) { e.printStackTrace(); } if (user != null) { response.sendRedirect("/WEBTest24/index.jsp"); } else { request.setAttribute("loginInfo", "密码或者用户名不正确"); request.getRequestDispatcher("/login.jsp").forward(request, response); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
3 service层
package www.test.service; import java.sql.SQLException; import www.test.dao.LoginDao; import www.test.domain.User; public class LoginService { public User findUser(String username, String password) throws SQLException { LoginDao dao = new LoginDao(); return dao.findUser(username,password); } }
4 dao层
package www.test.dao; import java.sql.SQLException; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler; import www.test.domain.User; import www.test.utils.C3P0Utils; public class LoginDao { public User findUser(String username, String password) throws SQLException { QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource()); String sql = "select * from user where username =? and password = ?"; return qr.query(sql, new BeanHandler<User>(User.class), username,password); } }