当须要将文档移动到一个新的位置时,就须要使用JSP重定向了。html
最简单的重定向方式就是使用response对象的sendRedirect()方法。这个方法的签名以下:java
public void response.sendRedirect(String location) throws IOException
这个方法将状态码和新的页面位置做为响应发回给浏览器。您也能够使用setStatus()和setHeader()方法来获得一样的效果:浏览器
.... String site = "http://www.runoob.com" ; response.setStatus(response.SC_MOVED_TEMPORARILY); response.setHeader("Location", site); ....
这个例子代表了JSP如何进行页面重定向: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.runoob.com"); response.setStatus(response.SC_MOVED_TEMPORARILY); response.setHeader("Location", site); %> </body> </html>
将以上代码保存在PageRedirecting.jsp文件中,而后访问http://localhost:8080/PageRedirect.jsp,它将会把您带至http://www.runoob.com/。spa