0

I am having troubles of directing to another servlet in a servlet file. I have a servlet file called NewDreamServlet.java and I want to redirect it to MyDreamsServlet.java.

This is what I currently have in the NewDreamServlet.java for redirecting.

request.getRequestDispatcher("/MyDreamsServlet").forward(request, response);

When I call this it ends up going to a blank page,

http://localhost:8080/ps10-austint/NewDreamServlet

How exactly would I accomplish this? Please let me know if there is any misunderstanding.

Jayamohan
  • 12,734
  • 2
  • 27
  • 41
AustinT
  • 1,998
  • 8
  • 40
  • 63

4 Answers4

1

Did you try: response.sendRedirect("/YourApp/MyDreamsServlet")

Lews Therin
  • 10,907
  • 4
  • 48
  • 72
0

Please try response.sendRedirect("/MyDreamsServlet"). Also, please note that you might have to add an return statement. The following post discusses this in more details java.lang.IllegalStateException: Cannot (forward | sendRedirect | create session) after response has been committed

Community
  • 1
  • 1
kuriouscoder
  • 5,394
  • 7
  • 26
  • 40
0

All these answers to your question are wrong. 1. if you like to use RD().forward, which is more used for with in application calls, all you need to do is go to your web.xml file and for the url part of your 2nd servlet give it any name you would like eg. /fireServletTwo.... Now come back to your 1st servlet and in the getRqstDispatcher braces, write("/fireServletTwo"); this will tell the xml file to look for a servlet mapping with that name and run that servlet. 2. if you would like to use send.Redirect(); which takes a URL and is used to mostly pass controls outside of the application to another domain, its simple.. DO NOT USE A SLASH /.... just write the name of your servlet2 inside "";

Hope that helps

HKM
  • 1
  • 3
0

This one works for me but it usually better to have the context path:

response.sendRedirect(request.getContextPath() + "/home.jsp");
Dan Hargis
  • 457
  • 5
  • 13