Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

request.set/get Attribute error

 
Anirvan Majumdar
Ranch Hand
Posts: 261
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a servlet which sets a Vector onto the request object using request.setAttribute("List", vecList);
Thereof, i send the control to a JSP page using the following line:
response.sendRedirect("/jsp/ABC.jsp");
In the JSP page, if i retrieve the vector by the follwoing line:
Vector vList = (Vector)request.getAttribute("List");
I get a "null" value for the vector.

By the looks of it, i think that i'm making some fundamental error here .(Or is this not the way to handle request attributes??). Any help/suggestions would be welcome.
[ December 06, 2005: Message edited by: Anirvan Majumdar ]
 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fundamental error is right.

You have to forward rather than redirect. Redirect results in the brwoser making an entirely new request and attributes, stored in the previous request is lost.

Forward ensures that you are still working with the same request, but you wish to transfer control to some other process for further processing. You forward using a RequestDispatcher object.



ram.
 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I forgot to mention. Its good practise to 'return' after forwarding. Thus,



cheers,
ram.
 
siva prakash
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ramprasad,

in ur reply u mentioned that using return statement after Dispacther method is good practise, Can u pls eloborate it





Thanks,
Prakash
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have a servlet which sets a Vector onto the request object using request.setAttribute("List", vecList);
Thereof, i send the control to a JSP page using the following line:
response.sendRedirect("/jsp/ABC.jsp");


Whenever you use redirect() the web browser picks the new URL from the response header and makes a new request. So any attribute set in request scope are lost in the destination(JSP page in your example).

Download HTTPLiveHeaders(FireFox plug-in) to have a look the request/response headers.


ramprasad
Its good practise to 'return' after forwarding.


Never heard of this.
 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Its good practise to 'return' after forwarding.

----------------------------------------------------------------------------

Never heard of this.


It about time then.

Request forwarding is entirely servlet specific stuff. If you have code that say, conditionally forwards the request, that will not ensure that code after the forwarding would not execute. This may potentially cause an exception, say, when you try accessing an output stream after the request is forwarded.

For example

would cause an IllegalStateException when the 'if' condition evaluates to true and the request is forwarded. This is so because after he request forwarding, you cannot write anything to the output stream from where you forwarded the request.

Thus,


would ensure that the method returns after forwarding correctly.

ram.
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry ramprasad, I don't think what you said will work out.

If I want to get back the control after forwarding the request to a resource I would rather use RequestDispatcher.include().
 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You misunderstood what I said - I never mentioned about getting the control back after forwarding.

What I said was that if you have code that forwards a request, you should return immediately after forwarding because in case of a conditional forward, you may have other code that shouldnt execute if the request has been forwarded (please look at the example I posted).

The point I am trying to make is that if you forward from resource1 to say, resource2, then you are done with resource1 for all means and purposes. The container doesnt allow you to write output from resource1. However all said and done, it's still a java class and if you have **code after forward**, that will still execute and possibly cause an IllegalStateException.

Please read the previous post carefully.

ram.
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
However all said and done, it's still a java class
Absolutely. Just because you're writing a servlet doesn't mean there's magic taking place. It's still Java. When you call a method, control always returns to the next statement in your code (unless an exception is thrown or the entire JVM ends or the method called goes into an infinite loop).
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You misunderstood what I said


Ya I misunderstood what you said in the first place.


if you have **code after forward**, that will still execute and possibly cause an IllegalStateException.


Sure I will follow what you said.
[ March 22, 2006: Message edited by: Vishnu Prakash ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!