自动登陆
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> </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> <span style="color: red">${loginInfo }</span> </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" placeholder="请输入验证码"> </div> <div class="col-sm-3"> <img src="./image/captcha.jhtml" /> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <div class="checkbox"> <label> <input type="checkbox" name="autoLogin" value="autoLogin"> 自动登录 </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>
LoginServlet
package com.itheima.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.itheima.domain.User; import com.itheima.service.UserService; public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=UTF-8"); HttpSession session = request.getSession(); String username = request.getParameter("username"); String password = request.getParameter("password"); UserService service = new UserService(); User user = service.login(username,password); if(user != null){ //登陆成功 //判断用户是否勾选自动登录 String autoLogin = request.getParameter("autoLogin"); if(autoLogin!=null){ Cookie cookie_username = new Cookie("cookie_username",username); Cookie cookie_password = new Cookie("cookie_password",password); cookie_username.setMaxAge(60*60); cookie_password.setMaxAge(60*60); cookie_username.setPath(request.getContextPath()); cookie_password.setPath(request.getContextPath()); response.addCookie(cookie_username); response.addCookie(cookie_password); } //将user存入session session.setAttribute("user", user); response.sendRedirect(request.getContextPath()+"/index.jsp"); }else{ request.setAttribute("loginInfo", "用户名或密码错误"); request.getRequestDispatcher("/login.jsp").forward(request, response); } } }
UserService
package com.itheima.service; import java.sql.SQLException; import com.itheima.dao.UserDao; import com.itheima.domain.User; public class UserService { public User login(String username, String password) { UserDao dao = new UserDao(); User user = null; try { user = dao.login(username,password); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return user; } }
UserDao
package com.itheima.dao; import java.sql.SQLException; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler; import com.itheima.domain.User; import com.itheima.utils.DataSourceUtils; public class UserDao { public User login(String username, String password) throws SQLException { QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource()); String sql = "select * from user where username=? and password=?"; return runner.query(sql,new BeanHandler<User>(User.class),username,password); } }
AutoLoginFilter
package com.itheima.servlet; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.itheima.domain.User; import com.itheima.service.UserService; public class AutoLoginFilter implements Filter { public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) resp; HttpSession session = request.getSession(); //定义 String cookie_username = null; String cookie_password = null; //获得cookie中的用户名和密码进行登录 Cookie[] cookies = request.getCookies(); if(cookies!=null){ for (Cookie cookie : cookies) { if("cookie_username".equals(cookie.getName())){ cookie_username = cookie.getValue(); } if("cookie_password".equals(cookie.getName())){ cookie_password = cookie.getValue(); } } } if(cookie_username != null && cookie_password != null){ //登录代码 UserService service = new UserService(); User user = service.login(cookie_username, cookie_password); session.setAttribute("user", user); } chain.doFilter(req, resp); } }