We help IT Professionals succeed at work.

how to keep session values using struts in jsp

chinsw
chinsw asked
on
884 Views
Last Modified: 2013-11-24
for e.g. I store some values in session when i call first action class (e.g. MsgHandler.action), and in the JSP result page (e.g. msgHandler.jsp), i still can get the session values. but from this msgHandler.jsp, I redirect to another action class (e.g. payment.action), I can't get the session values anymore in its JSP result page (e.g. payment.jsp).

I'm using this code in MsgHandler java:
HttpServletRequest request = ServletActionContext.getRequest();
request.getSession();
session.setAttribute("name", name);

and in msgHandler.jsp :
<%=(String)session.getAttribute("name")%>

but,in payment.jsp even i'm using same code, i can't get the value back,it returns null.

is it a correct way to do this? thanks.
Comment
Watch Question

CERTIFIED EXPERT

Commented:
From description given in the question, it is difficult to make out the reason why the values are lost in the same session, unless removed explicitly.

You can refer the following links
http://www.theserverside.com/discussions/thread.tss?thread_id=39146
http://www.coderanch.com/t/57308/Struts/access-session-variable-jsp

If this doesn't help you, please post your JSP and Action class source code.
The given code is fine. Check the code you have not pasted, if you are clearing the session somewhere.
n you can also try getting session object from 'request' object, rather than directly using session object. Although it does not make any difference but still you can try.

<% HttpSession session = request.getSession() %>

***if you can paste your complete code, so that we can debug.

Author

Commented:
***struts.xml***:
<action name="MsgHandler" class="my.com.eprotea.acs.redirect.MsgHandler" >
    <result name="success" type="dispatcher">MsgHandler.jsp</result>
</action>
<action name="Payment" >
    <result>/authentication/Payment.jsp</result>
</action>
=====================================================
***MsgHandler.java***:
public class MsgHandler extends ActionSupport{
HttpServletRequest request;
HttpSession session;

public String execute() throws Exception {
         request = ServletActionContext.getRequest();
         session = request.getSession();

         session.setAttribute("CurrTime", getTime());
         session.setAttribute("showPage", "Payment.action");

         return SUCCESS;
}
public static final long getTime(){
        return new java.util.Date().getTime();
    }
}
====================================================
***MsgHandler.jsp***

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>

    <script language="javascript">
        function redirect()
        {
            var f = document.mainFrm;

            f.method = "post";
            f.action = "<%=(String)session.getAttribute("showPage")%>";
            f.target = "_self";
            f.submit();
        }

    </script>
       
    </head>
    <body onload="redirect();">
        <form name="mainFrm">
           
        </form>
    </body>
</html>
===========================================================
***Payment.jsp***

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page session="true" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<html>

<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<script language="Javascript">

var start = <%=(String)session.getAttribute("CurrTime")%>;

function counter(){

    var diff = (<%=new java.util.Date().getTime()%> - start) / 1000;

    if(diff >= <%=(Integer)session.getAttribute("Timeout")%>) {
        timeOut();
    }else {
        setTimeout('counter()',1000);
    }

}
</script>
<body onload="counter()">
-----------------
</body>
</html>

=========================================

and the problem is in Payment.jsp the "start" variable is null.




Author

Commented:
can someone find out the problem? thx
ok.. check if following thing works for you...
first check whether you are getting value in "msgHandler.jsp" for property "currTime".
If yes, then set again this property and then check in Payment.jsp page.

Author

Commented:
Hi amitkathpal123, in msgHandler.jsp i able to get the value,and i try to set back the value into session. But, when come to the Payment.jsp, it returns null again. sigh....I don't know what is the problem...
CERTIFIED EXPERT

Commented:
@chinsw: I am not able to make out from your code that how did you navigate to Payment.jsp from msgHandler.jsp.
Is there any link/action for that which is missing from the code that you have posted?
It looks like that you MsgHandler.jsp and Payment.jsp are not in accessed in the same session, or you have removed the attribute in between.
Are you able to retrieve other session variables in Payment.jsp?
chinsw,  can you please

put this code before to the javascript, in the payment.jsp

System.out.println( (String)session.getAttribute("CurrTime") );
and test what value it is displaying.

Author

Commented:
Hi gurvinder372, in my struts.xml, i have pointed to Payment.action. i try to get other session var in Payment,and it returns a new session value. but i wonder how the value is different, is my way incorrect?

Author

Commented:
HI Vivekanandar, it displays null..but in MsgHandler, it displays the time value.
CERTIFIED EXPERT

Commented:
Please post the code for PaymentAction also.

Also please mention the scope of the request in the action tag in the struts-config.xml.
Also check what is value of this session variable in that PaymentAction class.
chinsw,

can you please remove that session= "true" in the start of the payment.jsp page and test.

Author

Commented:
Hi gurvinder372, i don't have Payment java class. it only has JSP page. and i'm using struts.xml instead of strus-config.xml configuration, that's why i didn't put scope there.

Author

Commented:
HI Vivekanandar, first time when i tried,i didn't put that tag...after i hit problem then i tried to put it...but no different.
chinsw,

shouldnot u use the normal execute(request,response,forward,form) method?

In that we can get the session through request.getSession();

But here u r using ServletActionContext.getRequest(),

if it possible can u test like that.
CERTIFIED EXPERT

Commented:
i think session undefine in  *Payment.jsp

so try to access after define this line
HttpServletRequest request = ServletActionContext.getRequest();
request.getSession();

session.getAttribute("CurrTime") ;
so access define and access ;

Author

Commented:
Vivekanandar,

i really can't change to that method..i've already done many things so far...but, it thought that method is used in Struts 1 instead of Struts 2?

Author

Commented:
dravidnsr,

i've tried that one also, but i checked the session value is different with first one and the session.getAttribute("CurrTime") ; return null also..
CERTIFIED EXPERT

Commented:
try onething rename it and try ....

Author

Commented:
dravidnsr,

what should i rename?

Author

Commented:
no one can find out the problem?
CERTIFIED EXPERT

Commented:
@chinsw: Please try this
Re-create the application from scratch using struts2.
See the following link as a reference

http://struts.apache.org/2.0.14/docs/how-do-we-get-access-to-the-session.html
http://www.roseindia.net/struts/struts2/strutsresources/access-session.shtml
http://www.roseindia.net/struts/struts2/index.shtml

You can use this source code also for understanding the difference/error
CERTIFIED EXPERT

Commented:
currTime only !!
Chinsw,

remove the type="dispatcher" from the following tag in the struts.config  and restart the application it will work.

 <result name="success" type="dispatcher">MsgHandler.jsp</result>

Author

Commented:
Vivekanandar,

thanks for your suggestion,but it didn't work also..it still returned null.   but i thought if we don't specified the type,the default one is "dispatcher" also...i also tried to use "chain" and "redirect",but they're also same...
CERTIFIED EXPERT

Commented:
Have u check that rename sometime that will work !!

do one more thing jsut print ur values in java and another jsp its getting value there ??

Author

Commented:
dravidnsr,

yes, i have tried to rename the name, but still same...:(
CERTIFIED EXPERT

Commented:
session["CurrTime"].ToString();
Try this bcz u have get the session value throw java script like this only

Author

Commented:
dravidnsr,

i hit error from that code..

Author

Commented:
any problems with my session code?

Author

Commented:
i found out the problem, actually the process is from merchant's website will call my page,but my page is inline in the merchant's site as a frame. but,when i try to set it not inline and make it standalone in my site,i'm able to get my session values. is it the problem because of this? have another way to solve this problem? thanks
CERTIFIED EXPERT

Commented:
@chinsw: The problem could be because of this.
But I am surprised to know that this important info was not shared!

Following links are examples of people facing similar issues
http://modxcms.com/forums/index.php?topic=41607.0
http://www.webmasterworld.com/forum21/10828.htm
http://codingforums.com/showthread.php?t=152567

Above mentioned links should be helpful to you.

Author

Commented:
thanks for the given links, but i still didn't find out what is the solution for this problem..is there any solution or sample to solve this? thanks
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
gurvinder372,

that's great...it works now...thank you very much...
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.