您的位置:首页 > 编程语言 > Java开发

深入体验JavaWeb开发内幕之Response对象操作实例之通过Response实现重定向和刷新跳转并跳转页面

2012-10-28 09:52 811 查看
通过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");

}
效果如图:





方式二:

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"); }






效果三:

private void refreshOneSecond(HttpServletResponse response)
throws IOException {
response.getWriter().write(new Date().toLocaleString());
response.setHeader("refresh", "1");
}






效果四:

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>!");

}




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