JSP 页面重定向
jsp 页面重定向
当需要将文档移动到一个新的位置时,就需要使用jsp重定向了。
最简单的重定向方式就是使用response对象的sendredirect()方法。这个方法的签名如下:
public void response.sendredirect(string location) throws ioexception
这个方法将状态码和新的页面位置作为响应发回给浏览器。您也可以使用setstatus()和setheader()方法来得到同样的效果:
....
string site = "http://www.yapf.com" ;
response.setstatus(response.sc_moved_temporarily);
response.setheader("location", site);
....
实例演示
这个例子表明了jsp如何进行页面重定向:
<%@ page language="java" contenttype="text/html; charset=utf-8"
pageencoding="utf-8"%>
<%@ page import="java.io.*,java.util.*" %>
<html>
<html>
<head>
<title>页面重定向</title>
</head>
<body>
<h1>页面重定向</h1>
<%
// 重定向到新地址
string site = new string("http://www.yapf.com");
response.setstatus(response.sc_moved_temporarily);
response.setheader("location", site);
%>
</body>
</html>
将以上代码保存在pageredirecting.jsp文件中,然后访问http://localhost:8080/pageredirect.jsp,它将会把您带至http://www.yapf.com/。


