Results 1 to 15 of 15
  1. #1
    j_kathiresan is offline Member
    Join Date
    Feb 2008
    Posts
    22
    Rep Power
    0

    Default Problem in accessing session variables

    Need to access Http Session values in all the layers....

    Thread local variable is explained in the link http://veerasundar.com/blog/2010/11/...d-code-sample/

    Java Code:
    Planning to do the following
    
    1. write an interceptor which extends HandlerInterceptorAdapter 
    2. input the session variable in a thread local variable per request 
    3. delete the thread local variable in afterConcurrentHandlingStarted and afterCompletion method 
    
    import org.slf4j.MDC; 
    import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; 
    
    import java.util.HashSet; 
    import java.util.Set; 
    
    import javax.servlet.http.HttpServletRequest; 
    import javax.servlet.http.HttpServletResponse; 
    
    public class SessionHandlerInterceptor extends HandlerInterceptorAdapter { 
       /** 
        * set of keys added to MDC so can be removed 
        */ 
       private ThreadLocal<Set<String>> storedKeys = ThreadLocal.withInitial(() -> new HashSet<>()); 
    
       @Override 
       public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { 
          addKey("sessionId", request.getHeader("X-Session-Id")); 
          addKey("userId", resolveUser(request)); 
          return true; 
       } 
    
    
    private void addKey(String key, String value) { 
          storedKeys.get().add(key); 
       } 
    
       @Override 
       public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { 
          // request ended on current thread remove properties 
          // clear the variables - key and values 
    removeKeys(); 
       } 
    
       @Override 
       public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) 
               throws Exception { 
         // clear the variables - key and values 
    removeKeys(); 
       } 
    
    private void removeKeys() { 
          storedKeys.remove(); 
       } 
       
    } 
    
    
    
    import org.springframework.context.annotation.Bean; 
    import org.springframework.context.annotation.Configuration; 
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry; 
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 
    
    @Configuration 
    public class BeanConfiguration { 
    
       @Bean 
       public SessionHandlerInterceptor sessionHandlerInterceptor() { 
          return new SessionHandlerInterceptor(); 
       } 
    
       @Bean 
       public WebMvcConfigurerAdapter webConfigurer() { 
          return new WebMvcConfigurerAdapter() { 
             @Override 
             public void addInterceptors(InterceptorRegistry registry) { 
                registry.addInterceptor(sessionHandlerInterceptor()); 
             } 
          }; 
       } 
    }


    Please advise whether the above is correct to proceed.
    Last edited by j_kathiresan; 02-08-2018 at 05:53 PM. Reason: code is missed

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,960
    Rep Power
    31

    Default Re: Problem in accessing session variables

    Does the code compile without errors?

    What do you want the code to do now?

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    j_kathiresan is offline Member
    Join Date
    Feb 2008
    Posts
    22
    Rep Power
    0

    Default Re: Problem in accessing session variables

    Norm, i have fixed the alignment and please check

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,960
    Rep Power
    31

    Default Re: Problem in accessing session variables

    Thanks, that looks better.
    please check
    What problem or questions do you have?

    Does the code compile ok? If not copy and paste the full text of the error messages.
    Does the code execute as desired? If not explain what the problem is.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    j_kathiresan is offline Member
    Join Date
    Feb 2008
    Posts
    22
    Rep Power
    0

    Default Re: Problem in accessing session variables

    wanted to know whether the approach is fine???

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,960
    Rep Power
    31

    Default Re: Problem in accessing session variables

    If this is a question about org.springframework then I'll move it to the section of the forum for spring.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    j_kathiresan is offline Member
    Join Date
    Feb 2008
    Posts
    22
    Rep Power
    0

    Default Re: Problem in accessing session variables

    Please kindly move it spring forum

  8. #8
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: Problem in accessing session variables

    I am usually registering the Interceptor bean in an xml dispatcher servlet with <mvc:interceptors>, simply because it seems easier. You can also register it in a Java config file as you did, try this link.

  9. #9
    j_kathiresan is offline Member
    Join Date
    Feb 2008
    Posts
    22
    Rep Power
    0

    Default Re: Problem in accessing session variables

    Can we access the interceptor in any service classes ??? Please help me with an example.

  10. #10
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: Problem in accessing session variables

    'Not sure what you are trying to do here. The interceptor is for the http session what an Aspect is for Java classes. Before the controller starts processing your request, the preHandle() method is run (analogue the other methods). Yes you can access that class with all methods that Java provides (DI). IF you give us a little more info we might be able to help you.

  11. #11
    j_kathiresan is offline Member
    Join Date
    Feb 2008
    Posts
    22
    Rep Power
    0

    Default Re: Problem in accessing session variables

    So do you say the first code which is written can be used to inject variables to thread local context and access in all layers..

    I have pasted the code in the first posting...Basically likes to inject session variables and access in any layer whever i need to retrieve the session variables.

  12. #12
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: Problem in accessing session variables

    I think you could do it that way, but it will be a nightmare to maintain that code. There is a way to store session attributes easier with the @SessionAttributes annotation.

    Concerning your other post: Java and JavaScript are two different things - just sayin'.

  13. #13
    j_kathiresan is offline Member
    Join Date
    Feb 2008
    Posts
    22
    Rep Power
    0

    Default Re: Problem in accessing session variables

    But the annotation can be used in the controller layer or in all layers ??? How to get the values of the session variables ??? Need a sample code snippet

  14. #14
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: Problem in accessing session variables

    If you follow the MVC architecture it will give you a predetermined answer. Again, think about the poor folks that have to perform maintenance in your code later on.

    Quote Originally Posted by j_kathiresan View Post
    Need a sample code snippet
    Yeah, we all need something. Any search engine should be your friend.

  15. #15
    j_kathiresan is offline Member
    Join Date
    Feb 2008
    Posts
    22
    Rep Power
    0

    Default Re: Problem in accessing session variables

    I searched it and hard to find it...can u help me please?

Similar Threads

  1. Replies: 13
    Last Post: 12-30-2011, 04:38 AM
  2. Problem accessing variables in other objects
    By falconfetus8 in forum New To Java
    Replies: 10
    Last Post: 11-15-2011, 03:08 AM
  3. Accessing session variable from Java Class
    By Rohit Pokhrel in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 12-08-2010, 05:07 PM
  4. accessing variables
    By emp in forum New To Java
    Replies: 3
    Last Post: 04-23-2009, 05:36 AM
  5. JSP - session variables
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-02-2007, 10:22 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •