Solved

scope of 'Request'

Posted on 2004-08-19
25
369 Views
Last Modified: 2010-03-31
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("regSubmit", "reg_submit");

in my servlet, and

i have

if ( request.getAttribute("regSubmit") != null )

in my JSP. But, it always shows exceptions in my servlet. ( I can work with sessions though ).

Any ideas. Thanks.
0
Comment
Question by:jaggybala2
  • 13
  • 10
  • 2
25 Comments
 

Author Comment

by:jaggybala2
ID: 11840393
This is the Exception i got:

14:15:00,179 ERROR [Engine] StandardWrapperValve[action]: Servlet.service() for servlet action threw exception
javax.servlet.ServletException

// blah...blah...blah....

14:15:00,179 ERROR [Engine] ----- Root Cause -----
java.lang.NullPointerException

// blah...blah...blah....
0
 
LVL 35

Expert Comment

by:girionis
ID: 11840401
Is this line

> if ( request.getAttribute("regSubmit") != null )

throwing the exception?
0
 

Author Comment

by:jaggybala2
ID: 11840475
No..i am getting exception in the servlet .. not in my JSP.

If i have:

HttpSession session = request.getSession();
session.setAttribute("regSubmit", "reg_submit");

in my servlet, and

if ( session.getAttribute("regSubmit") != null )

in my JSP, it works fine.

But, i want to go for request.getAttribute.
               
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 35

Expert Comment

by:girionis
ID: 11840498
Can you post the exact line you are getting the error?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11840507
You should set the attribute into the Session in one request and then retrieve it later from the Session in another/the same one
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11840540
>>But, i want to go for request.getAttribute.

That would only be valid for one request
0
 
LVL 35

Expert Comment

by:girionis
ID: 11840563
One request is the request you initiate and the response you receive back. Anything else is a different request.
0
 

Author Comment

by:jaggybala2
ID: 11840869
Is it possible to send something from my Java Class(servlet) using request.setAttribute() and retrieve it in my JSP using request.getAttribute()

0
 
LVL 35

Expert Comment

by:girionis
ID: 11840887
You can if you the servlet is doing the request or if you forward the request.
0
 

Author Comment

by:jaggybala2
ID: 11840921
>> 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("regSubmit", "reg_submit");

in my Actoin Class, and

if ( session.getAttribute("regSubmit") != null )

in my JSP works fine without any errors.


I want to do similarly like:

request.setAttribute("regSubmit", "reg_submit");   in my Action Class, and

if ( request.getAttribute("regSubmit") != 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 ?
0
 

Author Comment

by:jaggybala2
ID: 11840939
>> 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.  :-)
0
 
LVL 35

Accepted Solution

by:
girionis earned 70 total points
ID: 11840978
If you forward from a struts action then just put it in the request of the struts and read it through a taglib.
0
 

Author Comment

by:jaggybala2
ID: 11841110
>> just put it in the request of the struts and read it through a taglib

would be better if you could tell me how.
0
 
LVL 35

Expert Comment

by:girionis
ID: 11841163
I am not sure... How do you forward the ActionClass?
0
 

Author Comment

by:jaggybala2
ID: 11841306
This is my "Action".

<action
                  attribute="testForm"
                  input="/testJSP.jsp"
                  name="testForm"
                  path="/test"
                  scope="request"
                  type="com.yourcompany.struts.testAction"
                  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
0
 
LVL 35

Expert Comment

by:girionis
ID: 11841881
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?
0
 

Author Comment

by:jaggybala2
ID: 11842013
>> 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("regSubmit", "reg_submit");
            return mapping.findForward("success");

      }
      
}

AND in my testJSP, I have:

<% if ( request.getAttribute("regSubmit") != 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.
0
 
LVL 35

Expert Comment

by:girionis
ID: 11842237
What you have there should work. Can you tell me what is the output of this:

<%System.out.println("param: " + request.getParameter("regSubmit"));%>

and also of this:

param: <bean:write name= "regSubmit" />
0
 

Author Comment

by:jaggybala2
ID: 11848138
Ok...after restarting, now i dont get any errors/exceptions, but

always getting "null".

For <%System.out.println("param: " + request.getParameter("regSubmit"));%> i get,

param: null

and For param: <bean:write name= "regSubmit" /> i get,

javax.servlet.ServletException: Cannot find bean RegSubmit in any scope

please tell me a solution/alternative. I increased to the max. points i have.
0
 

Author Comment

by:jaggybala2
ID: 11848158
again with session, it works fine.

With SESSIONS, For

<%System.out.println("param: " + request.getParameter("regSubmit"));%>

as welll as For

param: <bean:write name= "regSubmit" />

i get:

param: reg_submit

0
 

Author Comment

by:jaggybala2
ID: 11848163
Sorry...

>> <%System.out.println("param: " + request.getParameter("regSubmit"));%>

is actually:

<%System.out.println("param: " + session.getParameter("regSubmit"));%>
0
 
LVL 35

Expert Comment

by:girionis
ID: 11850301
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.
0
 

Author Comment

by:jaggybala2
ID: 11850842
yes..thats what i am doing right now.

<% if ( session.getAttribute("regSubmit") != null ) { %>

  <html:messages id="error">
      <TR class="errFont"><bean:write name="error"/></TR>
   </html:messages>

<% session.setAttribute("regSubmit", null); } %>

I think this wont be bad designing. Isnt it ?
0
 

Author Comment

by:jaggybala2
ID: 11850848
>> 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.
0
 
LVL 35

Expert Comment

by:girionis
ID: 11850878
I meant what exactly you were doing:

request.setAttribute("regSubmit", "reg_submit");
return mapping.findForward("success");

this should have worked but not sure why it did not.

><% session.setAttribute("regSubmit", null); } %>
>
>I think this wont be bad designing. Isnt it ?

Not at all. FYI you might as well use the removeAttribute method:

session.removeAttribute("regSubmit");

P.s. Thank you for accepting :)
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
JList custom Cell Renderer refresh 15 56
github account with ecipse 1 47
HSSFWorkbook cannot be resolved error 10 65
factorial example 4 39
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now