Java HttpServletResponse響應(yīng)實現(xiàn)過程詳解
用戶在客戶端輸入網(wǎng)址(虛擬路徑)時,開始發(fā)送一個HTTP請求(請求行、請求頭、請求體)至服務(wù)器。服務(wù)器內(nèi)的Tomcat引擎會解析請求的地址,去找XML文件,然后根據(jù)虛擬路徑找Servlet的真實路徑,真實的Servlet會將請求的信息封裝成request(請求)對象,然后再創(chuàng)建一個response(響應(yīng))對象,(此時的response內(nèi)是空的)同時創(chuàng)建servlet對象,并調(diào)用service方法(或doGet和doPost方法)。
這樣就是把兩個對象傳給了服務(wù)器內(nèi)的某個servlet的service方法,通過這個方法,我們可以獲得request的所有的信息,并且向response內(nèi)設(shè)置信息。response.getwriter().write()將內(nèi)容寫到response的緩沖區(qū),這樣service方法結(jié)束了,方法返回后,tomcat引擎會將從該response緩沖區(qū)中獲取的設(shè)置信息封裝成一個HTTP響應(yīng)(響應(yīng)行、響應(yīng)頭、響應(yīng)體),發(fā)送給客戶端??蛻舳私馕鲰憫?yīng)回來的東西繼而進(jìn)行顯示。
概述:
我們在創(chuàng)建Servlet時會覆蓋service()方法,或doGet()/doPost(),這些方法都有兩個參數(shù),一個為代表請求的request和代表響應(yīng)response。service方法中的response的類型是ServletResponse,而doGet/doPost方法的response的類型是HttpServletResponse,HttpServletResponse是ServletResponse的子接口,功能和方法更加強(qiáng)大
通過response 設(shè)置響應(yīng)行:
設(shè)置響應(yīng)行的狀態(tài)碼:setStatus( int sc)
通過response 設(shè)置響應(yīng)頭:
setHeader(String name,String value) 設(shè)置
三秒以后跳轉(zhuǎn)到百度:
public class RefreshServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //設(shè)置定時刷新的頭 response.setHeader("refresh","5;url=https://www.baidu.com"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <script type="text/javascript"> window.onload=function(){ //獲取span元素 var second=document.getElementById("second"); //定義秒數(shù) var time =5; //設(shè)置定時器 var timer=setInterval(function(){ second.innerHTML=time; time--; if(time < 0){ clearInterval(timer); location. rel="external nofollow" rel="external nofollow" ; } },1000); } </script> </head> <body> 恭喜您,注冊成功! <span id="second" style="color:red">5</span> 秒后跳轉(zhuǎn),如沒跳轉(zhuǎn),請點擊<a rel="external nofollow" rel="external nofollow" >這里</a> </body> </html>
重定向:(請求服務(wù)器兩次,地址欄變化)
①、狀態(tài)碼:302;
②、響應(yīng)頭:location 代表重定向地址;
public class Servlet01 extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /*// 設(shè)置響應(yīng)狀態(tài)碼 response.setStatus(302); //設(shè)置響應(yīng)頭中的Location response.setHeader("Location","/WEB0/Servlet02");*/ //重定向 response.sendRedirect("/WEB0/Servlet02"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
public class Servlet02 extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().write("Servlet02"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
深入Spring Boot之ClassLoader的繼承關(guān)系和影響
這篇文章主要介紹了深入Spring Boot之ClassLoader的繼承關(guān)系和影響,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06學(xué)習(xí)Java之如何正確地向上轉(zhuǎn)型與向下轉(zhuǎn)型
面向?qū)ο蟮牡谌齻€特征是多態(tài),實現(xiàn)多態(tài)有三個必要條件:繼承、方法重寫和向上轉(zhuǎn)型,在學(xué)習(xí)多態(tài)之前,我們還要先學(xué)習(xí)Java的類型轉(zhuǎn)換,本篇文章就來帶大家認(rèn)識什么是類型轉(zhuǎn)換,看看類型轉(zhuǎn)換都有哪幾種情況,以及如何避免類型轉(zhuǎn)換時出現(xiàn)異常2023-05-05Spring Boot 實現(xiàn)https ssl免密登錄(X.509 pki登錄)
這篇文章主要介紹了Spring Boot 實現(xiàn)https ssl免密登錄(X.509 pki登錄),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01java:程序包org.bouncycastle.jce.provider不存在問題及解決
這篇文章主要介紹了java:程序包org.bouncycastle.jce.provider不存在問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05IntelliJ IDEA中Project與Module的概念以及區(qū)別
這篇文章主要給大家介紹了關(guān)于IntelliJ IDEA中Project與Module的概念以及區(qū)別的相關(guān)資料,文中通過實例介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01IntelliJ IDEA 關(guān)閉多余項目的操作方法
這篇文章主要介紹了IntelliJ IDEA 關(guān)閉多余項目的操作方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04