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 javax.Servlet.Filter
implementation is ServletRequest
, whilst HttpServlet inherited classes, doPost method parameter request
is HttpServletRequest.
Thanks!
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);
// ...
}