久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Java HttpServletResponse響應(yīng)實(shí)現(xiàn)過(guò)程詳解

瀏覽:2日期:2022-09-01 09:54:13

 用戶在客戶端輸入網(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)。

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 太和县| 瑞昌市| 天台县| 襄汾县| 白朗县| 武冈市| 福州市| 日土县| 子长县| 蒙自县| 遵义市| 临夏市| 剑河县| 伊金霍洛旗| 建湖县| 辽中县| 房山区| 丰顺县| 敦化市| 漾濞| 平原县| 东港市| 甘德县| 罗江县| 连城县| 洛扎县| 锡林郭勒盟| 石家庄市| 萨嘎县| 永寿县| 麻栗坡县| 诸暨市| 湖口县| 永修县| 临泉县| 固安县| 岳西县| 长海县| 南投县| 宜良县| 休宁县|