为了账号安全,请及时绑定邮箱和手机立即绑定

Filter案例用户自动登录学习笔记

标签:
Java

写Filter一定要知道该Filter过滤哪个或哪些资源,不是所有的Filter都过滤/*的资源。

logn.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>  <meta http-equiv="content-type" content="text/html; charset=UTF-8">  </head>  <body>    <form action="/day19/LoginServlet" method="post">        <table border="1" align="center">            <caption><br>用户自动登录</caption>            <tr>                <th>用户名</th>                <td><input type="text" name="username"/></td>            </tr>            <tr>                <th>密码</th>                <td><input type="password" name="password"/></td>            </tr>            <tr>                <th>时间</th>                <td>                    <input checked name="time" type="radio" value="60"/>1分钟                    <input name="time" type="radio" value="180"/>3分钟                    <input name="time" type="radio" value="300"/>5分钟                </td>            </tr>            <tr>                <td colspan="2" align="center">                    <input type="submit" value="提交"/>                </td>            </tr>        </table>    </form>  </body></html>

login.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>  <meta http-equiv="content-type" content="text/html; charset=UTF-8">  </head>  <body>    <form action="/day19/LoginServlet" method="post">        <table border="1" align="center">            <caption><br>用户自动登录</caption>            <tr>                <th>用户名</th>                <td><input type="text" name="username"/></td>            </tr>            <tr>                <th>密码</th>                <td><input type="password" name="password"/></td>            </tr>            <tr>                <th>时间</th>                <td>             <input checked name="time" type="radio" value="60"/>1分钟                    <input name="time" type="radio" value="180"/>3分钟                    <input name="time" type="radio" value="300"/>5分                </td>            </tr>            <tr>                <td colspan="2" align="center">                    <input type="submit" value="提交"/>                </td>            </tr>        </table>    </form>  </body></html>

welcome.jsp

<%@ page language="java" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <body>    <%--     用户名->Filter->welcome.jsp    --%>    欢迎${!empty sessionScope.username?username:'游客'}光临  </body></html>

autoLoginFilter(过滤器)

import java.io.IOException;import javax.servlet.Filter;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;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;//对敏感页面或目录进行认证public class AutoLoginFilter implements Filter {    public void init(FilterConfig filterConfig) throws ServletException {    }    public void doFilter(ServletRequest req, ServletResponse res,FilterChain chain) throws IOException, ServletException {        HttpServletRequest request = (HttpServletRequest) req;        HttpServletResponse response = (HttpServletResponse) res;        //取得浏览器的Cookie        Cookie[] cookies = request.getCookies();         //记录cookie值        Cookie userCookie = null;        if(cookies!=null){            for(Cookie c : cookies){                if(c.getName().equals("usernameAndPassword")){                    userCookie = c;                    break;                }            }            //找到对应的Cookie            if(userCookie!=null){                String usernameAndPassword = userCookie.getValue();                String[] both = usernameAndPassword.split("_");                String username = both[0];                String password = both[1];                if(username.equals("jack") && password.equals("123")){                    request.getSession().setAttribute("username",username);                }            }        }        //发行资源        chain.doFilter(request,response);    }       public void destroy() {    }}

LoginServlet

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;//用户自动登录public class LoginServlet extends HttpServlet {    public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {        request.setCharacterEncoding("UTF-8");        String username = request.getParameter("username");        String password = request.getParameter("password");        if(username!=null && password!=null && username.trim().length()>0 && password.trim().length()>0){            if(username.equals("jack") && password.equals("123")){                //登录成功,将信息绑定到HttpSession域对象中                request.getSession().setAttribute("username",username);                request.getSession().setAttribute("password",password);                //向浏览器写入Cookie                Cookie cookie = new Cookie("usernameAndPassword",username+"_"+password);                int time = Integer.parseInt(request.getParameter("time"));                cookie.setMaxAge(time);                response.addCookie(cookie);                //重定向到welcome.jsp页面                response.sendRedirect(request.getContextPath()+"/welcome.jsp");            }        }    }}

web.xml

<filter>    <filter-name>AutoLoginFilter</filter-name>    <filter-class>cn.itcast.web.filter.AutoLoginFilter</filter-class>  </filter>  <filter-mapping>    <filter-name>AutoLoginFilter</filter-name>    <url-pattern>/welcome.jsp</url-pattern>  </filter-mapping>

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消