jaggybala2
asked on
scope of 'Request'
What is the scope of request.setAttribute.
I want to define request.setAttribute in my servlet and retieve in my JSP as request.getAttribute.
i.e.:
I have
request.setAttribute("regS ubmit", "reg_submit");
in my servlet, and
i have
if ( request.getAttribute("regS ubmit") != null )
in my JSP. But, it always shows exceptions in my servlet. ( I can work with sessions though ).
Any ideas. Thanks.
I want to define request.setAttribute in my servlet and retieve in my JSP as request.getAttribute.
i.e.:
I have
request.setAttribute("regS
in my servlet, and
i have
if ( request.getAttribute("regS
in my JSP. But, it always shows exceptions in my servlet. ( I can work with sessions though ).
Any ideas. Thanks.
Is this line
> if ( request.getAttribute("regS ubmit") != null )
throwing the exception?
> if ( request.getAttribute("regS
throwing the exception?
ASKER
No..i am getting exception in the servlet .. not in my JSP.
If i have:
HttpSession session = request.getSession();
session.setAttribute("regS ubmit", "reg_submit");
in my servlet, and
if ( session.getAttribute("regS ubmit") != null )
in my JSP, it works fine.
But, i want to go for request.getAttribute.
If i have:
HttpSession session = request.getSession();
session.setAttribute("regS
in my servlet, and
if ( session.getAttribute("regS
in my JSP, it works fine.
But, i want to go for request.getAttribute.
Can you post the exact line you are getting the error?
You should set the attribute into the Session in one request and then retrieve it later from the Session in another/the same one
>>But, i want to go for request.getAttribute.
That would only be valid for one request
That would only be valid for one request
One request is the request you initiate and the response you receive back. Anything else is a different request.
ASKER
Is it possible to send something from my Java Class(servlet) using request.setAttribute() and retrieve it in my JSP using request.getAttribute()
You can if you the servlet is doing the request or if you forward the request.
ASKER
>> That would only be valid for one request
One request is what i need. :-)
>> Can you post the exact line you are getting the error?
As i said, With SESSIONS,
HttpSession session = request.getSession();
session.setAttribute("regS ubmit", "reg_submit");
in my Actoin Class, and
if ( session.getAttribute("regS ubmit") != null )
in my JSP works fine without any errors.
I want to do similarly like:
request.setAttribute("regS ubmit", "reg_submit"); in my Action Class, and
if ( request.getAttribute("regS ubmit") != null ) in my JSP
1. First of all, is it correct ?
If it is correct, i think i miss some declarations. For SESSIONS, i have :
HttpSession session = request.getSession();
2. Do i need something like this for my "request" also ?
One request is what i need. :-)
>> Can you post the exact line you are getting the error?
As i said, With SESSIONS,
HttpSession session = request.getSession();
session.setAttribute("regS
in my Actoin Class, and
if ( session.getAttribute("regS
in my JSP works fine without any errors.
I want to do similarly like:
request.setAttribute("regS
if ( request.getAttribute("regS
1. First of all, is it correct ?
If it is correct, i think i miss some declarations. For SESSIONS, i have :
HttpSession session = request.getSession();
2. Do i need something like this for my "request" also ?
ASKER
>> You can if you the servlet is doing the request or if you forward the request.
I am forwarding the Action Class to the JSP. :-)
I am forwarding the Action Class to the JSP. :-)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
>> just put it in the request of the struts and read it through a taglib
would be better if you could tell me how.
would be better if you could tell me how.
I am not sure... How do you forward the ActionClass?
ASKER
This is my "Action".
<action
attribute="testForm"
input="/testJSP.jsp"
name="testForm"
path="/test"
scope="request"
type="com.yourcompany.stru ts.testAct ion"
unknown="false"
validate="true">
<forward
name="success"
path="/testJSP.jsp"
redirect="false"
contextRelative="false" />
</action>
i want:
1. request.setAttribute() in my Action Class.
2. Retrieve it using request.setAttribute() in my testJSP.jsp
<action
attribute="testForm"
input="/testJSP.jsp"
name="testForm"
path="/test"
scope="request"
type="com.yourcompany.stru
unknown="false"
validate="true">
<forward
name="success"
path="/testJSP.jsp"
redirect="false"
contextRelative="false" />
</action>
i want:
1. request.setAttribute() in my Action Class.
2. Retrieve it using request.setAttribute() in my testJSP.jsp
So from your testActoin you want to go to the testJSP.jsp? Can you show me the code in your action class that you read this path attribute?
ASKER
>> So from your testActoin you want to go to the testJSP.jsp?
Yes..
>> Can you show me the code in your action class that you read this path attribute?
public class TestAction extends Action {
// -------------------------- ---------- ---------- ---------- - Instance Variables
// -------------------------- ---------- ---------- ---------- - Methods
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute( ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
TestForm testForm = (TestForm) form;
request.setAttribute("regS ubmit", "reg_submit");
return mapping.findForward("succe ss");
}
}
AND in my testJSP, I have:
<% if ( request.getAttribute("regS ubmit") != null ) { %>
<html:messages id="error">
<bean:write name="error"/>
</html:messages>
<% } %>
My doubt is how it works when i use Sessions in my Action Class, but not with 'request'.
Thanks for your input.
Yes..
>> Can you show me the code in your action class that you read this path attribute?
public class TestAction extends Action {
// --------------------------
// --------------------------
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute( ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
TestForm testForm = (TestForm) form;
request.setAttribute("regS
return mapping.findForward("succe
}
}
AND in my testJSP, I have:
<% if ( request.getAttribute("regS
<html:messages id="error">
<bean:write name="error"/>
</html:messages>
<% } %>
My doubt is how it works when i use Sessions in my Action Class, but not with 'request'.
Thanks for your input.
What you have there should work. Can you tell me what is the output of this:
<%System.out.println("para m: " + request.getParameter("regS ubmit"));% >
and also of this:
param: <bean:write name= "regSubmit" />
<%System.out.println("para
and also of this:
param: <bean:write name= "regSubmit" />
ASKER
Ok...after restarting, now i dont get any errors/exceptions, but
always getting "null".
For <%System.out.println("para m: " + request.getParameter("regS ubmit"));% > i get,
param: null
and For param: <bean:write name= "regSubmit" /> i get,
javax.servlet.ServletExcep tion: Cannot find bean RegSubmit in any scope
please tell me a solution/alternative. I increased to the max. points i have.
always getting "null".
For <%System.out.println("para
param: null
and For param: <bean:write name= "regSubmit" /> i get,
javax.servlet.ServletExcep
please tell me a solution/alternative. I increased to the max. points i have.
ASKER
again with session, it works fine.
With SESSIONS, For
<%System.out.println("para m: " + request.getParameter("regS ubmit"));% >
as welll as For
param: <bean:write name= "regSubmit" />
i get:
param: reg_submit
With SESSIONS, For
<%System.out.println("para
as welll as For
param: <bean:write name= "regSubmit" />
i get:
param: reg_submit
ASKER
Sorry...
>> <%System.out.println("para m: " + request.getParameter("regS ubmit"));% >
is actually:
<%System.out.println("para m: " + session.getParameter("regS ubmit"));% >
>> <%System.out.println("para
is actually:
<%System.out.println("para
I am really not sure what's going on :(
I suggest you add the parameter to the session and then delete it when you are done.
I suggest you add the parameter to the session and then delete it when you are done.
ASKER
yes..thats what i am doing right now.
<% if ( session.getAttribute("regS ubmit") != null ) { %>
<html:messages id="error">
<TR class="errFont"><bean:writ e name="error"/></TR>
</html:messages>
<% session.setAttribute("regS ubmit", null); } %>
I think this wont be bad designing. Isnt it ?
<% if ( session.getAttribute("regS
<html:messages id="error">
<TR class="errFont"><bean:writ
</html:messages>
<% session.setAttribute("regS
I think this wont be bad designing. Isnt it ?
ASKER
>> If you forward from a struts action then just put it in the request of the struts and read it through a taglib.
Could you plese what does this mean.
I'll award the points anyway. Thanks.
Could you plese what does this mean.
I'll award the points anyway. Thanks.
I meant what exactly you were doing:
request.setAttribute("regS ubmit", "reg_submit");
return mapping.findForward("succe ss");
this should have worked but not sure why it did not.
><% session.setAttribute("regS ubmit", null); } %>
>
>I think this wont be bad designing. Isnt it ?
Not at all. FYI you might as well use the removeAttribute method:
session.removeAttribute("r egSubmit") ;
P.s. Thank you for accepting :)
request.setAttribute("regS
return mapping.findForward("succe
this should have worked but not sure why it did not.
><% session.setAttribute("regS
>
>I think this wont be bad designing. Isnt it ?
Not at all. FYI you might as well use the removeAttribute method:
session.removeAttribute("r
P.s. Thank you for accepting :)
ASKER
14:15:00,179 ERROR [Engine] StandardWrapperValve[actio
javax.servlet.ServletExcep
// blah...blah...blah....
14:15:00,179 ERROR [Engine] ----- Root Cause -----
java.lang.NullPointerExcep
// blah...blah...blah....