jsp Response物件頁面重定向、時間的動態顯示
阿新 • • 發佈:2021-07-30
Response物件
response物件主要用於對客戶端的請求進行迴應,將web伺服器處理後的結果發回給客戶端,封裝了p產生的響應,併發送到客戶端響應客戶端的請求,請求的資料可以是各種資料型別,甚至是檔案。
常用的方法:void addCookie(Cookie c)
新增一個Cookie物件,用來儲存客戶端使用者資訊。
比如一些登入操作是否記住使用者名稱等等。void setHeader(String name,String value)
常用的有重新整理操作和定時跳轉頁面。
response.setHeader(“refresh”,“1”),response.setHeader(“refresh”,“2:URL=xxx”)。
void sendRedirect(String url)設定頁面重定向。
舉例1:時間動態顯示
<%@ page language="" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.util.*"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>日期的更新</title> </head> <body> <% //void setHeader(String name,String value) //功能:常用的重新整理refresh,例如:response.setHeader("refresh","1") //幾秒後跳轉:response.setHeader("refresh","2:URL=xxx") Date now = new Date(); out.print(now.toLocaleString()); response.setHeader("refresh","1");//一秒重新整理一次 %> </body> </html>
頁面的重定向:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>頁面重定向</title> </head> <body> <% /* *在瀏覽器進行重定向。跳轉時機,當頁面程式碼執行完畢, *把響應傳送給客戶端之後,客戶端再根據 *重新指向的url地址。瀏覽器位址列中的地址是改變的 */ response.sendRedirect("response3.jsp"); %> </body> </html>
跳轉的頁面
<%@ page language="jahttp://www.cppcns.comva" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta chBLxlyjarset="UTF-8"> <title>重定向後的內容</titleBLxlyj> </head> <body> <www.cppcns.comh1>重定向後的頁面</h1> </body> </html>
到此這篇關於jsp Response物件頁面重定向、時間的動態顯示的文章就介紹到這了,更多相關jsp Response頁面重定向內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!