JSP 页面重定向


JSP 页面重定向

当需要将文档移动到一个新的位置时,就需要使用JSP重定向了。

最简单的重定向方式就是使用response对象的sendRedirect()方法。这个方法的签名如下:

public void response.sendRedirect(String location)
throws IOException 

这个方法将状态码和新的页面位置作为响应发回给浏览器。您也可以使用setStatus()和setHeader()方法来得到同样的效果:

....
String site = "http:" ;
response.setStatus(response.SC_MOVED_TEMPORARILY);
response.setHeader("Location", site); 
....

实例演示

这个例子表明了JSP如何进行页面重定向:

<%@ page import="java.io.*,java.util.*" %>
<html>
<head>
<title>Page Redirection</title>
</head>
<body>
<center>
<h1>Page Redirection</h1>
</center>
<%    
// 重定向到新地址    
String site = new String("http:");    
response.setStatus(response.SC_MOVED_TEMPORARILY);    
response.setHeader("Location", site);  %>
</body>
</html>

将以上代码保存在PageRedirecting.jsp文件中,然后访问http://localhost:8080/PageRedirect.jsp,它将会把您带至/

作者:唐伯虎点蚊香,如若转载,请注明出处:https://www.web176.com/jsp/13675.html

(0)
打赏 支付宝 支付宝 微信 微信
唐伯虎点蚊香的头像唐伯虎点蚊香
上一篇 2023年3月19日
下一篇 2023年3月19日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注