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

  • Home
  • SEARCH
  • 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 9017055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:09:59+00:00 2026-06-16T04:09:59+00:00

I used to have an Open Session In Conversation Filter based on cookies for

  • 0

I used to have an Open Session In Conversation Filter based on cookies for a JSF 2 app. Now I want to build the same mechanism but technology-agnostic. Reusing some code, I have written this in a class that extends OncePerRequestFilter:

@Override
protected void doFilterInternal(HttpServletRequest request,
        HttpServletResponse response, FilterChain filterChain)
        throws ServletException, IOException {

    UUID conversationId = lookupConversationOrCreateIfNecessary(request,
            response);

    log.debug("Binding conversation '{}' to request '{}'", conversationId,
            request);
    bindConversation(conversationId, request);

    try {
        filterChain.doFilter(request, response);
    } finally {
        log.debug("Unbinding conversation '{}' from request '{}'",
                conversationId, request);
        unbindConversation(conversationId, request);
    }

}

Now, when I reach bindConversation(conversationId, request) I just add a request attribute which points to the conversationId which is mapped to a Hibernate Session.

Anyways, in JSF I can access the current request by using FacesContext.getCurrentInstance().getExternalContext().getRequest() and implemented a CurrentSessionContext using this. But in plain servlets how can I access the current request programmatically?

Note: I have been reading the OncePerRequestFilter javadocs and I found this:

As of Servlet 3.0, a filter may be invoked as part of a REQUEST or
ASYNC dispatches that occur in separate threads. A filter can be
configured in web.xml whether it should be involved in async
dispatches. However, in some cases servlet containers assume different
default configuration. Therefore sub-classes can override the method
shouldNotFilterAsyncDispatch() to declare statically if they [sic] shouuld
indeed be invoked, once, during both types of dispatches in order to
provide thread initialization, logging, security, and so on. This
mechanism complements and does not replace the need to configure a
filter in web.xml with dispatcher types.

So, would it be dangerous to use a ThreadLocal to achieve what I want?

  • 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-06-16T04:10:01+00:00Added an answer on June 16, 2026 at 4:10 am

    As you mention in your question: using a ThreadLocal seems a good option. I don’t see why it would be unsafe as soon as you use your filter for both REQUEST and ASYNC.

    EDIT

    @Override
    protected void doFilterInternal(HttpServletRequest request,
                                    HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {
    
    
        UUID conversationId = lookupConversationOrCreateIfNecessary(request,
                response);
    
        log.debug("Binding conversation '{}' to request '{}'", conversationId,
                request);
    
        ConversationHolder.setId(conversationId);
    
        bindConversation(conversationId, request);
    
        try {
            filterChain.doFilter(request, response);
        } finally {
            log.debug("Unbinding conversation '{}' from request '{}'",
                    conversationId, request);
            ConversationHolder.clear();
            unbindConversation(conversationId, request);
        }
    
    }
    
    @Override
    protected boolean shouldNotFilter(HttpServletRequest request) throws ServletException {
        return false; //to be sure both REQUEST and ASYNC are filtered
    }
    

    And the ConversationHolder

    public class ConversationHolder extends ThreadLocal<UUID>{
    
        private static ConversationHolder INSTANCE = new ConversationHolder();
    
        public static void setId(UUID conversationId){
              INSTANCE.set(conversationId);
        }
    
        public static UUID getId(){
            return INSTANCE.get();
        }
    
        public static void clear(){
            INSTANCE.remove();
        }
    
    }
    

    Since conversationId is a local variable it won’t be shared between request.

    Since ConversationHolder is a ThreadLocal, the value you get from it during doFilter(…) will be correct. (except if you create new Thread by hand during your request processing, but it is not a recommended design)

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

Sidebar

Related Questions

I have used two GUIDs to open the folders My Computer and My Documents
Well I have created a web application using JSF 2.0. Everywhere I have used
Hey there, I have my own Membership-Provider where I open a NHibernate-Session in the
i have one asp.net project. In this i used session for logged user. If
i have used FPDF for creating pdf in PHP. I used session variable to
I have a JSF application that makes much use of session-scoped variables. A new
I have used the silverlight control in CRM 2011.It also published on form but
We have used Shibboleth to authenticate users. It works great. The issue is that
I have used some jquery components in my web site, Suddenly i'm getting an
I have used a plugin that uses prototype js, it's working fine but the

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.