2017.10.9 response對象、application對象、session對象的區別
1.response對象
response對象與request對象相對應,由服務器向客戶端輸出信息。當服務器向客戶端傳送數據時,
JSP容器會自動創建response對象並將信息封裝到response對象中,當jsp容器處理完請求後,response
對象會被銷毀。response和request結合起來完成動態網頁的交互功能。
1.1 response對象的常用方法
response對象提供了頁面重定向(sendRedirect)方法、設置狀態行(setStatus)方法和設置文本類型(setContentType)方法
方法 說明
SendRedirect(String url) 使用指定的重定向位置url向客戶端發送重定向響應
setDataHeader(String name,long data) 使用給定的名稱和日期值設置一個響應報頭,如果指定的名稱已經設置,則新值會覆蓋舊值
setHeader(String name,int value) 使用給定的名稱和整數值設置一個響應報頭,如果指定的名稱已經設置,則新值會覆蓋舊值
setContentType(String name,int value) 為響應設置內容類型,其參數值可以為tex/html、text/plain、application/x_msexcel或application/msword
setContentLength(int len) 為響應設置內容長度
setLocale(java.util.Local loc) 為響應設置地區信息
1.3重定向網頁
使用response對象中的sendRedirect()方法
頁面定時刷新或自動跳轉
<%@ 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>頁面自動刷新或定時刷新</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> 當前時間是:<%=new Date().toLocaleString()%><br/> <hr> <%response.setHeader("refresh","1"); %> </body> </html>
利用session對象獲取會話信息並顯示
<%@page contentType="text/html" import="java.util.*"pageEncoding="UTF-8" %> <html> <head> <title>利用session對象獲取會話信息並顯示</title> </head> <body> <hr> session的創建時間是: <%=new Date(session.getCreationTime())%><br/> session的ID號: <%session.getId();%><br/> 客戶最近一次訪問的是: <%=new java.sql.Time(session.getLastAccessedTime()) %> 兩次請求間隔多長時間session將被取消(ms): <%=session.getMaxInactiveInterval() %><br/> 是否新創建的session:<%=session.isNew()?"是":"否"%> </body> </html>
案例-----統計網站訪問人數
<%@page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <head> <title>統計網站訪問人數及其當前在線人數</title> </head> <body text="blue"> <%! Integer yourNumber=new Integer(0); %> <% if(session.isNew()){ //如果是一個新的會話 Integer number=(Integer)application.getAttribute("Count"); if(number==null) //如果是第一個訪問本站 { number=new Integer(1); } else { number=new Integer(number.intValue()+1); } application.setAttribute("Count",number); yourNumber=(Integer)application.getAttribute("Count"); } %> 歡迎訪問本站,您是第<%=yourNumber %>個訪問用戶。 </body> </html>
利用out對象響應用戶輸出
<%@page language="java" import="java.util.*" pageEncoding="UTF-8"%> <HTML> <head> <title>out的使用</title> </head> <body> 利用out對象輸出的頁面信息 <hr> <% out.print("aaa<br/>bbb"); out.print("<br>用戶名或密碼不正確,請重新<a href=‘http://www.sohu.com‘><font size=‘15‘ color=‘red‘>登陸</font></a>"); out.print("<br><a href=‘javascript:history.back()‘>後退</a>......"); %> </body> </HTML>
2017.10.9 response對象、application對象、session對象的區別