Retrieving servlet context, session and request in a POJO outside container

Is there any way to retrieve a session from a POJO? Or ultimately to retrieve a bean from a POJO.

To clarify:

Basically I am creating a bean from a servlet and I need to access the properties of that bean from outside of the web container (from a POJO). I cannot pass the request to the pojo; and the request is needed to retrieve the session.

More specifically I have a web application that uses the Cactus framework to run JUnit tests from a web interface. However the servlet that invokes the JUnit test runner is compiled in a jar; I added extra drop down menus to change settings from which the JUnit test will read from to switch between different environments (WLI clusters), so given that the runner servlet is already compiled I cannot modify it to handle the extra parameters from the multiple environments. I have tried the persistence approach of writing to a .dat file fro which the JUnit test will read from by way of a Reader class; also I have have tried the bean approach which ultimately was not accessible from the JUnit test.


Only and only if your POJO is running in the same thread as the HttpServletRequest is running in, then you'll be able to achieve this with help of ThreadLocal<T> .

Create the following class:

public final class YourContext implements AutoCloseable {

    private static ThreadLocal<YourContext> instance = new ThreadLocal<>();

    private HttpServletRequest request;
    private HttpServletResponse response;

    private YourContext(HttpServletRequest request, HttpServletResponse response) {
        this.request = request;
        this.response = response;
    }

    public static YourContext create(HttpServletRequest request, HttpServletResponse response) {
        YourContext context = new YourContext(request, response);
        instance.set(context);
        return context;
    }

    public static YourContext getCurrentInstance() {
        return instance.get();
    }

    @Override    
    public void close() {
        instance.remove();
    }

    public HttpServletRequest getRequest() {
        return request;
    }

    public HttpSession getSession() {
        return request.getSession();
    }

    public ServletContext getServletContext() {
        return request.getServletContext();
    }

    // ... (add if necessary more methods here which return/delegate the request/response).    
}

Implement javax.servlet.Filter which does the following in doFilter() method and is mapped on an url-pattern of interest, eg /* or on the servlet-name of your front controller servlet.

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

    try (YourContext context = YourContext.create(request, response)) {
        chain.doFilter(request, response);
    }
}

Note the importance of try-with-resources statement. It guarantees that the YourContext#close() will be called after the filter has done its job and the ThreadLocal resource will be cleared. Otherwise the thread will still contain it when recycled for another HTTP request.

And here's how you could use it in the POJO:

YourContext context = YourContext.getCurrentInstance();
HttpSession session = context.getSession();

This all is basically also how the Context objects of the average MVC framework works, like JSF's FacesContext and the one in Wicket.

Said that, have you looked at CDI? Perhaps it's easier to make the artifacts CDI-managed so you can just @Inject them in each other.


A Pojo is a Plain Old Java Object. POJOS have nothing to do with sessions.

The https session is available on the request object.

Check out this

http://download.oracle.com/docs/cd/E17802_01/webservices/webservices/docs/1.6/api/index.html

specifically the HttpServletRequest interface, and the getSession() method.

For the 'bean' part of your question. A bean is a java class that conforms to 3 standards.

  • No arg constructor
  • setters and getters to access private fields
  • Implements serializable.
  • A POJO is a bean if it follows those conventions.


    Assuming that you are referring to Servlet programming ....

    There's no direct way to get from a POJO to the Session. You need to get the Session from the HttpServletRequest object.

    There are 2 popular solutions that I've seen for dealing with this.

    First option is to create a context object of some sort that contains the Session. This context is then passed along down into your business layer so that your POJOs can get this information if they need it.

    Second option is to leverage ThreadLocal storage. Often the session is placed on ThreadLocal storage by a filter or interceptor. Then any object in your system can fetch it from the thread. This pattern shows up in a lot of the web frameworks like Spring and Struts.

    链接地址: http://www.djcxy.com/p/76538.html

    上一篇: 请求MVC和组件MVC之间的区别

    下一篇: 在容器外的POJO中检索servlet上下文,会话和请求