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.getRe quest();
request.getSession();
session.setAttribute("name ", name);
and in msgHandler.jsp :
<%=(String)session.getAttr ibute("nam e")%>
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.
I'm using this code in MsgHandler java:
HttpServletRequest request = ServletActionContext.getRe
request.getSession();
session.setAttribute("name
and in msgHandler.jsp :
<%=(String)session.getAttr
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.
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.
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.
ASKER
***struts.xml***:
<action name="MsgHandler" class="my.com.eprotea.acs. redirect.M sgHandler" >
<result name="success" type="dispatcher">MsgHandl er.jsp</re sult>
</action>
<action name="Payment" >
<result>/authentication/Pa yment.jsp< /result>
</action>
========================== ========== ========== =======
***MsgHandler.java***:
public class MsgHandler extends ActionSupport{
HttpServletRequest request;
HttpSession session;
public String execute() throws Exception {
request = ServletActionContext.getRe quest();
session = request.getSession();
session.setAttribute("Curr Time", getTime());
session.setAttribute("show Page", "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.getAtt ribute("sh owPage")%> ";
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.getAttr ibute("Cur rTime")%>;
function counter(){
var diff = (<%=new java.util.Date().getTime() %> - start) / 1000;
if(diff >= <%=(Integer)session.getAtt ribute("Ti meout")%>) {
timeOut();
}else {
setTimeout('counter()',100 0);
}
}
</script>
<body onload="counter()">
-----------------
</body>
</html>
========================== ========== =====
and the problem is in Payment.jsp the "start" variable is null.
<action name="MsgHandler" class="my.com.eprotea.acs.
<result name="success" type="dispatcher">MsgHandl
</action>
<action name="Payment" >
<result>/authentication/Pa
</action>
==========================
***MsgHandler.java***:
public class MsgHandler extends ActionSupport{
HttpServletRequest request;
HttpSession session;
public String execute() throws Exception {
request = ServletActionContext.getRe
session = request.getSession();
session.setAttribute("Curr
session.setAttribute("show
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.getAtt
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.getAttr
function counter(){
var diff = (<%=new java.util.Date().getTime()
if(diff >= <%=(Integer)session.getAtt
timeOut();
}else {
setTimeout('counter()',100
}
}
</script>
<body onload="counter()">
-----------------
</body>
</html>
==========================
and the problem is in Payment.jsp the "start" variable is null.
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.
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.
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?
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.getAttribu te("CurrTi me") );
and test what value it is displaying.
put this code before to the javascript, in the payment.jsp
System.out.println( (String)session.getAttribu
and test what value it is displaying.
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?
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.
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.
can you please remove that session= "true" in the start of the payment.jsp page and test.
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.
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,f orward,for m) method?
In that we can get the session through request.getSession();
But here u r using ServletActionContext.getRe quest(),
if it possible can u test like that.
shouldnot u use the normal execute(request,response,f
In that we can get the session through request.getSession();
But here u r using ServletActionContext.getRe
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.getRe quest();
request.getSession();
session.getAttribute("Curr Time") ;
so access define and access ;
so try to access after define this line
HttpServletRequest request = ServletActionContext.getRe
request.getSession();
session.getAttribute("Curr
so access define and access ;
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?
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?
ASKER
dravidnsr,
i've tried that one also, but i checked the session value is different with first one and the session.getAttribute("Curr Time") ; return null also..
i've tried that one also, but i checked the session value is different with first one and the session.getAttribute("Curr
try onething rename it and try ....
ASKER
dravidnsr,
what should i rename?
what should i rename?
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
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">MsgHandl er.jsp</re sult>
remove the type="dispatcher" from the following tag in the struts.config and restart the application it will work.
<result name="success" type="dispatcher">MsgHandl
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...
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 ??
do one more thing jsut print ur values in java and another jsp its getting value there ??
ASKER
dravidnsr,
yes, i have tried to rename the name, but still same...:(
yes, i have tried to rename the name, but still same...:(
session["CurrTime"].ToStri ng();
Try this bcz u have get the session value throw java script like this only
Try this bcz u have get the session value throw java script like this only
ASKER
dravidnsr,
i hit error from that code..
i hit error from that code..
ASKER
any problems with my session code?
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.
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
gurvinder372,
that's great...it works now...thank you very much...
that's great...it works now...thank you very much...
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.