Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with request.setAttribute in jsp

 
roger james
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I am having two jsp pages.

One.jsp and two.jsp.

I am using scriplet code in the One.jsp to set attribute like this :

request.setAttribute(“Name”,”Java”);

In the form action of one.jsp I am giving the value as “two.jsp”.

And after submitting , when I am trying to get the attribute using request.getAttribute(“Name”) in the second jsp , it is printing null.

I checked if the request objects are different , but when I printed the request object in one and two jsps they both gave same value ,i.e same request object is passed from one.jsp to two.jsp.

Then why am I not able to get the attribute in the second jsp page if the request object that is passed is same.

Anybody help me understanding , how and when the request object is created.
 
Dawn Charangat
Ranch Hand
Posts: 249
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
roger james wrote:

Request.setAttribute(“Name”,”Java”);




you might want to change the 'Request' to 'request'. The latter is an implicit object in JSP, but the former one is an unknown entity.
 
Ramkumar Subburaj
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you submit a form or go to a another jsp page, a new request object is created. I think

When you one.jsp is loaded, a request object is created and you are setting the value in that object.



When you submit the form a new JSP page(two.jsp) is rendered and a new request object is created.

Hope this helps and I can be pretty sure that same request object cannot be transferred when you submit the form.

I think the only chance where the request object can be forwarded is when you forward the request.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65110
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
roger james wrote:i.e same request object is passed from one.jsp to two.jsp.

Your conclusion is incorrect. The two requests are different,.
 
roger james
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
roger james wrote:i.e same request object is passed from one.jsp to two.jsp.

Your conclusion is incorrect. The two requests are different,.


Hi Bear Bibeault,
I printed the request object in both one.jsp and two.jsp. I got same object ...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic