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 7519841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:58:07+00:00 2026-05-30T01:58:07+00:00

I really got tired of this problem and can’t find a best practice for

  • 0

I really got tired of this problem and can’t find a best practice for doing it. Let me clear the problem with an example:

I have a static method like :

public class AppError {
   public static void systemError(string errorMsg) { //Like "Can't connect to DB"
      HttpServletRequest request = ?????????? "HOW TO ACCESS?";
      request.setAttribute("Error", errorMsg);
      request.getRequestDispatcher("/error.jsp").forward(request, response(????????));
   }
}

As you can see I need to access request and response objects and I DON’T WANT to pass HttpServletRequest/Response by parameters because that means passing it where AppError.systemError called, and that is so many places: reading/writing files, cookie and user operations, database operations etc…

I thought I found a solution, ThreadLocal, I used it in a Filter and saved request and response objects but then Filter.Destroy() method where I free ThreadLocal variables by ThreadLocal.remove() method is not invoked when I tested it in Debug mode. That means the thread may never be dealloced.

The ServletContextListerner is NOT A SOLUTION too because I couldn’t find a way to access request and response objects within the ServletContext object. I expected a method like ServletContext.getRequest() but there’s none.

I’m also using JSF, but the problem with the JSF is FacesContext is not accessible in custom classess too. Also JSF is being loaded later than listeners, so I can’t use my error system in the listeners if I used FacesContext to get request and response objects.

So what is the best practice here? I mean this is a clear problem and I can’t move on without solving this.

Btw, the error method is an example, there are other places that I need to access request and response objects.

  • 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-30T01:58:07+00:00Added an answer on May 30, 2026 at 1:58 am

    In principal there’s no reason why a correctly setup Filter would not work in combination with thread local storage (TLS).

    The key insight is not to clear the TLS in the Filter#Destroy() method, but in a finally clause after the main filtering chain:

    @WebFilter(filterName="requestTLSFilter")
    public class RequestTLSFilter extends Filter {
    
        private static final ThreadLocal<ServletRequest> THREAD_LOCAL = new ThreadLocal<ServletRequest>();
    
        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
            try {
                THREAD_LOCAL.put(request);
                chain.doFilter(request, response);
            } finally {
                THREAD_LOCAL.remove();
            }
        }
    
        // Other methods    
    }
    

    With respect to JSF, FacesContext.getCurrentInstance() is available everywhere. JSF has no implicit knowledge of who’s calling that, BUT it’s only available to code in the call chain after the Facelets servlet is invoked.

    There’s one other alternative. If your container has Java Authorization Contract for Containers (JACC) support, then the following gets you the current request as well:

    HttpServletRequest request = (HttpServletRequest) PolicyContext.getContext("javax.servlet.http.HttpServletRequest");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Got this really stupid problem: Can find the proper name for the android folder
ive got a really weird problem. i have no clue why its not working.
I started working with linq to SQL several weeks ago. I got really tired
After reading this article on thedailywtf.com, I'm not sure that I really got the
Our team and project got really big and we can no longer sustain it.
I've got this really simple piece of code that I thought was the correct
I have tried to ask a variant of this question before. I got some
I've got the following problem and I can't seem to get why it's not
I've got a very frustrating sizer problem. I have two wxFlexGridSizer s (and a
I got a serious problem here: I have a scrolling background that is 1320

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.