2017.10.9 response對象、application對象、session對象的區別

來源:互聯網
上載者:User

標籤: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對象的區別

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.