Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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?
 
Joe Lemmer
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
 
Ranch Hand
Posts: 56
Eclipse IDE Postgres Database Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

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
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 56
Eclipse IDE Postgres Database Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

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
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 56
Eclipse IDE Postgres Database Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 56
Eclipse IDE Postgres Database Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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...

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
"Java Joe", please check your private messages for an important administrative matter.
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic