標籤:date() intvalue 位置 imp 資訊 間隔 encoding scheme tac
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對象的區別