aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes request Attribute doubt? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "request Attribute doubt?" Watch "request Attribute doubt?" New topic
Author

request Attribute doubt?

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9351
    
    2

Hi Guys,

I tried the following and got end up with a null pointer exception!

The test.jsp,


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Nikhil Jain
Ranch Hand

Joined: May 15, 2005
Posts: 385
How is the doGet being called. Are you forwarding it to the Servlet or something....


SCJP 1.4, SCWCD 1.4, SCBCD 1.5
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9351
    
    2

Yes,

I'm using a servlet-mapping element as below,
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi Jothi Shankar Kumar Sankararaj ,



Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9351
    
    2

Srinivas,

I'm not able to follow...1.4 sends the response form to user.
1.5 request ends here. (request and all attributes gone
with wind)

Why and how??
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi Shankar,

Important to know What specs guarentee you!

SRV.4.10 Lifetime of the Request Object

.
[ April 26, 2007: Message edited by: Srinivasan thoyyeti ]
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

The user is making two requests.

The first one for test.jsp (the form page) by typing the correct url in the browser, I guess.
The second for "ExampleServlet" (this requested is triggered when he presses the "SUBMIT" button).


SCJP 1.4 (88%) - SCJP 5.0 Upgrade (93%) - SCWCD 1.4 (97%) - SCBCD 5.0 (98%)
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9351
    
    2

Got the catch! Srinivasan, thanks for bringing the bits from the servlet specs.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9351
    
    2

Guys,

Then in such a situation (for the scenario as above where we have to go from a jsp to a servlet), we can only place this in the application scope or session scope. Am I right??
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9351
    
    2

So what else could be the way to use the request attribute for the code above?? I want to set the attribute in the jsp and get it in the servlet. Should I get a request dispatcher and forward the request and response or Should I store this as a session attribute which I don't want since I'm not going to use session for the above example??
[ April 26, 2007: Message edited by: Jothi Shankar Kumar Sankararaj ]
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Then in such a situation (for the scenario as above where we have to go from a jsp to a servlet), we can only place this in the application scope or session scope. Am I right??


You could put some values in html hidden fields too. They will be sent in the request as strings.


[My Blog]
All roads lead to JavaRanch
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9351
    
    2

Satou,

The question is when I set the request attribute in a jsp and when hitting the submit button, the jsp calls a servlet and in that servlet, I'm trying to get the values of the attribute using getAttribute method and when I tried this, it gave me NullPointerException. I later understood that when the service method of the jsp finishes the request attributes gets vanished which is why I'm getting a null pointer. So like you say when I use a hidden field it is again the same or??
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Sorry I got confused. Putting them in hidden fields, you would have to use getParameter to retrieve them, not getAttribute. Please forget about this.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9351
    
    2

Satou,

Then what would be the solution to my question?? I want to use only request attribute.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9351
    
    2

Can I try doing something with a requestdispatcher like this in my jsp??

RequestDispatcher view = request.getRequestDispatcher("/ExampleServlet")?? Is the argument correct or should it only point to a jsp???
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

You can't, because the request is generated by the web client after submission. You can do it with servlet->jsp, not jsp->servlet. If you need to store something beyong the scope of a request, then you'll have to put it in session or application scope.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9351
    
    2

I guessed it and you confirmed it Satou. You help me a lot. Thanks once again
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: request Attribute doubt?