TechnoCrat
TechnoCrat

Reputation: 2065

How to redirect user to custom web page after session times out?

I want to redirect user to session timed out page through my code. What logic I can write in my filter which would check whether the session has been timed out and redirect user to that custom page (say CustomSessionTimeout.jsp). This page need not have to pass through any filtering.

This is the signature of one method in first filter

public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)

Upvotes: 1

Views: 329

Answers (1)

BalusC
BalusC

Reputation: 1108742

You can check that by looking if the user has sent a session cookie along with the request which is not valid anymore according to the server.

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) {
    HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = (HttpServletResponse) res;

    if (request.getRequestedSessionId() != null && !request.isRequestedSessionIdValid()) {
        // Session has expired! Now do your job.
        response.sendRedirect(request.getContextPath() + "/CustomSessionTimeout.jsp");
        return;
    }

    chain.doFilter(req, res);
}

Upvotes: 1

Related Questions