日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java结丹期(13)----javaweb(responserequestservletcontext)

發(fā)布時間:2025/3/21 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java结丹期(13)----javaweb(responserequestservletcontext) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Request

? ? 1. request對象和response對象的原理
?? ??? ?1. request和response對象是由服務(wù)器創(chuàng)建的。我們來使用它們
?? ??? ?2. request對象是來獲取請求消息,response對象是來設(shè)置響應(yīng)消息
?? ?
?? ?2. request對象繼承體系結(jié)構(gòu):?? ?
?? ??? ?ServletRequest?? ??? ?--?? ?接口
?? ??? ??? ?|?? ?繼承
?? ??? ?HttpServletRequest?? ?-- 接口
?? ??? ??? ?|?? ?實現(xiàn)
?? ??? ?org.apache.catalina.connector.RequestFacade 類(tomcat)

?? ?3. request功能:
?? ??? ?1. 獲取請求消息數(shù)據(jù)
?? ??? ??? ?1. 獲取請求行數(shù)據(jù)
?? ??? ??? ??? ?* GET /day14/demo1?name=zhangsan HTTP/1.1
?? ??? ??? ??? ?* 方法:
?? ??? ??? ??? ??? ?1. 獲取請求方式 :GET
?? ??? ??? ??? ??? ??? ?* String getMethod() ?
?? ??? ??? ??? ??? ?2. (*)獲取虛擬目錄:/day14
?? ??? ??? ??? ??? ??? ?* String getContextPath()
?? ??? ??? ??? ??? ?3. 獲取Servlet路徑: /demo1
?? ??? ??? ??? ??? ??? ?* String getServletPath()
?? ??? ??? ??? ??? ?4. 獲取get方式請求參數(shù):name=zhangsan
?? ??? ??? ??? ??? ??? ?* String getQueryString()
?? ??? ??? ??? ??? ?5. (*)獲取請求URI:/day14/demo1
?? ??? ??? ??? ??? ??? ?* String getRequestURI():?? ??? ?/day14/demo1
?? ??? ??? ??? ??? ??? ?* StringBuffer getRequestURL() ?:http://localhost/day14/demo1

?? ??? ??? ??? ??? ??? ?* URL:統(tǒng)一資源定位符 : http://localhost/day14/demo1? ??
?? ??? ??? ??? ??? ??? ?* URI:統(tǒng)一資源標(biāo)識符 : /day14/demo1? ? ? ? ? ? ? ? ? ?
?? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ?6. 獲取協(xié)議及版本:HTTP/1.1
?? ??? ??? ??? ??? ??? ?* String getProtocol()

?? ??? ??? ??? ??? ?7. 獲取客戶機的IP地址:
?? ??? ??? ??? ??? ??? ?* String getRemoteAddr()
?? ??? ??? ??? ?

