Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3791964
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T12:32:59+00:00 2026-05-19T12:32:59+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T12:32:59+00:00Added an answer on May 19, 2026 at 12:32 pm

    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, e.g. /* 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to retrieve the server's computer name the database's name from
Is there any way to retrieve a page's javascript variables from a Google Chrome
Is there any way to retrieve a user's email address after authenticating with Yesod's
Is there any way to retrieve the names of the stored procedures by some
Is there any way to retrieve the address map file for ntdll.dll? I know
Is there any way to retrieve the parent layout of a widget in Qt?
Is there any way to retrieve all friends in a particular network? For example,
Is there any way to retrieve information about how many extra displays there are
Is there any way to use Google's API to retrieve a user's current zipcode
I wonder if there is any way to retrieve, for both a server and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.