It's not a secret anymore!*
The moose likes Struts and the fly likes how to use Session in Struts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "how to use Session in Struts" Watch "how to use Session in Struts" New topic
Author

how to use Session in Struts

samir ware
Ranch Hand

Joined: Jul 27, 2005
Posts: 192
Hi all ....
I am novice to struts....I want to know how to use Session in the Struts...
There is a tag in the "action" element which says "scope=". Is there anything to do with this in the session management.
Plese explain me with the following example. Suppose my first screen is accepting the username and the password. I want to use the username entered at this screen on the ,say 15th screen.So how to do this.
If I am suppose to use the normal approch like "session.setAttreibute(key,value) and session.getAttribute(key) then what is the use of the "scope= " field in the action element.
Thanks in advance
murali kankanala
Ranch Hand

Joined: Nov 15, 2004
Posts: 110
hi Samir,

that "scope=" attribute is used to store the form bean after creation.
you can set the form bean in session or request , ......
samir ware
Ranch Hand

Joined: Jul 27, 2005
Posts: 192
hi Murli,
thanks for the reaply....but need some more clarification.Do u mean to say that the form bean which is in the session scope can be accessed through out the application.
Do I need to say session.setAttribute("Form Bean Name) and can access that perticular form bean as session.getAttribute("Form Bean Name).
and what if I need to store only a single value in the session such as username. Do I need to follow the same procedure like setting the attribute in the session and getting it afterwords or is there any other alternative way out for the same in struts

Thanks in advance
Rishi Chohan
Greenhorn

Joined: Jul 06, 2002
Posts: 4
Hi,

I have a simmilar question related to 'Session Management' using struts.

How can we use Struts framework to maintain session without using the Session object?

Is there any specific way other than maintaining data in ActionForm?

Thanks in advance,
Rishi
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Since the HttpServletRequest object is passed as a parameter to the execute() method in a Struts action, you can always retrieve the HttpSession object by using the request.getSession() method. Once you have this object, you are free to do anything you want with it, particuarly store objects using setAttribute, or retrieve them using getAttribute. Any objects you put in the session are also available to the JSPs just as in any other Java EE application.


Merrill
Consultant, Sima Solutions
Rishi Chohan
Greenhorn

Joined: Jul 06, 2002
Posts: 4
Hi Merrill,

Thanks for your reply.
Is there any other way through which we can achieve Session Management mechanism? Let me clarify this bit further.
Does struts framework provide any mechanism to maintain session through some configuration and not by HttpSession object?
Can we maintain state of clients instance variable using any of the Struts Framework component?

Thanks in advance,
Rishi
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
I'm afraid there's nothing special in Struts to maintain state. You have only the same mechanisms that are available in any J2EE application:
  • Using the HttpSession object
  • Passing data from one page to the next using the HttpServletRequest object and hidden fields on the page
  • Saving and retrieving data from a relational database
  • Storing and retrieving cookies on the client system

  • Srikanth Kondeti
    Greenhorn

    Joined: Oct 14, 2005
    Posts: 2
    I too have the same issue here with Struts.

    If I want to give the scope as session where should I give?? Like inside the JSP form scope or insise the Struts-config.xml Action mappings??
    Pls.Clarify on this.

    I need to maintain username and password in session scope with Struts framework.

    Srikanth
    Kishore Sahu
    Ranch Hand

    Joined: Mar 10, 2009
    Posts: 31
    Everybody is saying in a same fashion.
    But I also need more clarification, like "how do I validate and invalidate a session object?", "how do I check a session is valid or not in every page before displaying that page?"
    If session is valid, continue with normal processing and display the current page, otherwise redirect to some Logout kind of Page.

    Please help me.


    Kishore
    sudheer kiran
    Ranch Hand

    Joined: Jun 26, 2008
    Posts: 237

    how about using
    together.

    both have scope property.
    if username is present in the session attributues validate.


    Sudheer
    SCWCD, SCJP 5
    Kishore Sahu
    Ranch Hand

    Joined: Mar 10, 2009
    Posts: 31
    Sudheer, Did you reply me or somebody else ???
    I want to know, how we can create a session, validate before each page and pass or fail accordingly ???
    David Newton
    Author
    Rancher

    Joined: Sep 29, 2008
    Posts: 12617

    Unless your JSP pages specifically turn off session creation you'll *always* have a session.

    If you're trying to implement something like a login then if login succeeds put the user object (or whatever) into session. If it fails, don't. You can then check for the presence of that object in session to see if there's a logged-in user.
    Kishore Sahu
    Ranch Hand

    Joined: Mar 10, 2009
    Posts: 31
    I have a doubt.
    Whenevr a user logged in, it creates a session and stores the username in session variable. In the mean time, if session expires, then how do I get the username again?
    and how do I update the databases status for that particular user ?
    David Newton
    Author
    Rancher

    Joined: Sep 29, 2008
    Posts: 12617

    Once the session expires the data in the session is gone.
    Dawn Charangat
    Ranch Hand

    Joined: Apr 26, 2007
    Posts: 249
    The "session=" parameter is something which you can add to your action class mapping in the struts-config.xml file. By default, the value for this is "request"
    which doesnt mean that you woun't have access to the session variable, but just the fact that the formbean object will be stored in the request object during transactions.

    And for the HttpSession object, since struts is just another web framework, it doesnt have any new thing to add to the already existing J2EE specs, but just makes it a bit more
    modular and easily accessible. So you need to get the session from the request obect [request.getSession()] and do the manipulation on that.

    Hope that helps.
    Kishore Sahu
    Ranch Hand

    Joined: Mar 10, 2009
    Posts: 31
    But after session expires, request.getSession() will give a new session, right .
    So the variable whatever I had set, that won't be there in the new session.

    Please help me, give me a better solution.
    sudheer kiran
    Ranch Hand

    Joined: Jun 26, 2008
    Posts: 237

    hey i didnt get your question.
    you are not the user ,you are the developer.
    why you do wait till session expires.
    Lei Guoguo
    Greenhorn

    Joined: Mar 31, 2009
    Posts: 26
    hi, the scope is like a collection, that have four configuration, their is : page, request, session , application.

    page mean: the variable who is in pageScope, it will destroy while the page closed;
    requst mean: the variable who is in requestScope, it will destroy while the request interrupt;
    session mean: the variable who is in sessionScope, it will desttoy while the browser closed;
    application mean: the variable who is in applicationScope, it will destroy while the application server shutdown;

    in struts-config.xml file , the action's config attribute "scope=request" is mean : the variable what in corresponding ActionForm definition will set in the requestScope, In other words, the ActionForm's fields will set in the requestScope.

    you can used in the page for example : <cut value="${requestScope.fieldName }" />

    I'm sorry, my english is so bad, do you understand? my e-mail is :lgg860911@yahoo.com.cn, if you have any other issue, you can mail to me.


    I'm a chinese, my english is so bad, I need help. If you want to help me, you can send your question to my e-mail: lgg860911@yahoo.com.cn, I will help you, and improve my english. Thanks everyone.
    surendra babu k
    Greenhorn

    Joined: Oct 21, 2011
    Posts: 11
    Hi every one....

    The best way to maintain session in struts application is create a session obj in ActionServlet....
    bcz ActionServlet is responsible for taking request and providing response.....
    so creating a session is ActionServlet is the best way...

    any questions..........

    Thanks to all.............
    sneha ulhe
    Ranch Hand

    Joined: May 15, 2012
    Posts: 45

    Hi,session is bydefault available in jsp pages.we can create session it in Action class.struts also provides the SessionAware interface.


    Sneha
    [OCJP 6]
    surendra babu k
    Greenhorn

    Joined: Oct 21, 2011
    Posts: 11
    Hi, We can have a multiple action classes in a project....So it not recommended that create a session a in Action classes, bez the no.of session objects also will increase..this will cause low performance....
    Sandy Chatterjee
    Ranch Hand

    Joined: Jun 27, 2012
    Posts: 55

    The best way to maintain session in struts application is create a session obj in ActionServlet....
    bcz ActionServlet is responsible for taking request and providing response.....
    so creating a session is ActionServlet is the best way

    A Session object is already created. We don't have to create it.
    In the Action class we retrieve it by using
    request.getSession() from the HttpRequest object. I guess this is what sneha meant.
    By the way could you please clarify how do you create session object in ActionServlet.
    As we only create it when one is not present by using the following:


    Take up one idea. Make that one idea your life; dream of it; think of it; live on that idea. Let the brain, the body, muscles, nerves, every part of your body be full of that idea, and just leave every other idea alone. This is the way to success.
    Dipali yadav
    Greenhorn

    Joined: May 21, 2012
    Posts: 22
    sneha ulhe wrote:Hi,session is bydefault available in jsp pages.we can create session it in Action class.struts also provides the SessionAware interface.


    Can you please tell me how to use SessionAware? As i m using it i m getting an error that :-
    packake not fount.........package org.apache.struts2 does not exist [javac] import org.apache.struts2.ServletActionContext;
    and symbol not found........cannot find symbol [javac] symbol: class SessionAware
    Yogesh Lonkar
    Ranch Hand

    Joined: Jul 17, 2012
    Posts: 94

    Dipali yadav wrote:
    sneha ulhe wrote:Hi,session is bydefault available in jsp pages.we can create session it in Action class.struts also provides the SessionAware interface.


    Can you please tell me how to use SessionAware? As i m using it i m getting an error that :-
    packake not fount.........package org.apache.struts2 does not exist [javac] import org.apache.struts2.ServletActionContext;
    and symbol not found........cannot find symbol [javac] symbol: class SessionAware


    To use Session-aware interface you need to have struts2-core-2.0.6.jar download here or higher version of struts-core in your WEB-INF/lib folder


    Learning some thing New Every Day
     
    Consider Paul's rocket mass heater.
     
    subject: how to use Session in Struts