aspose file tools*
The moose likes Servlets and the fly likes Can't get RequestDispatcher forward() method or response.sendRedirect() to work Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Reply locked New topic
Author

Can't get RequestDispatcher forward() method or response.sendRedirect() to work

Joe Lemmer
Ranch Hand

Joined: Oct 24, 2008
Posts: 171
Hi there,

I really can't understand why my servlet isn't working. It is compiling fine and I have used println to make sure that the program is running where it should, but no page is being sent to the client. I have tried to use response.sendRedirect("options.jsp") as well as the following:



So when I get the password right, then the system prints out "In equals" and "After RequestDispatcher", but no new is displayed by the browser.

Does anyone have any ideas?

Thanks in advance

Joe


OCPJP 85%
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61764
    
  67

We need much more detail. Where are the JSP and HTML file located relative to the context root? What does View Source in the browser show? Any errors in the logs?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Joe Lemmer
Ranch Hand

Joined: Oct 24, 2008
Posts: 171
Ok thanks Bear.

The JSP and HTML files are located in the base directory for the web app where I'd normally keep the web stuff and where you can see the actual folder icon for WEB-INF.

View Source in the browser just shows the HTML for the web-page that I started from.

There doesn't seem to be anything out of the ordinary in the logs that I can see.

Cheers
Charles 'King
Ranch Hand

Joined: Jul 05, 2009
Posts: 56

Joe Lemmer wrote: where you can see the actual folder icon for WEB-INF.
???


As stated above more info would help. Consider the following: type the correct URL of one of your JSP's in the browser... Is the JSP at the root? If so, prefix your resource string with a "/" e.g. - getRequestDispatcher("/options.jsp");

If that solved your issue, its safe to conjecture your use of a relative path may have been the problem. If not, provide more info...
Joe Lemmer
Ranch Hand

Joined: Oct 24, 2008
Posts: 171
Hi Charles,

Thanks for your reply. I have tried using "/options.jsp" and also using the full "http://localhost:8080/MiniProject/options.jsp", which takes me to the right page when I type it into a browser.

I don't know what other information I can provide really. All of my debugging statements are printing out fine, both before and after the RequestDispatcher code, as I said above the address is fine, there are no errors in the log, the html source code is completely unchanged from the start page, the web.xml is fine. Does anybody have any other ideas? This should be simple, I've done it a few times before and it's worked fine.


Joe Lemmer
Ranch Hand

Joined: Oct 24, 2008
Posts: 171
I've gone through the MVC tutorial again in chapter 3 of HF Servlets & JSP and it worked fine. I also shortened the code so that all there was in the servlet was the RequestDispatcher code ie:

and this returned the result.jsp page fine.

I then commented out all the code in the doPost method of the servlet that won't return a page, except for:



and it still does not return the options.jsp, but it does print the After RequestDispatcher message. I'm sure it's something very basic that I'm doing wrong, but with no actual error messages anywhere, I've no idea what.
Charles 'King
Ranch Hand

Joined: Jul 05, 2009
Posts: 56


Post the code of the entire servlet and options.jsp - then post your DD...As an aside, are you deploying your app as a war file?

Joe Lemmer
Ranch Hand

Joined: Oct 24, 2008
Posts: 171
Ok cheers, here's the whole code:

DD:


options.jsp:


LoginServlet.java:



I've also tried:



I'm not getting any output to the client, although as stated I am getting the output to system.

In response to your question I am not deplying using a WAR file.

Thanks
Charles 'King
Ranch Hand

Joined: Jul 05, 2009
Posts: 56

Are you using tomcat? If so, go to the webapps directory and zip up your folder {MiniProject} and attach it. That would probably be easier than the back and forth

If you don't want to attach the folder, list your directory structure...
Charles 'King
Ranch Hand

Joined: Jul 05, 2009
Posts: 56

Joe, try this:

Put your jsp in the WEB-INF directory. Then, modify this line:



->TO<-


What happens when you access the servlet now? By the way, unless you're posting data from a form (e.g. method="POST"), you'll need a doGet service method...

Johan Van Roste
Greenhorn

Joined: Dec 10, 2009
Posts: 1
Hey Joe,

maybe you could try to use the NamedRequestDispatcher, which provides you with some more "flexibility" & not having to deal with contextnames & stuff.

First you need to declare your JSP's in your deployment descriptor.



And then use the following syntax in your LoginServlet

Joe Lemmer
Ranch Hand

Joined: Oct 24, 2008
Posts: 171
Hi,

Thanks to everyone for their responses so far.

I have made the request.getRequestDispatcher() method work by calling the servlet direct from the following code:



So the problem must be the way I am calling the servlet, or something from the client side. What I originally had for my client side code was:

HTML:


and for the javascript:


I think that the sendLogin() function is the critical thing, but I can't see any reason as to why there would be any difference to this than the simpler html that I mentioned in this post. Is it something to do with the jQuery '$.post()' method? It works fine in the sendSignup() method and as I've written before, all the params etc are getting through and the servlet is running right through to the end, so I don't see why it would make any difference how the doPost() method of my Servlet (which has the request.getDispatcher() code in) was called. It should result in the same result...except it doesn't.

Can anyone help please?

Joe
Joseph Morgan
Greenhorn

Joined: Jan 30, 2010
Posts: 6
Joe,

Did you ever solve this problem? I am having the exact same problem.

I've traced my code with output statements in both my servlet and my jsp, and my forward is actually hitting and processing my entire JSP page, but then goes off to somwhere else and the end result is a valid, but empty, html page.

Joe
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61764
    
  67

"Java Joe", please check your private messages for an important administrative matter.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can't get RequestDispatcher forward() method or response.sendRedirect() to work