user1050619
user1050619

Reputation: 20856

Adding a filter to check if a session is valid or not

Im adding a filter to check if a session is valid or not.

Added the following but get the error

public void doFilter(ServletRequest request, ServletResponse response,
        FilterChain chain) throws java.io.IOException, ServletException {

    if (request.getRequestedSessionId() != null
            && !request.isRequestedSessionIdValid()) {

Error:-

The method getRequestedSessionId() is undefined for the type ServletRequest

Upvotes: 2

Views: 766

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279970

The method getRequestedSessionId (and isRequestedSessionIdValid) is declared on the HttpServletRequest interface. You're trying to invoke the method on a reference of type ServletRequest. If you know the referenced object will really be a HttpServletRequest, cast it in order to invoke the method.

HttpServletRequest httpRequest = (HttpServletRequest) request;
if (httpRequest.getRequestedSessionId() != null && !httpRequest.isRequestedSessionIdValid()) {...

Upvotes: 4

Related Questions