*
The moose likes Servlets and the fly likes Can I forward a local variable to another servlet? 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 » Java » Servlets
Bookmark "Can I forward a local variable to another servlet?" Watch "Can I forward a local variable to another servlet?" New topic
Author

Can I forward a local variable to another servlet?

Ken Shamrock
Ranch Hand

Joined: Jan 23, 2002
Posts: 139
If I forward or include a servlet B from servlet A,
req.getRequestDispatcher(forwardTo).forward(req, res);
req.getRequestDispatcher(includeIn).include(req, res);
besides forward the req (request) varible by user, can I forward a local/global variable declared inside Servlet A? (eg String a=test; can I forward this 'a' variable to another servlet by forward or include) ? Thanks


Thanks everyone who helped me
Adam Hardy
Ranch Hand

Joined: Oct 09, 2001
Posts: 566
Set the variable as an attribute to the request.
request.setAttribute("myVar", localVar);


I have seen things you people would not believe, attack ships on fire off the shoulder of Orion, c-beams sparkling in the dark near the Tennhauser Gate. All these moments will be lost in time, like tears in the rain.
Ken Shamrock
Ranch Hand

Joined: Jan 23, 2002
Posts: 139
oh yes..thanks
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Can I forward a local variable to another servlet?