aspose file tools*
The moose likes Servlets and the fly likes Problem with getRequestDispatcher Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Problem with getRequestDispatcher" Watch "Problem with getRequestDispatcher" New topic
Author

Problem with getRequestDispatcher

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi all,
The following is the url used to display a jsp file.
http://localhost:8080/JSP/calendar_main.jsp?a_month=0&source=login&agn_month=1&agn_day=0&agn_year=0
I'm using tomcat 3.2
In the page, a column displays a form so the user enters some info. and click on submit button. The form post to a servlet(Calendar_Servlet) and if everything is fine, it should redisplay the same jsp page(as url above) I want to reload the page because a javabean is created(in the servlet) and property are set. First I do a session.setAttribute("bean_name", bean_info) and I want to use getName() and other functions from the jsp page.

I use to following code :
String _parent = "/JSP/calendar_main.jsp?a_month=0&source=calendar&agn_month=0&agn_day=20&agn_year=2001";

dispatch = req.getRequestDispatcher( _parent );
dispatch.include( req, res );
When the page is redisplayed, the new url displayed is: http://localhost:8080/servlet/Calendar_Servlet
My question is why the above url? How come the url isn't setup using the value in getRequestDispatcher?
Is the problem in server.xml ? I noticed the following:
<RequestInterceptor className="org.apache.tomcat.request.InvokerInterceptor"
debug="0" prefix="/servlet/" />
Is it where "servlet" (in the wrong url) is coming from?
In server.xml, I changed servlet to JSP(where I keep all jsp/html files), I get a HTTP 404 page not found. The url is still : http://localhost:8080/servlet/Calendar_Servlet
Any help would be appreciated.
Thanks
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

It's because the original request is going through your Calendar_servlet.

That is who is responding to the web request.. even though it dispatched off to another page, the client still sees the response as coming from your servlet.

To make the address bar look pretty, I'd investigate using a sendRedirect() instead.

This will send (I'm not sure.. either 301 or 303) a 300 HTTP response code to the browser, and will make the browser reload a different URL.. and happily, you send it which URL to redirect to.
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

And I *just* saw something else...

You are using dispatch.include(request, response).

What this does is includes the output of the resource you are dispatching to, in the response from the requesting resource (in this case, the servlet).

Before trying the sendRedirect(), try changing this to dispatch.forward(request, response), which will forward execution to the requested resource and *that* resource is responsible for output.
Garann Means
Ranch Hand

Joined: Jan 28, 2002
Posts: 214
I am having this same problem, and thought I couldn't use sendRedirect() as it wouldn't pass the information in the request?
My app works fine the first time I submit my form, but once it reloads the JSP (using a RequestDispatcher), the url is incorrect and trying to submit it again produces an error telling me the resource (i.e., the servlet) is not available.
Here is the code from the servlet:

Thanks in advance for any help,
g.
[ July 01, 2002: Message edited by: Garann Rose Means ]
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

You have an error in 'phone.jsp'.
Try read the log file(s).

/Rene


Regards, Rene Larsen
Dropbox Invite
Garann Means
Ranch Hand

Joined: Jan 28, 2002
Posts: 214
Rene,
Can you be more specific? What sort of error do you think I have?
In looking through the log file, nothing appears to be amiss.
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

What is in the JSP-file?
What web-server are you using?
/Rene
[ July 01, 2002: Message edited by: Rene Larsen ]
Garann Means
Ranch Hand

Joined: Jan 28, 2002
Posts: 214
So far, all that's in the JSP is HTML. It doesn't use any scriptlets.
I'm using Tomcat on my local machine.
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

Under Tomcat 4.x is a folder 'logs', in this folder is a file called 'localhost_log.<DATE>.txt'. This file contain the error from the JSP page.
/Rene
[ July 02, 2002: Message edited by: Rene Larsen ]
Garann Means
Ranch Hand

Joined: Jan 28, 2002
Posts: 214
As I said above, there are no errors listed in the log file. Everything appears as expected. The only entries detail what was loaded, initialized, unloaded, etc.
The above thread indicates that this behavior is normal when a servlet dispatches to a JSP. For usability purposes, I need to change the url after the servlet runs to the location of the JSP, not the servlet, be it through JavaScript or any other way I can accomplish this.
Are there any suggestions regarding how to either force the dispatcher to display the URL of the JSP, or how to fix the URL once control returns to the JSP?
Thanks,
g.
[ July 03, 2002: Message edited by: Garann Rose Means ]
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

You can 'change' the page like this:
JavaScript:
JSP:
/Rene
[ July 03, 2002: Message edited by: Rene Larsen ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Problem with getRequestDispatcher
 
Similar Threads
Getting started with JSP-Servlet using apache-tomcat-5.5.17
Servlet Context Virtual Sandbox
Tomcat 5 always asks for login & password-HELP
Ajax with jQuery and servlets
form based authentication example