Web(过滤器,cookie)

创建一个过滤器

public class AuthenticationFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // 在这里进行一些初始化操作,比如读取配置文件等
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        // 在这里对请求进行身份验证的操作
        // 如果验证成功,调用chain.doFilter方法将请求传递到下一个过滤器或Servlet;否则,返回错误信息
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {
        // 在这里进行一些资源回收的操作,比如关闭数据库连接等
    }
}

配置过滤器


在web.xml文件中,我们需要配置过滤器的相关信息,包括过滤器的类名和过滤路径等。示例如下:

<filter>
    <filter-name>AuthenticationFilter</filter-name>
    <filter-class>com.example.AuthenticationFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>AuthenticationFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

上述代码中,我们使用标签来定义一个过滤器,并指定了过滤器的类名为com.example.AuthenticationFilter。同时,我们使用标签来定义过滤器的映射路径,即对所有请求进行过滤。
 

init方法在过滤器被初始化时调用,可以进行一些初始化操作;doFilter方法用于处理请求,可以进行身份验证等操作,也可以通过调用chain.doFilter方法继续将请求传递到下一个过滤器或Servlet;destroy方法在过滤器被销毁时调用,可以进行一些资源回收等操作。

 使用过滤器


当我们访问Web应用程序中的某个页面时,过滤器会自动进行过滤和处理。如果请求与过滤器的映射路径匹配,则过滤器的doFilter方法将被调用,可以在该方法中进行一些额外的操作,比如身份验证、日志记录等等。如果过滤器需要继续将请求传递到下一个过滤器或Servlet,可以通过调用chain.doFilter方法实现。如果不希望将请求传递到下一个过滤器或Servlet,可以直接返回相应的错误信息。最后,在过滤器被销毁时,可以进行一些资源回收等操作。
 

什么是Cookie


      Cookie是由服务器端产生并发送给客户端浏览器的,浏览器会将发送过来的Cookie存储到某个文件中,随后再一次请求时,会自动带着存储的Cookie一并请求到服务器端(注意: Cookie虽然保存了sessionId,但它并不是JSP的内置对象,是需要实例化的)。

Cookie的作用


     负责客户端和服务器端的文本传递,最终文本保存在浏览器。

Cookie的工作原理


      浏览器发送第一次请求时,服务器会判断有没有Cookie,如果没有就创建Cookie,以名值对的形式将sessionId保存在Cookie中并响应,浏览器接收响应并把Cookie保存在客户端浏览器,当浏览器下一次发送请求时,request中会自动带着Cookie去发送到服务器端,服务器接收处理并再次响应。
 

使用Cookie存储数据

1.创建Cookie对象

 Cookie newCookie=new Cookie(String name,String value);

Cookie存储的数据类型是字符串

Cookie的常用方法
方法名称            说明
void setMaxAge(int expiry)    
设置Cookie的有效期,以秒为单位

: 当expiry 参数大于0时,表示Cookie的有效期

: 当expiry 参数等于0时,表示客户端删除该Cookie

: 当expiry 参数小于0或者不设置时,表示Cookie关闭浏览器窗口后失效。

void setValue(String value)    在Cookie创建后,对Cookie进行赋值
String getName()    获取Cookie的名称
String getValue()    获取Cookie的值
int getMaxAge()    获取Cookie的有效时间,以秒为单位
2.将Cookie对象写入响应
response.addCookie(newCookie);

写入响应Cookie才能被浏览器保存

3.从请求中读取Cookie数据
  Cookie是使用name/value的形式保存的,它是一个数组,遍历数组时需要使用getName()方法来审查。

for (Cookie cookie : cookies) {
       if (cookie.getName().equals("userName")) {
                   //执行操作
            }
 }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过使用过滤器(Filter)来实现用户的自动登录,以下是实现步骤: 1. 创建一个过滤器,实现Filter接口,并在web.xml中配置过滤器: ```xml <filter> <filter-name>AutoLoginFilter</filter-name> <filter-class>com.example.AutoLoginFilter</filter-class> </filter> <filter-mapping> <filter-name>AutoLoginFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> ``` 2. 在过滤器中实现自动登录的逻辑,具体步骤如下: - 在过滤器初始化时,获取ServletContext对象,保存在FilterConfig中,以便后续使用: ```java public void init(FilterConfig filterConfig) throws ServletException { this.context = filterConfig.getServletContext(); } ``` - 在过滤器中的doFilter方法中判断用户是否已经登录,如果未登录,则从Cookie中获取用户信息,然后进行自动登录: ```java public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse res = (HttpServletResponse) response; HttpSession session = req.getSession(); if (session.getAttribute("user") == null) { // 从Cookie中获取用户信息 Cookie[] cookies = req.getCookies(); String username = null; String password = null; if (cookies != null) { for (Cookie cookie : cookies) { if (cookie.getName().equals("username")) { username = cookie.getValue(); } else if (cookie.getName().equals("password")) { password = cookie.getValue(); } } } // 如果Cookie中存在用户信息,则进行自动登录 if (username != null && password != null) { User user = userService.login(username, password); if (user != null) { session.setAttribute("user", user); } } } chain.doFilter(request, response); } ``` - 在用户登录成功后,将用户信息保存在Session中,然后设置Cookie保存用户信息: ```java session.setAttribute("user", user); Cookie usernameCookie = new Cookie("username", user.getUsername()); usernameCookie.setMaxAge(60 * 60 * 24 * 7); res.addCookie(usernameCookie); Cookie passwordCookie = new Cookie("password", user.getPassword()); passwordCookie.setMaxAge(60 * 60 * 24 * 7); res.addCookie(passwordCookie); ``` 这样,就可以实现用户的自动登录了。当用户访问网站时,过滤器会先判断用户是否已经登录,如果未登录,则从Cookie中获取用户信息,然后进行自动登录。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值