[英]Forward A servlet request to a .html file in java?
Basically I have servlet named forward
.基本上我有一个名为forward
的servlet。 When a request is made to it, it forwards the request to a.html file like this:当向它发出请求时,它将请求转发到一个 .html 文件,如下所示:
@WebServlet("/forward")
public class forward extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("/videos/forward.html").forward(request, response);
return;
}
}
The problem is that when I test this on eclipse when a request is made to this servlet, it responds with the link as localhost/videos/forward.html
问题是,当我在 eclipse 上测试这个 servlet 时,它会以localhost/videos/forward.html
forward.html 的链接作为响应
But then when I deployed it with name com.war
Now when a request is made to it, it responds with localhost/com/videos/forward.html
但是当我使用名称com.war
部署它时,当向它发出请求时,它会响应localhost/com/videos/forward.html
How can I make the requestDispatcher to respond with localhost/videos/forward.html
and not as localhost/com/videos/forward.html
如何使 requestDispatcher 响应localhost/videos/forward.html
forward.html 而不是localhost/com/videos/forward.html
No you cannot. 你不能。 Forwarding is a request made to the servlet container to pass control to another servlet in same servlet context. 转发是向Servlet容器发出的请求,用于将控制权传递给同一Servlet上下文中的另一个Servlet 。 A JSP page in indeed implemented as a servlet, but a HTML is just a ressource, so you cannot forward to it. JSP页面确实实现为servlet,但是HTML只是资源,因此您不能转发给它。
But you can redirect to it. 但是您可以重定向到它。 A redirection works by sending a special response telling the browser that it should go to that other URL. 重定向的工作原理是发送一个特殊的响应,告知浏览器应转到其他URL。 As it works at browser level, you can redirect to a HTML page or even to a completely different site. 由于它可以在浏览器级别工作,因此您可以重定向到HTML页面,甚至重定向到完全不同的站点。
You can use the sendRedirect
method from HttpServletResponse
to initiate a redirection from a servlet: 您可以使用HttpServletResponse
的sendRedirect
方法来启动servlet的重定向:
@WebServlet("/forward")
public class forward extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.sendRedirect("/videos/forward.html");
return;
}
}
只需编写response.sendRedirect(pagename.html)
Yes, u can.是的你可以。 use:利用:
RequestDispatcher r = req.getRequestDispatcher(String arg);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.