Java HttpServletResponse響應(yīng)實(shí)現(xiàn)過程詳解
用戶在客戶端輸入網(wǎng)址(虛擬路徑)時(shí),開始發(fā)送一個(gè)HTTP請(qǐng)求(請(qǐng)求行、請(qǐng)求頭、請(qǐng)求體)至服務(wù)器。服務(wù)器內(nèi)的Tomcat引擎會(huì)解析請(qǐng)求的地址,去找XML文件,然后根據(jù)虛擬路徑找Servlet的真實(shí)路徑,真實(shí)的Servlet會(huì)將請(qǐng)求的信息封裝成request(請(qǐng)求)對(duì)象,然后再創(chuàng)建一個(gè)response(響應(yīng))對(duì)象,(此時(shí)的response內(nèi)是空的)同時(shí)創(chuàng)建servlet對(duì)象,并調(diào)用service方法(或doGet和doPost方法)。
這樣就是把兩個(gè)對(duì)象傳給了服務(wù)器內(nèi)的某個(gè)servlet的service方法,通過這個(gè)方法,我們可以獲得request的所有的信息,并且向response內(nèi)設(shè)置信息。response.getwriter().write()將內(nèi)容寫到response的緩沖區(qū),這樣service方法結(jié)束了,方法返回后,tomcat引擎會(huì)將從該response緩沖區(qū)中獲取的設(shè)置信息封裝成一個(gè)HTTP響應(yīng)(響應(yīng)行、響應(yīng)頭、響應(yīng)體),發(fā)送給客戶端??蛻舳私馕鲰憫?yīng)回來的東西繼而進(jìn)行顯示。
概述:
我們?cè)趧?chuàng)建Servlet時(shí)會(huì)覆蓋service()方法,或doGet()/doPost(),這些方法都有兩個(gè)參數(shù),一個(gè)為代表請(qǐng)求的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è)置定時(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è)置定時(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>
恭喜您,注冊(cè)成功!
<span id="second" style="color:red">5</span>
秒后跳轉(zhuǎn),如沒跳轉(zhuǎn),請(qǐng)點(diǎn)擊<a rel="external nofollow" rel="external nofollow" >這里</a>
</body>
</html>
重定向:(請(qǐng)求服務(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);
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
深入Spring Boot之ClassLoader的繼承關(guān)系和影響
這篇文章主要介紹了深入Spring Boot之ClassLoader的繼承關(guān)系和影響,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-06-06
學(xué)習(xí)Java之如何正確地向上轉(zhuǎn)型與向下轉(zhuǎn)型
面向?qū)ο蟮牡谌齻€(gè)特征是多態(tài),實(shí)現(xiàn)多態(tài)有三個(gè)必要條件:繼承、方法重寫和向上轉(zhuǎn)型,在學(xué)習(xí)多態(tài)之前,我們還要先學(xué)習(xí)Java的類型轉(zhuǎn)換,本篇文章就來帶大家認(rèn)識(shí)什么是類型轉(zhuǎn)換,看看類型轉(zhuǎn)換都有哪幾種情況,以及如何避免類型轉(zhuǎn)換時(shí)出現(xiàn)異常2023-05-05
Spring Boot 實(shí)現(xiàn)https ssl免密登錄(X.509 pki登錄)
這篇文章主要介紹了Spring Boot 實(shí)現(xiàn)https ssl免密登錄(X.509 pki登錄),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
java:程序包org.bouncycastle.jce.provider不存在問題及解決
這篇文章主要介紹了java:程序包org.bouncycastle.jce.provider不存在問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05
IntelliJ IDEA中Project與Module的概念以及區(qū)別
這篇文章主要給大家介紹了關(guān)于IntelliJ IDEA中Project與Module的概念以及區(qū)別的相關(guān)資料,文中通過實(shí)例介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
IntelliJ IDEA 關(guān)閉多余項(xiàng)目的操作方法
這篇文章主要介紹了IntelliJ IDEA 關(guān)閉多余項(xiàng)目的操作方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04

