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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

请求对象Request

發布時間:2023/12/3 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 请求对象Request 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.概念

<1>請求:請求:獲取資源。在BS架構中,就是客戶端瀏覽器服務器端發出詢問。
<2>請求對象:就是在項目當中用于發送請求的對象。
<3>對象創建:需要實現ServletRequest和HttpservletRequest接口,不過在tomcat底層已經幫我們創建好了請求對象。

關系視圖如下:

2.Request獲取路徑的相關方法

注:瀏覽器返回【本機地址】ipv6和ipv4的地址是本地host文件控制

//Request獲取路徑的相關方法 @WebServlet("/ServletDemo1") public class ServletDemo1 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//1.獲取虛擬目錄名稱getContextPath()String contextPath = req.getContextPath();System.out.println(contextPath);//2.獲取Servlet映射路徑getServletPath()String servletPath = req.getServletPath();System.out.println(servletPath);//3.獲取訪問者ip getRemoteAddr()String remoteAddr = req.getRemoteAddr();//http://127.0.0.1:8080 //127.0.0.1//http://localhost:8080[和host文件的配置有關] //0:0:0:0:0:0:0:1System.out.println(remoteAddr);//4.獲取請求消息的數據getQueryString()//?username=zhangsan&password=123456String queryString = req.getQueryString();System.out.println(queryString);//5.獲取統一資源標識符 getRequestURI()String requestURI = req.getRequestURI();System.out.println(requestURI);//URL更精確,表示范圍比URI小//6.獲取統一資源定位符 getRequestURL()StringBuffer requestURL = req.getRequestURL();System.out.println(requestURL);//瀏覽器訪問http://127.0.0.1:8080/ServletRequest/ServletDemo1?username=zhangsan&password=123456}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);} }打印結果: ----------------------------------------------------------------------- /ServletRequest /ServletDemo1 127.0.0.1 null /ServletRequest/ServletDemo1 http://127.0.0.1:8080/ServletRequest/ServletDemo1
3.Request獲取請求頭內信息的相關方法
//Request獲取請求頭內信息的相關方法 @WebServlet("/ServletDemo2") public class ServletDemo2 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//請求頭名稱[可以有多個值]不區分大小寫//1.getHeader("請求頭名稱")根據請求頭名稱獲取值String connection = req.getHeader("Connection");System.out.println(connection);System.out.println("------------------------");//返回類型和前面不同,返回為集合//2.getHeader("請求頭名稱")根據請求頭名稱[有多個值]獲取多個值String AcceptEncoding = req.getHeader("Accept-Encoding");System.out.println(AcceptEncoding);System.out.println("------------------------");//3.getHeaderNames獲取所有的請求頭名稱Enumeration<String> headerNames = req.getHeaderNames();while (headerNames.hasMoreElements()) {String headerName = headerNames.nextElement();String value = req.getHeader("s");System.out.println(headerName + "..." + value);}}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);} }打印結果: ---------------------------------------------------------------------- keep-alive ------------------------ gzip, deflate ------------------------ host...null user-agent...null accept...null accept-language...null accept-encoding...null connection...null cookie...null upgrade-insecure-requests...null sec-fetch-dest...null sec-fetch-mode...null sec-fetch-site...null sec-fetch-user...null
4.獲取請求參數相關信息的方法

表單屬性為【form action="/ServletRequest/ServletDemo3" method=“post” autocomplete=“off”】

@WebServlet("/ServletDemo3") public class ServletDemo3 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//1.getParameter()根據名稱獲取數據String value = req.getParameter("name");System.out.println(value);System.out.println("-------------------------");//2.getParameterValues()根據名稱獲取所有數據String[] hobbies = req.getParameterValues("hobby");System.out.println(Arrays.toString(hobbies));/*for (String hobby : hobbies) {System.out.println(hobby);}*/System.out.println("-------------------------");//3.getParameterNames()獲取所有名稱Enumeration<String> parameterNames = req.getParameterNames();while (parameterNames.hasMoreElements()){String s = parameterNames.nextElement();System.out.println(s);}System.out.println("-------------------------");//4.getParameterMap()獲取所有參數的鍵值對,存入Map集合Map<String, String[]> parameterMap = req.getParameterMap();Set<String> keys = parameterMap.keySet();for (String key : keys) {String[] values = parameterMap.get(key);System.out.println(key + Arrays.toString(values));}}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);} }打印結果: ---------------------------------------------------------------------- zhangsan ------------------------- [study, game, book] ------------------------- name password hobby ------------------------- name[zhangsan] password[123456] hobby[study, game, book
5.通過流對象獲取數據

【請求方法必須是post方式】
表單屬性為【form action="/ServletRequest/ServletDemo7" method=“post” autocomplete=“off”】

//通過流對象獲取數據[請求方法必須是post方式] //不是自己new的都不需要釋放資源 @WebServlet("/ServletDemo7") public class ServletDemo7 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//字符流/*BufferedReader br = req.getReader();String line = br.readLine();while (line != null) {System.out.println(line);line = br.readLine();}*///字節流[應用場景,上傳文件]ServletInputStream sis = req.getInputStream();byte[] bytes = new byte[1024];int len = sis.read(bytes);while (len!=-1){System.out.println(new String(bytes,0,len));len = sis.read(bytes);}}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);} }打印結果: --------------------------------------------------------------------- name=%E6%9D%8E%E5%9B%9B&password=123&hobby=game
6.請求轉發

表單屬性為【form action="/ServletRequest/ServletDemo9" method=“post” autocomplete=“off”】

//ServletDemo9 @WebServlet("/ServletDemo9") public class ServletDemo9 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//設置請求字符編碼,防止亂碼req.setCharacterEncoding("utf-8");//設置共享數據setAttribute(鍵,值)System.out.println("ServletDemo9設置共享數據");req.setAttribute("name","zhangsan");//getRequestDispatcher()獲取請求調度對象[中介]RequestDispatcher rd = req.getRequestDispatcher("/ServletDemo10");//實現轉發功能[參數為請求對象和相應對象]rd.forward(req,resp);//所以ServletDemo9中接收的請求參數也會傳遞到10中}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);} }//ServletDemo10 @WebServlet("/ServletDemo10") public class ServletDemo10 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//設置請求字符編碼,防止亂碼req.setCharacterEncoding("utf-8");//獲取共享數據System.out.println("ServletDemo10獲取共享數據");Object name = req.getAttribute("name");System.out.println(name);System.out.println("獲取ServletDemo7的請求參數");System.out.println(req.getParameter("name"));}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);} }打印結果: ---------------------------------------------------------------------- ServletDemo9設置共享數據 ServletDemo10獲取共享數據 zhangsan 獲取ServletDemo7的請求參數 王五

總結

以上是生活随笔為你收集整理的请求对象Request的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。