Java 使用 <a href=""> 从服务器 doGet() 方法从一个 servlet 重定向到另一个
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19079114/
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
Redirect from one servlet to another from the servler doGet() method using <a href="">
提问by Emil Sm?t
Hi I have a question how to redirect from starting servlet body(doGet() or doPost()) to another's servlet site?
嗨,我有一个问题,如何从启动 servlet 主体(doGet() 或 doPost())重定向到另一个 servlet 站点?
First servlet:
第一个 servlet:
public class StartingServlet extends HttpServlet
{
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException
{
PrintWriter out=response.getWriter();
out.println("Strona startowa");
// String go ="http://localhost:8080/HelloWorld/test";
// response.sendRedirect(response.encodeRedirectURL(go));
out.println("<a href=”http://localhost:8080/HelloWorld/test”> Hello World Servlet </a>");
}
}
Second servlet(the one that i want to go after i click on link):
第二个servlet(我点击链接后想去的那个):
public class HelloWorldServlet extends HttpServlet
{
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
{
PrintWriter out= response.getWriter();
out.println("Hello World Servlet");
}
}
web.xml file(servlet-mappings url-pattern for the starting servlet is empty on purpose):
web.xml 文件(用于启动 servlet 的 servlet-mappings url-pattern 故意为空):
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>HelloWorld</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Starting servlet</servlet-name>
<servlet-class>pl.javastart.servlets.StartingServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Starting servlet</servlet-name>
<url-pattern></url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Hello World Servlet</servlet-name>
<servlet-class>pl.javastart.servlets.HelloWorldServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello World Servlet</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
</web-app>
How do I do that? Thank you in advance.
我怎么做?先感谢您。
采纳答案by Juned Ahsan
To redirect a request from servlet you can use sendRedirect
method :
要重定向来自 servlet 的请求,您可以使用sendRedirect
方法:
response.sendRedirect("http://localhost:8080/HelloWorld/test");
Btw this statement:
顺便说一句:
out.println("<a href=”http://localhost:8080/HelloWorld/test”> Hello World Servlet </a>");
will simply send the link in response body but will not redirect.
将简单地在响应正文中发送链接,但不会重定向。
回答by missKK
You can link one servlet to another using href as :
您可以使用 href 将一个 servlet 链接到另一个 servlet 为:
<a href="servletURL?param=value">click here</a>
Here servletURL is written as "/nameofproject/urlpattern?"
这里 servletURL 写成“/nameofproject/urlpattern?”
Other method is getRequestDispatcher which will pass the values also to other servlet/jsp file etc.
其他方法是 getRequestDispatcher 它将值也传递给其他 servlet/jsp 文件等。
request.getRequestDispatcher("/xyz.jsp").forward(request, response);
Hope that helps.
希望有帮助。