• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to use Session in Struts

 
samir ware
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rishi Chohan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 31
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    sudheer kiran
    Ranch Hand
    Posts: 237
    Firefox Browser MyEclipse IDE Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    how about using
    together.

    both have scope property.
    if username is present in the session attributues validate.
     
    Kishore Sahu
    Ranch Hand
    Posts: 31
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 12617
    IntelliJ IDE Ruby
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 31
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 12617
    IntelliJ IDE Ruby
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Once the session expires the data in the session is gone.
     
    Dawn Charangat
    Ranch Hand
    Posts: 249
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 31
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 237
    Firefox Browser MyEclipse IDE Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 26
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    surendra babu k
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 52
    Eclipse IDE Java MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi,session is bydefault available in jsp pages.we can create session it in Action class.struts also provides the SessionAware interface.
     
    surendra babu k
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 55
    Hibernate Java Mac
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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:
     
    Dipali yadav
    Greenhorn
    Posts: 22
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 94
    Eclipse IDE Hibernate Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic