Session variables in ServletRequest

前端 未结 2 740
后悔当初
后悔当初 2021-01-08 01:01

I need to access session variables through a filter. I don\'t even know if it is possible. In practice, the problem is that the doFilter method type from

相关标签:
2条回答
  • 2021-01-08 01:29

    Sure you can. ServletRequest allows you access to session that contains attributes. You can review, add, remove and modify attributes whenever you want either in filter, servlet, jsp, session listener. This technique is very useful and especially attended for communication between different components within the same session.

    0 讨论(0)
  • 2021-01-08 01:41

    Just cast the obtained ServletRequest to HttpServletRequest.

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpSession session = request.getSession(false);
        // ...
    }
    

    See also:

    • Our servlet-filters wiki page
    0 讨论(0)
提交回复
热议问题