简体   繁体   中英

Redirect to another servlet in java

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.

您是否尝试过: response.sendRedirect("/YourApp/MyDreamsServlet")

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

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

这对我有用,但是通常最好有上下文路径:

 response.sendRedirect(request.getContextPath() + "/home.jsp"); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM