1登录页面-login.jsp
<%--
Created by IntelliJ IDEA.
User: yxl
Date: 2019/9/6
Time: 11:17
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>用户登录</title>
</head>
<body>
<h2>用户登录</h2>
<form action="${pageContext.request.contextPath}/loginservlet" method="post">
用户名:<input type="text" name="username"><br/>
密码:<input type="text" name="password"><br/>
<input type="checkbox" name="auto">记住我<br/>
<input type="submit" value="登录">
</form>
</body>
</html>
2登录的servlet-LoginServlet
package com.qf.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Base64;
/*
* yxl 2019/9/6 11:20
* 佛祖保佑,永无BUG!
*/
@WebServlet(name = "LoginServlet",value = "/loginservlet")
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//处理
//乱码
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//接受数据
String username = request.getParameter("username");
String password = request.getParameter("password");
String auto = request.getParameter("auto");
System.out.println("auto:"+auto);
//判断
if(username.equals("admin")&&password.equals("888")){
//成功
//把用户信息放入session中
request.getSession().setAttribute("username", username);
System.out.println("登录成功");
//判断有没有勾选自动登录
if(auto!=null){
//创建cookie
String info=username+"#"+password;
info= Base64.getEncoder().encodeToString(info.getBytes());
Cookie cookie=new Cookie("info", info);
cookie.setMaxAge(60*60*24*7);
cookie.setPath("/");
cookie.setHttpOnly(true);
response.addCookie(cookie);
System.out.println("服务器创建了cookie");
}
//重定向
response.sendRedirect("index.jsp");
}else{
response.getWriter().write("用户名或密码错误");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
3自动登录过滤器-AutoLoginFilter
package com.qf.filter2;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Base64;
/*
* yxl 2019/9/6 11:33
* 佛祖保佑,永无BUG!
*/
@WebFilter(filterName = "AutoLoginFilter",value = "/index.jsp")
public class AutoLoginFilter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
HttpServletRequest request= (HttpServletRequest) req;
HttpServletResponse response= (HttpServletResponse) resp;
//自动登录
//1当前是否已经登录
Object username = request.getSession().getAttribute("username");
if(username!=null){
chain.doFilter(req, resp);
return;
}
//2获取cookie
Cookie[] cookies = request.getCookies();
if(cookies!=null){
for (Cookie cookie : cookies) {
if(cookie.getName().equals("info")){
String value = cookie.getValue();
//解码
byte[] bytes= Base64.getDecoder().decode(value);
String info=new String(bytes);
System.out.println("info:"+info);
String[] infos = info.split("#");
if(infos[0].equals("admin")&&infos[1].equals("888")){
//成功
request.getSession().setAttribute("username", infos[0]);
chain.doFilter(req, resp);
return;
}else{
//删除cookie
Cookie cookie1=new Cookie("info", "");
cookie1.setMaxAge(0);//表示删除
cookie1.setPath("/");
response.addCookie(cookie1);
response.getWriter().write("登录信息已过期,请重新登录");
}
}
}
}else{
//既没session也没cookie
response.sendRedirect(request.getContextPath() + "/login.jsp");
}
//chain.doFilter(req, resp);
}
public void init(FilterConfig config) throws ServletException {
}
}
4首页面-index.jsp
<%--
Created by IntelliJ IDEA.
User: yxl
Date: 2019/9/6
Time: 9:05
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
<%--<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="0">--%>
</head>
<body>
<h2>网站首页:登录名:${username}</h2>
</body>
</html>