Java HttpServletResponse響應(yīng)實(shí)現(xiàn)過(guò)程詳解
用戶在客戶端輸入網(wǎng)址(虛擬路徑)時(shí),開(kāi)始發(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方法,通過(guò)這個(gè)方法,我們可以獲得request的所有的信息,并且向response內(nèi)設(shè)置信息。response.getwriter().write()將內(nèi)容寫(xiě)到response的緩沖區(qū),這樣service方法結(jié)束了,方法返回后,tomcat引擎會(huì)將從該response緩沖區(qū)中獲取的設(shè)置信息封裝成一個(gè)HTTP響應(yīng)(響應(yīng)行、響應(yīng)頭、響應(yīng)體),發(fā)送給客戶端。客戶端解析響應(yīng)回來(lái)的東西繼而進(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)大
通過(guò)response 設(shè)置響應(yīng)行:
設(shè)置響應(yīng)行的狀態(tài)碼:setStatus( int sc)
通過(guò)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 style='color:red'>5</span> 秒后跳轉(zhuǎn),如沒(méi)跳轉(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í)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. PHP設(shè)計(jì)模式(四)原型模式Prototype實(shí)例詳解【創(chuàng)建型】2. SpringBoot 開(kāi)發(fā)提速神器 Lombok+MybatisPlus+SwaggerUI3. python 爬取嗶哩嗶哩up主信息和投稿視頻4. Java向Runnable線程傳遞參數(shù)方法實(shí)例解析5. Python編寫(xiě)nmap掃描工具6. PHP擴(kuò)展之APC——Alternative PHP Cache(可選PHP緩存)7. Java 基于UDP協(xié)議實(shí)現(xiàn)消息發(fā)送8. Android里巧妙實(shí)現(xiàn)緩存9. php5.6不能擴(kuò)展redis.so的解決方法10. 10個(gè)提供免費(fèi)PHP腳本下載的網(wǎng)站
