Link to home
Start Free TrialLog in
Avatar of chinsw
chinsw

asked on

how to keep session values using struts in jsp

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.
Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

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.
Avatar of amitkathpal123
amitkathpal123

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.
Avatar of chinsw

ASKER

***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.




Avatar of chinsw

ASKER

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.
Avatar of chinsw

ASKER

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...
@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.

Avatar of chinsw

ASKER

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?
Avatar of chinsw

ASKER

HI Vivekanandar, it displays null..but in MsgHandler, it displays the time value.
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.
Avatar of chinsw

ASKER

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.
Avatar of chinsw

ASKER

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.
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 ;
Avatar of chinsw

ASKER

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?
Avatar of chinsw

ASKER

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..
try onething rename it and try ....
Avatar of chinsw

ASKER

dravidnsr,

what should i rename?
Avatar of chinsw

ASKER

no one can find out the problem?
@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
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>

Avatar of chinsw

ASKER

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...
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 ??
Avatar of chinsw

ASKER

dravidnsr,

yes, i have tried to rename the name, but still same...:(
session["CurrTime"].ToString();
Try this bcz u have get the session value throw java script like this only
Avatar of chinsw

ASKER

dravidnsr,

i hit error from that code..
Avatar of chinsw

ASKER

any problems with my session code?
Avatar of chinsw

ASKER

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
@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.

Avatar of chinsw

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of chinsw

ASKER

gurvinder372,

that's great...it works now...thank you very much...