Java – session variables in ServletRequest

I need to access session variables through a filter I don't even know if it's possible In fact, the problem comes from javax Servlet. The dofilter method type implemented by filter is ServletRequest, while httpservlet inherits the class. The parameter request of dopost method is HttpServletRequest

>Can I access the session in ServletRequest in the filter? > Can you recommend me

thank you!

Solution

Just connect the 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);
    // ...
}

You can also see:

> Our servlet-filters wiki page

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>