java 在java中重定向到另一个servlet

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15869869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:07:53  来源:igfitidea点击:

Redirect to another servlet in java

javajspservlets

提问by AustinT

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

我在指向 servlet 文件中的另一个 servlet 时遇到了麻烦。我有一个 servlet 文件NewDreamServlet.java,我想将它重定向到MyDreamsServlet.java.

This is what I currently have in the NewDreamServlet.javafor redirecting.

这是我目前在NewDreamServlet.java重定向中的内容。

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.

我将如何做到这一点?如果有任何误解,请告诉我。

回答by Lews Therin

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

你试过了吗: response.sendRedirect("/YourApp/MyDreamsServlet")

回答by kuriouscoder

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

请尝试response.sendRedirect("/MyDreamsServlet")。另外,请注意,您可能需要添加 return 语句。以下帖子更详细地讨论了这一点java.lang.IllegalStateException: 响应提交后无法(转发 | sendRedirect | 创建会话)

回答by HKM

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 "";

所有这些对你的问题的回答都是错误的。1. 如果您喜欢使用 RD().forward,它在应用程序调用中更常用,您需要做的就是转到您的 web.xml 文件,并为您的第二个 servlet 的 url 部分给它任何名称想要例如。/fireServletTwo.... 现在回到您的第一个 servlet 并在 getRqstDispatcher 大括号中, write("/fireServletTwo"); 这将告诉 xml 文件查找具有该名称的 servlet 映射并运行该 servlet。2.如果你想使用send.Redirect(); 它接受一个 URL,主要用于将应用程序外部的控件传递到另一个域,它很简单.. 不要使用斜杠 /.. 只需将 servlet2 的名称写入 "";

Hope that helps

希望有帮助

回答by Dan Hargis

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

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

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