当前位置: 代码迷 >> Web前端 >> 深入体会JavaWeb开发内幕之Response对象操作实例之通过Response实现重定向和刷新跳转并跳转页面
  详细解决方案

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

热度:127   发布时间:2012-11-09 10:18:48.0
深入体验JavaWeb开发内幕之Response对象操作实例之通过Response实现重定向和刷新跳转并跳转页面

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

    }



  


    

  相关解决方案