如何在Servlet中将重定向发送到JSP页面 [英] How to send redirect to JSP page in Servlet
本文介绍了如何在Servlet中将重定向发送到JSP页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我在servlet中完成处理并且结果有效时,则需要将响应重定向到另一个JSP页面,例如Web内容文件夹中的welcome.jsp
.我该怎么办?
When I'm done processing in a servlet, and the result is valid, then I need to redirect the response to another JSP page, say welcome.jsp
in web content folder. How can I do it?
例如:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
// Some processing code here ...
// How do I redirect to another JSP here when I'm ready?
} catch (Exception e) {
throw new ServletException(e);
}
}
推荐答案
Look at the HttpServletResponse#sendRedirect(String location)
method.
用作:
response.sendRedirect(request.getContextPath() + "/welcome.jsp")
Alternatively, look at HttpServletResponse#setHeader(String name, String value)
method.
通过添加位置标头设置重定向:
The redirection is set by adding the location header:
response.setHeader("Location", request.getContextPath() + "/welcome.jsp");
这篇关于如何在Servlet中将重定向发送到JSP页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文