File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Websphere and the fly likes RequestDispatcher not working properly in Websphere Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Websphere
Bookmark "RequestDispatcher not working properly in Websphere" Watch "RequestDispatcher not working properly in Websphere" New topic
Author

RequestDispatcher not working properly in Websphere

Shrikant Kulkarni
Ranch Hand

Joined: May 10, 2005
Posts: 42
Hi,

I have a JSP page, in this page i am using RequestDispacher to forward the request to a servlet.
The is as below
RequestDispatcher d=application.getRequestDispatcher("/EnlargeImage");
d.forward(request, response);

In the above code "/EnlargeImage" is the URL pattern for the servlet mapping mentioned in the web.xml.

The request is forwarded to the servlet successfully. But when i try to get parameters from the request, i get null values.

Can any one tell me what is the problem and how to solve it.
[ June 01, 2006: Message edited by: Bear Bibeault ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Are you sure the request parameters weren't null before you forwarded?
[ May 31, 2006: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Shrikant Kulkarni
Ranch Hand

Joined: May 10, 2005
Posts: 42
Yes they are not null, i checked it.
Harpreet Hira
Ranch Hand

Joined: Sep 27, 2001
Posts: 72
when i try to get parameters from the request, i get null values.

Are you looking for the parameters that originally came in the request OR are you looking for the attributes that your forwarding servlet might have set in request.

In case, you are looking for attribute then you need to use
request.getAttribute(String attributeName);

If you are looking for parameter there seems to be no reason why you shouldn't get it since it is same request object that has been passed to the forwarded servlet.
vishwanath nadimpally
Ranch Hand

Joined: Jan 25, 2005
Posts: 116
Originally posted by Shrikant Kulkarni:
Hi,

I have a JSP page, in this page i am using RequestDispacher to forward the request to a servlet.
The is as below
RequestDispatcher d=application.getRequestDispatcher("/EnlargeImage");
d.forward(request, response);

In the above code "/EnlargeImage" is the URL pattern for the servlet mapping mentioned in the web.xml.

The request is forwarded to the servlet successfully. But when i try to get parameters from the request, i get null values.

Can any one tell me what is the problem and how to solve it.


Instead of this

try this

I don't know how this is different from the way you are doing it, but I think this may work.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

Originally posted by vishwanath nadimpally:

I don't know how this is different from the way you are doing it, but I think this may work.


It isn't any different. It performs the exact same opreation.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Michael Valentino
Ranch Hand

Joined: Nov 01, 2005
Posts: 96
If you're trying to get the original parameters passed in to the servlet from the Request object, you won't be able to do so once the request has been forwarded. At this point, you should use attributes to allow the forwarded page/servlet to access those original parameters. I never really liked that, but in my experience with WebSphere implementations, that's how it worked and there's nothing I could do about it.


SCJP 1.4, SCWCD J2EE 1.4, SCJD J2SE 1.5, SCBCD J2EE 1.3, SCDJWS (In Progress)
Shrikant Kulkarni
Ranch Hand

Joined: May 10, 2005
Posts: 42
Thanks all,
The issue got solved by using
request.setAttribure("<name>",object);
in JSP and getting that request attribute from JSP by using
String name = (String)request.getAttribure("<name>");
in the forwarded servlet.
Ed Ward
Ranch Hand

Joined: Jan 30, 2006
Posts: 147
I know you said you got this to work by setting a request scoped attribute but I thought I'd throw in an additional two cents to up my post count.
Mr Valentino is correct, the original parameters in a request are not directly available from the request once it has been forwarded.
However, why couldn't you just set the parameters to be forwarded with the request when you get the RequestDispatcher?

Something like:


Or some such thing.....



doh!
I should think about things longer before actually posting?
Bear has corrected me below yet again.
Another try:
The parameters of the original request are available, however any new parameters passed using the RequestDispatcher take precedence over parms of the original request with the same name. This means that a call to request.getParameterValues("parmName") from the included or forwarded to resource will show them at the begining of the returned String array.
Correct?
[ June 01, 2006: Message edited by: Ed Ward ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

Originally posted by Ed Ward:

Mr Valentino is correct, the original parameters in a request are not directly available from the request once it has been forwarded.


Please be aware that if this is behavior exhibited by Websphere, it is an example of specification non-compliance. In a forward, the original request is forwarded to the next resource, be it another servlet or a JSP page, and the request parameters should still be available.
[ June 01, 2006: Message edited by: Bear Bibeault ]
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16



Anyway, Shrikant solved his problem (was it one?) with

which is the happy end.


[My Blog]
All roads lead to JavaRanch
Michael Valentino
Ranch Hand

Joined: Nov 01, 2005
Posts: 96
I'll be the first to admit that WebSphere's implementation is FAR from perfect. Unfortunately we, the developers, don't have much of a say on what to use. But whether or not the parameters are actually passed in the WebSphere implementation i was using, one thing was clear... they weren't accessible by req.getParameter("myParam")... from the sound of the problem that started this thread, it doesn't sound like WebSphere is the only implementation this occurs in either...

P.S. Congrats, glad the getAttribute() approach worked better for you.
[ June 01, 2006: Message edited by: Bear Bibeault ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

The OP didn't say what container he was using, but it was certainly not one of Tomcat or Resin which implement this correctly.

The OP should clarify what container was being used so others can be apprised of the problem. For now I'll assume it's Websphere and move this to that forum.
[ June 01, 2006: Message edited by: Bear Bibeault ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

Can anyone else that uses Websphere corrorborate this problem?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

This was a hard catch, but it was actually written in the thread's topic :
"RequestDispatcher not working properly in Websphere"
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

Actually I added the "in Websphere" to the topic while is was still in the Servlets forum so that other readers would not get confused and think that this was a problem with all containers.

So we're still waiting for container info from the original poster (please include version numbers too).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: RequestDispatcher not working properly in Websphere