Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in setAttribute() for Request object

 
Abhijit Das
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
I want to pass information from Servlet1 to Servlet2 in the request object by using setAttribute(). The hyperlink is used to invoke the Servlet2 .
I tried the following code...
Servlet1.java

Servlet2.java


For the above code, the value is coming null. Again, if i use the RequestDispatcher object in the Servlet1.java, I am getting the value.

Why ?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Abhijit Das:
hi,
I want to pass information from Servlet1 to Servlet2 in the request object by using setAttribute(). The hyperlink is used to invoke the Servlet2 .
I tried the following code...
Servlet1.java

Servlet2.java


For the above code, the value is coming null. Again, if i use the RequestDispatcher object in the Servlet1.java, I am getting the value.

Why ?


1.if you use hyperlink to access servlet2..then you have to use session object insteadof request.

2.still if you want to use request...then forward your request object to servlet2 using RquestDispatcher

3. if you do not want to leave hperlink then pass the value (Querystring)[href="a.jsp?param=value"]

Hope This Helps
 
Abhijit Das
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello friend,
I used both setAttribute() and getAttribute() in Session and ServletContext APIs with hyperlink.. It is working fine.
But without using RequestDispatcher , Is it possible to use setAttribute() in request object?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Abhijit Das:
hello friend,
I used both setAttribute() and getAttribute() in Session and ServletContext APIs with hyperlink.. It is working fine.
But without using RequestDispatcher , Is it possible to use setAttribute() in request object?

But without using RequestDispatcher --- Not Possible

Hi Abhijit Das,

request scope: imagine,if you want to access the request scope in a.jsp,
then you should forward the rquest object to a.jsp

for more Details,please read about scope of the request
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic