过滤器版本的Cookie,Session自动登录案例
先写几个简单的html页面(主页,登录,注册,个人中心)
注册代码
@WebServlet("/register")
public class RegisterServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String userName=req.getParameter("userName");
String password=req.getParameter("password");
String repassword=req.getParameter("repassword");
if(userName==null||userName.isEmpty()||password==null||password.isEmpty()) {
System.out.println("对不起,用户名或密码不能为空");
req.getRequestDispatcher("pages/register.html").forward(req, resp);
return;
}
BufferedReader br=new BufferedReader(new FileReader("d://demo.txt"));
String str;
while((str=br.readLine())!=null) {
String[] strs=str.split("=");
if(strs[0].equals(userName)) {
System.out.println("对不起,用户名已存在");
req.getRequestDispatcher("pages/register.html").forward(req, resp);
br.close();
return;
}
}
br.close();
if(password.equals(repassword)) {
System.out.println("恭喜您,注册成功");
BufferedWriter bw=new BufferedWriter(new FileWriter("d://demo.txt",true));
bw.write(userName+"="+password);
bw.newLine();
bw.close();
resp.sendRedirect("/day_31/pages/login.html");
}else {
System.out.println("两次输入的密码不一致");
req.getRequestDispatcher("pages/register.html").forward(req, resp);
}
}
}
登录代码
@WebServlet("/login")
public class LoginServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String userName=req.getParameter("userName");
String password=req.getParameter("password");
if(userName==null||userName.isEmpty()||password==null||password.isEmpty()) {
System.out.println("对不起,用户名或密码不能为空");
req.getRequestDispatcher("pages/login.html").forward(req, resp);
return;
}
BufferedReader br=new BufferedReader(new FileReader("d://demo.txt"));
String str;
while((str=br.readLine())!=null) {
String[] strs=str.split("=");
if(strs[0].equals(userName)&&strs[1].equals(password)) {
System.out.println("恭喜您,登录成功");
req.getSession().setAttribute("login", true);
Cookie cookie=new Cookie("uap", userName+"="+password);
cookie.setMaxAge(7*24*3600);
resp.addCookie(cookie);
resp.sendRedirect("/day_31/pages/home.html");
br.close();
return;
}
}
br.close();
req.getRequestDispatcher("pages/login.html").forward(req, resp);
}
}
个人中心代码
@WebServlet("/infopage")
public class InfoPageServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if(req.getSession().getAttribute("login")!=null) {
req.getRequestDispatcher("/WEB-INF/pages/infoPage.html").forward(req, resp);
}else {
resp.sendRedirect("/day_31/pages/login.html");
}
}
}
过滤器代码
@WebFilter("/*")
public class AutoLoginFilter implements Filter{
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest req=(HttpServletRequest) request;
HttpServletResponse resp=(HttpServletResponse) response;
HttpSession session = req.getSession();
if(session.getAttribute("login")!=null) {
chain.doFilter(request, response);
return;
}
Cookie[] cookies=req.getCookies();
if(cookies==null) {
chain.doFilter(request, response);
return;
}
w:for (Cookie cookie : cookies) {
if("uap".equals(cookie.getName())){
String value=cookie.getValue();
String[] values=value.split("=");
BufferedReader br=new BufferedReader(new FileReader("d://demo.txt"));
String str;
while((str=br.readLine())!=null) {
String[] strs=str.split("=");
if(!strs[0].equals(values[0])) {
continue;
}
if(!strs[1].equals(values[1])) {
//用户名相同,密码不同,说明修改过密码,清除cookie
Cookie cookie2=new Cookie("uap", "");
cookie2.setMaxAge(0);
resp.addCookie(cookie2);
br.close();
break w;
}
req.getSession().setAttribute("login", true);
break w;
}
//没有找到对应的用户名密码,说明用户名密码错误
break w;
}
}
chain.doFilter(request, response);
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub
}
}
注销代码
@WebServlet("/logout")
public class LogOutServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.getSession().removeAttribute("login");
Cookie cookie=new Cookie("uap", "");
cookie.setMaxAge(0);
resp.addCookie(cookie);
resp.sendRedirect("/day_31/pages/login.html");
}
}