JSP具體篇——response對象


response對象

response對象用於響應client請求,向客戶輸出信息。

他封裝了JSP產生的響應,並發送到client以響應client請求。

 

1.重定向網頁

使用response對象的sendRedirect()方法能夠將網頁重定向到還有一個頁面。重定向支持將地址重定向到不同的主機上,這一點與轉發不同。在client瀏覽器上將會得到跳轉后的地址,並又一次發送請求鏈接;用戶能夠從瀏覽器的地址欄中看到跳轉后的地址;重定向操作后,request中的屬性將會所有失效,並開始一個新的request對象

sendRedirect()方法的語法格式例如以下:

response.sendRedirect(String path);

參數說明:

path:指定的目標地址,能夠是相對路徑,也能夠是不同主機的其它URL地址

 

范例:

定義index.jsp重定向到Login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<%response.sendRedirect("Login.jsp"); %>

 

<!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/csshref="styles.css">

-->

 

  </head>

  

  <body>

    This is my JSP page. <br>

  </body>

</html>

 

 

定義Login.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 'Login.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/csshref="styles.css">

-->

 

  </head>

  

  <body>

    <form  name="form1" method="post" action="">

     username:<input name="name" type="text" id="name" style="width:120px"><br/><br/>

     密碼:<input name="pass" type="password" id="pass" style="width:120px"><br/>

     <br/>

     <input type="submit" name="submit" value="提交">

    </form>

  </body>

</html>

 

2.處理HTTP頭文件

response對象能夠設置HTTP響應包頭,當中最經常使用的是禁用緩存。設置頁面自己主動刷新和定時跳轉頁面。

禁用緩存

默認情況下。瀏覽器將會顯示的網頁的內容進行緩存,能夠通過response對象來實現對緩存的禁用。

范例:

改動Login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<%

response.setHeader("Catch-control""no-store");

response.setDateHeader("Expires", 0);

 %>

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <base href="<%=basePath%>">

    

    <title>My JSP 'Login.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/csshref="styles.css">

-->

 

  </head>

  

  <body>

    <form  name="form1" method="post" action="">

     username:<input name="name" type="text" id="name" style="width:120px"><br/><br/>

     密碼:<input name="pass" type="password" id="pass" style="width:120px"><br/>

     <br/>

     <input type="submit" name="submit" value="提交">

    </form>

  </body>

</html>

 

設置頁面定時跳轉

范例:

改動index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<%

  response.setHeader("refresh""5;URL=hello.jsp");

  %>

 

<!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/csshref="styles.css">

-->

 

  </head>

  

  <body>

    This is my JSP page. <br>

  </body>

</html>

 

 

 

設置頁面定時刷新

   <%

   

     response.setHeader("refresh""5");

    

     %>

設置輸出緩沖

通常情況下,server要輸出到client的內容不會直接寫到client,而是先寫到一個輸出緩沖區。當滿足以下三種情況之中的一個就會把緩沖區的內容寫到client

A.JSP頁面的輸出已經所有寫到了緩沖區

B.緩沖區已經滿了

C.在JSP頁面中調用了response對象的flushBuffer()方法或者是out對象的flush()方法

 

response對象對緩沖區的配置有例如以下幾種方法:

flushBuffer():強制將緩沖區中的內容輸出到client

getBufferSize():獲取對應所使用的緩沖區的實際大小。假設沒有緩沖區則返回0

reset():清除緩沖區的內容 。同一時候清除狀態碼和報頭

isCommited():檢測server端是否已經把數據寫入client。

 

范例:

將緩沖區的大小設置為20KB

response.setBufferSize(); 

PS:假設將緩沖區的大小設置為0則表示不緩沖




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM