通过Response对象实现重定向的两种方式:
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
首页 <br>
</body>
</html>
page.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'page.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%=application.getAttribute("content") %>
</body>
</html>
redirect.java
package net.csdn.servlet;
import java.io.IOException;
import java.io.PrintWriter;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
public class redirect extends HttpServlet {
publicvoid doGet(HttpServletRequest request, HttpServletResponse response)
throwsServletException, IOException {
redirect1(response);
direct2(response);
}
publicvoid doPost(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {
doGet(request,response);
}
}
方式一:
privatevoid redirect1(HttpServletResponse response) throws IOException {
PrintWriterout = response.getWriter();
out.println("servlet");
response.setStatus(302);
response.setHeader("location","http://www.baidu.com");
}
效果如图:
![](https://img-my.csdn.net/uploads/201210/28/1351387145_8143.png)
方式二:
privatevoid direct2(HttpServletResponse response) throws IOException {
response.getWriter().write("登陆!");
response.sendRedirect("http://www.baidu.com");
}
效果亦如图:
通过Response对象实现刷新并跳转页面的几种方式:
RefreshServlet .java
package net.csdn.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class RefreshServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
refreshRequestForword(request, response);
refreshToPage(response);
refreshOneSecond(response);
refreshOneSecondToPage(response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
效果一:
private void refreshRequestForword(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String content = "<meta http-equiv='refresh' content='3;url = http://www.sina.com'>登陆成功,此页面3秒钟后将自动跳转,如果无法正常跳转,请点击<a href ='/WebProject/page.jsp'>超链接</a>!";
this.getServletContext().setAttribute("content", content);
this.getServletContext().getRequestDispatcher("/page.jsp").forward(request, response);
}
效果二:
private void refreshOneSecondToPage(HttpServletResponse response)
throws IOException {
response.getWriter().write(new Date().toLocaleString());
response.setHeader("refresh", "1;url = http://www.baidu.com");
}
![](https://img-my.csdn.net/uploads/201210/28/1351388546_4354.png)
效果三:
private void refreshOneSecond(HttpServletResponse response)
throws IOException {
response.getWriter().write(new Date().toLocaleString());
response.setHeader("refresh", "1");
}
![](https://img-my.csdn.net/uploads/201210/28/1351388546_4354.png)
效果四:
private void refreshToPage(HttpServletResponse response) throws IOException {
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset = UTF-8");
response.setHeader("refresh","3;url = /WebProject/index.jsp");//3;url = http://www.sina.com
response.getWriter().write("登陆成功,此页面3秒后跳转,如果跳转失败,请点击<a href='aaa'>超链接</a>!");
}
![](https://img-my.csdn.net/uploads/201210/28/1351388546_4354.png)
![](https://img-my.csdn.net/uploads/201210/28/1351388584_3949.png)