Announcement Announcement Module
Collapse
No announcement yet.
Create a session Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Create a session

    Hi,
    i have a simple web application using Spring mvc and spring framework and everything is working just fine. I would like in the controller of the first page (login page), when a user successfully logs in to create a session in the onSubmit method, and pass a parameter (the user id) in that session. All of the following pages should be able to get that parameter user id to use it. How can i create that session?
    Thank you

  • #2
    vlaxos,

    Have you checked out org.springframework.web.util.WebUtils?

    Code:
    WebUtils.setSessionAttribute(request, "recentOrders", recentOrders);
    I've always had luck with it, pretty simple to use and has a nice feature:

    Code:
    String userid = (String) WebUtils.getRequiredSessionAttribute(request, "userid");
    Which throws an IllegalStateException exception if it's not found, otherwise, it gets populated with the session parameter.

    Hope that helps,

    Comment


    • #3
      Hey thnx for the reply,
      i tried that but still does not work for me
      I created a session in the controller of my first jsp page (login page)

      HttpSession session = request.getSession(true);
      session.setAttribute("userId");

      My problem now is how can i access this specific session from other SimpleFormControllers in my web app?
      (I tried through the request of the onSubmit function but is getting a different session)

      Any help is appreciated

      Comment


      • #4
        vlaxos713,

        The methods I gave you should work; I've used them many many times. Basically what you're saying is that two sessions are being created, I'd check to see if/where you're invalidating the first session. If you're not, then you've probably got container problems and should also ask around your container's forums/mailing-lists etc.

        However, I'll assume that your code
        Code:
        HttpSession session = request.getSession(true);
        session.setAttribute("userId");
        was just a typo as session.setAttribute takes two parameters.

        Hope that helps,

        Comment


        • #5
          Yes sorry it is a typo the correct code is session.setAttrribute("userId", userId) - name and object.
          In some other forums i posted my web.xml and my app-conf.xml files and everyone says that are correct.
          I assume the reason that my code is not working is the same reason that the code you gave me is not working..

          Comment


          • #6
            Another thing i forgot to mention is that when i use the code you gave me it throws me IllegalStateException if i don't catch it, which means that no parameter is there to get. I m so confused beacuse i set the session just fine. The problem definitely is two different sessions

            Comment


            • #7
              vlaxos713,

              If I were you, I'd make a quick HttpSessionListener and HttpSessionAttributeListener to monitor exactly when the sessions are created and removed as well as the attributes.

              Something like the following (use your preferred logging or System.out.println if so desired):

              Code:
              public class MySessionListener implements HttpSessionListener, HttpSessionAttributeListener {
              
                  private static Log log = LogFactory.getLog(MySessionListener.class);
                  
                  public void sessionCreated(HttpSessionEvent event) {
                      log.info("New session created!");
              
                      HttpSession session = event.getSession();
                      log.info("session id: " + session.getId());
                      log.info("the maximum time interval, in seconds, that the servlet" +
                              " container will keep this session open between client accesses: "
                              + session.getMaxInactiveInterval());
                  }
              
                  public void sessionDestroyed(HttpSessionEvent event) {
                      log.info("Session destroyed!");
              
                      HttpSession session = event.getSession();
                      log.info("session id: " + session.getId());
                      log.info("session created: " + new Date(session.getCreationTime()));
                      log.info("session last accessed: " + new Date(session.getLastAccessedTime()));
                      log.info("session max inactive interval: " + session.getMaxInactiveInterval());
                  }
              
                  public void attributeAdded(HttpSessionBindingEvent se) {
                      log.info("New attribute added with name: " + se.getName()
                              + " and value (toString): " + se.getValue());
                  }
              
                  public void attributeRemoved(HttpSessionBindingEvent se) {
                      log.info("Existing attribute removed with name: " + se.getName()
                              + " and value (toString): " + se.getValue());
                  }
              
                  public void attributeReplaced(HttpSessionBindingEvent se) {
                      log.info("Existing attribute replaced with name: " + se.getName()
                              + " and value (toString): " + se.getValue());
                  }
              
              }
              and then in your web.xml

              Code:
                  <listener>
                      <listener-class>MySessionListener</listener-class>
                  </listener>
              Redeploy your app and watch the logs, try to figure out what's causing it. You can use those event classes to get more detailed information about the session if so desired.

              Hope that helps,

              Comment


              • #8
                Thank you for your help.. Both ways are correct, the problem is my pc's configuration somewhere, as i tried the web app in another computer and it works fine!

                Comment

                Working...
                X