import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException;/*** 演示Request對象獲取請求行數(shù)據(jù)*/@WebServlet("/requestDemo1") public class RequestDemo1 extends HttpServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {/*1. 獲取請求方式 :GET* String getMethod()2. (*)獲取虛擬目錄:/day14* String getContextPath()3. 獲取Servlet路徑: /requestDemo1* String getServletPath()4. 獲取get方式請求參數(shù):name=zhangsan* String getQueryString()5. (*)獲取請求URI:/day14/demo1* String getRequestURI(): /day14/requestDemo1* StringBuffer getRequestURL() :http://localhost/day14/requestDemo16. 獲取協(xié)議及版本:HTTP/1.1* String getProtocol()7. 獲取客戶機的IP地址:* String getRemoteAddr()*///1. 獲取請求方式 :GETString method = request.getMethod();System.out.println(method);//2.(*)獲取虛擬目錄:/day14String contextPath = request.getContextPath();System.out.println(contextPath);//3. 獲取Servlet路徑: /demo1String servletPath = request.getServletPath();System.out.println(servletPath);//4. 獲取get方式請求參數(shù):name=zhangsanString queryString = request.getQueryString();System.out.println(queryString);//5.(*)獲取請求URI:/day14/demo1String requestURI = request.getRequestURI();StringBuffer requestURL = request.getRequestURL();System.out.println(requestURI);System.out.println(requestURL);//6. 獲取協(xié)議及版本:HTTP/1.1String protocol = request.getProtocol();System.out.println(protocol);//7. 獲取客戶機的IP地址:String remoteAddr = request.getRemoteAddr();System.out.println(remoteAddr);} }

?? ?
?? ??? ??? ?2. 獲取請求頭數(shù)據(jù)
?? ??? ??? ??? ?* 方法:
?? ??? ??? ??? ??? ?* (*)String getHeader(String name):通過請求頭的名稱獲取請求頭的值
?? ??? ??? ??? ??? ?* Enumeration<String> getHeaderNames():獲取所有的請求頭名稱
?? ??? ??? ??? ?
?? ??? ??? ?3. 獲取請求體數(shù)據(jù):
?? ??? ??? ??? ?* 請求體:只有POST請求方式,才有請求體,在請求體中封裝了POST請求的請求參數(shù)
?? ??? ??? ??? ?* 步驟:
?? ??? ??? ??? ??? ?1. 獲取流對象
?? ??? ??? ??? ??? ??? ?* ?BufferedReader getReader():獲取字符輸入流,只能操作字符數(shù)據(jù)
?? ??? ??? ??? ??? ??? ?* ?ServletInputStream getInputStream():獲取字節(jié)輸入流,可以操作所有類型數(shù)據(jù)
?? ??? ??? ??? ??? ??? ??? ?* 在文件上傳知識點后講解

?? ??? ??? ??? ??? ?2. 再從流對象中拿數(shù)據(jù)
?? ??? ??? ??? ?
?? ??? ??? ??? ?
?? ??? ?2. 其他功能:
?? ??? ??? ?1. 獲取請求參數(shù)通用方式:不論get還是post請求方式都可以使用下列方法來獲取請求參數(shù)
?? ??? ??? ??? ?1. String getParameter(String name):根據(jù)參數(shù)名稱獲取參數(shù)值 ? ?username=zs&password=123
?? ??? ??? ??? ?2. String[] getParameterValues(String name):根據(jù)參數(shù)名稱獲取參數(shù)值的數(shù)組 ?hobby=xx&hobby=game
?? ??? ??? ??? ?3. Enumeration<String> getParameterNames():獲取所有請求的參數(shù)名稱
?? ??? ??? ??? ?4. Map<String,String[]> getParameterMap():獲取所有參數(shù)的map集合

?? ??? ??? ??? ?* 中文亂碼問題:
?? ??? ??? ??? ??? ?* get方式:tomcat 8 已經(jīng)將get方式亂碼問題解決了
?? ??? ??? ??? ??? ?* post方式:會亂碼
?? ??? ??? ??? ??? ??? ?* 解決:在獲取參數(shù)前,設(shè)置request的編碼request.setCharacterEncoding("utf-8");
?? ??? ??? ?

?? ??? ??? ??? ??? ?
?? ??? ??? ?2. 請求轉(zhuǎn)發(fā):一種在服務(wù)器內(nèi)部的資源跳轉(zhuǎn)方式
?? ??? ??? ??? ?1. 步驟:
?? ??? ??? ??? ??? ?1. 通過request對象獲取請求轉(zhuǎn)發(fā)器對象:RequestDispatcher getRequestDispatcher(String path)
?? ??? ??? ??? ??? ?2. 使用RequestDispatcher對象來進行轉(zhuǎn)發(fā):forward(ServletRequest request, ServletResponse response)?

?? ??? ??? ??? ?2. 特點:
?? ??? ??? ??? ??? ?1. 瀏覽器地址欄路徑不發(fā)生變化
?? ??? ??? ??? ??? ?2. 只能轉(zhuǎn)發(fā)到當(dāng)前服務(wù)器內(nèi)部資源中。
?? ??? ??? ??? ??? ?3. 轉(zhuǎn)發(fā)是一次請求

import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException;@WebServlet("/requestDemo8") public class RequestDemo8 extends HttpServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {System.out.println("demo8888被訪問了。。。");//轉(zhuǎn)發(fā)到demo9資源 /*RequestDispatcher requestDispatcher = request.getRequestDispatcher("/requestDemo9");requestDispatcher.forward(request,response);*///存儲數(shù)據(jù)到request域中request.setAttribute("msg","hello");request.getRequestDispatcher("/requestDemo9").forward(request,response);//request.getRequestDispatcher("http://www.baidu.com").forward(request,response);}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doPost(request,response);} }


?? ??? ??? ?3. 共享數(shù)據(jù):
?? ??? ??? ??? ?* 域?qū)ο?#xff1a;一個有作用范圍的對象,可以在范圍內(nèi)共享數(shù)據(jù)
?? ??? ??? ??? ?* request域:代表一次請求的范圍,一般用于請求轉(zhuǎn)發(fā)的多個資源中共享數(shù)據(jù)
?? ??? ??? ??? ?* 方法:
?? ??? ??? ??? ??? ?1. void setAttribute(String name,Object obj):存儲數(shù)據(jù)
?? ??? ??? ??? ??? ?2. Object getAttitude(String name):通過鍵獲取值
?? ??? ??? ??? ??? ?3. void removeAttribute(String name):通過鍵移除鍵值對

?? ??? ??? ?4. 獲取ServletContext:
?? ??? ??? ??? ?* ServletContext getServletContext()
?? ??? ??? ?

?

?

Response對象

* 功能:設(shè)置響應(yīng)消息
1. 設(shè)置響應(yīng)行
?? ?1. 格式:HTTP/1.1 200 ok
?? ?2. 設(shè)置狀態(tài)碼:setStatus(int sc)?
2. 設(shè)置響應(yīng)頭:setHeader(String name, String value)?
?? ?
3. 設(shè)置響應(yīng)體:
?? ?* 使用步驟:
?? ??? ?1. 獲取輸出流
?? ??? ??? ?* 字符輸出流:PrintWriter getWriter()

?? ??? ??? ?* 字節(jié)輸出流:ServletOutputStream getOutputStream()

?? ??? ?2. 使用輸出流,將數(shù)據(jù)輸出到客戶端瀏覽器


* 案例:
1. 完成重定向
?? ?* 重定向:資源跳轉(zhuǎn)的方式
?? ?* 代碼實現(xiàn):
?? ??? ?//1. 設(shè)置狀態(tài)碼為302
?? ??? ?response.setStatus(302);
?? ??? ?//2.設(shè)置響應(yīng)頭location
?? ??? ?response.setHeader("location","/day15/responseDemo2");
?? ??? ?//簡單的重定向方法
?? ??? ?response.sendRedirect("/day15/responseDemo2");

import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException;/*** 重定向*/@WebServlet("/responseDemo1") public class ResponseDemo1 extends HttpServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {System.out.println("demo1........");//訪問/responseDemo1,會自動跳轉(zhuǎn)到/responseDemo2資源/* //1. 設(shè)置狀態(tài)碼為302response.setStatus(302);//2.設(shè)置響應(yīng)頭locationresponse.setHeader("location","/day15/responseDemo2");*/request.setAttribute("msg","response");//動態(tài)獲取虛擬目錄String contextPath = request.getContextPath();//簡單的重定向方法response.sendRedirect(contextPath+"/responseDemo2");}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doPost(request,response);} }

* forward 和 ?redirect 區(qū)別?

* 重定向的特點:redirect
?? ??? ?1. 地址欄發(fā)生變化
?? ??? ?2. 重定向可以訪問其他站點(服務(wù)器)的資源
(關(guān)鍵)
?? ??? ?3. 重定向是兩次請求。不能使用request對象來共享數(shù)據(jù)(關(guān)鍵)
?? ?* 轉(zhuǎn)發(fā)的特點:forward
?? ??? ?1. 轉(zhuǎn)發(fā)地址欄路徑不變
?? ??? ?2. 轉(zhuǎn)發(fā)只能訪問當(dāng)前服務(wù)器下的資源
(關(guān)鍵)
?? ??? ?3. 轉(zhuǎn)發(fā)是一次請求,可以使用request對象來共享數(shù)據(jù)(關(guān)鍵)
?? ??
?? ?* 路徑寫法:
?? ??? ?1. 路徑分類
?? ??? ??? ?1. 相對路徑:通過相對路徑不可以確定唯一資源
?? ??? ??? ??? ?* 如:./index.html
?? ??? ??? ??? ?* 不以/開頭,以.開頭路徑

?? ??? ??? ??? ?* 規(guī)則:找到當(dāng)前資源和目標(biāo)資源之間的相對位置關(guān)系
?? ??? ??? ??? ??? ?* ./:當(dāng)前目錄
?? ??? ??? ??? ??? ?* ../:后退一級目錄
?? ??? ??? ?2. 絕對路徑:通過絕對路徑可以確定唯一資源
?? ??? ??? ??? ?* 如:http://localhost/day15/responseDemo2?? ??? ?/day15/responseDemo2
?? ??? ??? ??? ?* 以/開頭的路徑

?? ??? ??? ??? ?* 規(guī)則:判斷定義的路徑是給誰用的?判斷請求將來從哪兒發(fā)出
?? ??? ??? ??? ??? ?* 給客戶端瀏覽器使用:需要加虛擬目錄(項目的訪問路徑)
?? ??? ??? ??? ??? ??? ?* 建議虛擬目錄動態(tài)獲取:request.getContextPath()
?? ??? ??? ??? ??? ??? ?* <a> , <form> 重定向...

?? ??? ??? ??? ??? ?* 給服務(wù)器使用:不需要加虛擬目錄
?? ??? ??? ??? ??? ??? ?* 轉(zhuǎn)發(fā)路徑?

? ? ? ? ? ? ??

?2. 服務(wù)器輸出字符數(shù)據(jù)到瀏覽器
?? ?* 步驟:
?? ??? ?1. 獲取字符輸出流
?? ??? ?2. 輸出數(shù)據(jù)

?? ?* 注意:
?? ??? ?* 亂碼問題:
?? ??? ??? ?1. PrintWriter pw = response.getWriter();獲取的流的默認編碼是ISO-8859-1
?? ??? ??? ?2. 設(shè)置該流的默認編碼
?? ??? ??? ?3. 告訴瀏覽器響應(yīng)體使用的編碼

?? ??? ??? ?//簡單的形式,設(shè)置編碼,是在獲取流之前設(shè)置
?? ??? ??? ?response.setContentType("text/html;charset=utf-8");

import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter;@WebServlet("/responseDemo4") public class ResponseDemo4 extends HttpServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//獲取流對象之前,設(shè)置流的默認編碼:ISO-8859-1 設(shè)置為:GBK// response.setCharacterEncoding("utf-8");//告訴瀏覽器,服務(wù)器發(fā)送的消息體數(shù)據(jù)的編碼。建議瀏覽器使用該編碼解碼//response.setHeader("content-type","text/html;charset=utf-8");//簡單的形式,設(shè)置編碼response.setContentType("text/html;charset=utf-8");//1.獲取字符輸出流PrintWriter pw = response.getWriter();//2.輸出數(shù)據(jù)//pw.write("<h1>hello response</h1>");pw.write("你好啊啊啊 response");}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doPost(request,response);} }


3. 服務(wù)器輸出字節(jié)數(shù)據(jù)到瀏覽器
?? ?* 步驟:
?? ??? ?1. 獲取字節(jié)輸出流
?? ??? ?2. 輸出數(shù)據(jù)

?

4. 驗證碼
?? ?1. 本質(zhì):圖片
?? ?2. 目的:防止惡意表單注冊

import javax.imageio.ImageIO; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.awt.*; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.Random;@WebServlet("/checkCodeServlet") public class CheckCodeServlet extends HttpServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {int width = 100;int height = 50;//1.創(chuàng)建一對象,在內(nèi)存中圖片(驗證碼圖片對象)BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);//2.美化圖片//2.1 填充背景色Graphics g = image.getGraphics();//畫筆對象g.setColor(Color.PINK);//設(shè)置畫筆顏色g.fillRect(0,0,width,height);//2.2畫邊框g.setColor(Color.BLUE);g.drawRect(0,0,width - 1,height - 1);String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghigklmnopqrstuvwxyz0123456789";//生成隨機角標(biāo)Random ran = new Random();for (int i = 1; i <= 4; i++) {int index = ran.nextInt(str.length());//獲取字符char ch = str.charAt(index);//隨機字符//2.3寫驗證碼g.drawString(ch+"",width/5*i,height/2);}//2.4畫干擾線g.setColor(Color.GREEN);//隨機生成坐標(biāo)點for (int i = 0; i < 10; i++) {int x1 = ran.nextInt(width);int x2 = ran.nextInt(width);int y1 = ran.nextInt(height);int y2 = ran.nextInt(height);g.drawLine(x1,y1,x2,y2);}//3.將圖片輸出到頁面展示ImageIO.write(image,"jpg",response.getOutputStream());}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doPost(request,response);} }

ServletContext對象:

1. 概念:代表整個web應(yīng)用,可以和程序的容器(服務(wù)器)來通信
2. 獲取:
?? ?1. 通過request對象獲取
?? ??? ?request.getServletContext();
?? ?2. 通過HttpServlet獲取
?? ??? ?this.getServletContext();

3. 功能:
?? ?1. 獲取MIME類型:
?? ??? ?* MIME類型:在互聯(lián)網(wǎng)通信過程中定義的一種文件數(shù)據(jù)類型

?? ??? ??? ?* 格式: 大類型/小類型 ? text/html?? ??? ?image/jpeg

?? ??? ?* 獲取:String getMimeType(String file) ?
?? ?2. 域?qū)ο?#xff1a;共享數(shù)據(jù)
?? ??? ?1. setAttribute(String name,Object value)
?? ??? ?2. getAttribute(String name)
?? ??? ?3. removeAttribute(String name)

?? ??? ?* ServletContext對象范圍:所有用戶所有請求的數(shù)據(jù)
?? ?3. 獲取文件的真實(服務(wù)器)路徑
?? ??? ?1. 方法:String getRealPath(String path) ?
?? ??? ???

? String b = context.getRealPath("/b.txt");//web目錄下資源訪問System.out.println(b);String c = context.getRealPath("/WEB-INF/c.txt");//WEB-INF目錄下的資源訪問System.out.println(c);String a = context.getRealPath("/WEB-INF/classes/a.txt");//src目錄下的資源訪問System.out.println(a);


?

總結(jié)

以上是生活随笔為你收集整理的java结丹期(13)----javaweb(responserequestservletcontext)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。