Suresh Payankannur

Monday, September 15, 2014

Accessing Http Session In JAX-RS 2.0

JAX-RS 2.0 defines Filters and Interceptors. One can access the Http session by implementing a filter. For example:
import java.io.IOException;
import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.core.Context;
import javax.ws.rs.ext.Provider;

@Named
@Provider
public class MyInterceptor implements ContainerRequestFilter {
    @Context
    private HttpServletRequest servletRequest;

    public void filter(ContainerRequestContext requestContext) throws IOException {
        
        HttpSession session = servletRequest.getSession(false);

        if (session != null) {
            // do stuff
        }
    }
}

3 comments:

  1. I seem to be getting session as null everytime. Any ideas?

    ReplyDelete
  2. Thanks for the post, I am techno savvy. I believe you hit the nail right on the head. I am highly impressed with your blog. It is very nicely explained. Your article adds best knowledge to our Java Online Training from India. or learn thru Java Online Training from India Students.

    ReplyDelete

Blog Archive

Scroll To Top