国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

java:詳解http模塊request對象

這篇具有很好參考價值的文章主要介紹了java:詳解http模塊request對象。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

背景

在 Java 中,HTTP 模塊的 request 對象和 response 對象分別表示客戶端向服務器發(fā)送請求和服務器向客戶端發(fā)送響應時使用的對象。

繼承關系

ServletRequest		--	接口
	|   繼承
HttpServletRequest	-- 接口
	|	實現(xiàn)
org.apache.catalina.connector.RequestFacade 類(tomcat)

獲取數(shù)據(jù)request

request 對象包含有關客戶端請求的信息,例如請求方法、請求 URL、請求頭、請求體等,其中常用的屬性和方法包括:

獲取請求行數(shù)據(jù)

  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)一資源標識符 : /day14/demo1

  6. 獲取協(xié)議及版本:HTTP/1.1

    • String getProtocol()
  7. 獲取客戶機的IP地址:

    • String getRemoteAddr()

以下是一個簡單的 Java Servlet 示例,演示如何使用 request 對象處理 HTTP 請求和響應:

package cn.xxx.web.request;

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/requestDemo1
            6. 獲取協(xié)議及版本:HTTP/1.1
                * String getProtocol()

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

         */
        //1. 獲取請求方式 :GET
        String method = request.getMethod();
        System.out.println(method);
        //2.(*)獲取虛擬目錄:/day14
        String contextPath = request.getContextPath();
        System.out.println(contextPath);
        //3. 獲取Servlet路徑: /demo1
        String servletPath = request.getServletPath();
        System.out.println(servletPath);
        //4. 獲取get方式請求參數(shù):name=zhangsan
        String queryString = request.getQueryString();
        System.out.println(queryString);
        //5.(*)獲取請求URI:/day14/demo1
        String requestURI = request.getRequestURI();
        StringBuffer requestURL = request.getRequestURL();
        System.out.println(requestURI);
        System.out.println(requestURL);
        //6. 獲取協(xié)議及版本:HTTP/1.1
        String protocol = request.getProtocol();
        System.out.println(protocol);
        //7. 獲取客戶機的IP地址:
        String remoteAddr = request.getRemoteAddr();
        System.out.println(remoteAddr);
    }
}

獲取請求頭數(shù)據(jù)

  • (*)String getHeader(String name):通過請求頭的名稱獲取請求頭的值
  • Enumeration<String> getHeaderNames():獲取所有的請求頭名稱
package cn.xxx.web.request;

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.util.Enumeration;

@WebServlet("/requestDemo2")
public class RequestDemo2 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //演示獲取請求頭數(shù)據(jù)
        
        //1.獲取所有請求頭名稱
        Enumeration<String> headerNames = request.getHeaderNames();
        //2.遍歷
        while(headerNames.hasMoreElements()){
            String name = headerNames.nextElement();
            //根據(jù)名稱獲取請求頭的值
            String value = request.getHeader(name);
            System.out.println(name+"---"+value);
        }

    }
}

package cn.xxx.web.request;

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.util.Enumeration;

@WebServlet("/requestDemo3")
public class RequestDemo3 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //演示獲取請求頭數(shù)據(jù):user-agent

        String agent = request.getHeader("user-agent");
        //判斷agent的瀏覽器版本
        if(agent.contains("Chrome")){
            //谷歌
            System.out.println("谷歌來了...");
        }else if(agent.contains("Firefox")){
            //火狐
            System.out.println("火狐來了...");
        }

    }
}

獲取請求體數(shù)據(jù)

只有POST請求方式,才有請求體,在請求體中封裝了POST請求的請求參數(shù)。

  1. 獲取流對象
    * BufferedReader getReader():獲取字符輸入流,只能操作字符數(shù)據(jù)

    • ServletInputStream getInputStream():獲取字節(jié)輸入流,可以操作所有類型數(shù)據(jù)(在文件上傳知識點后講解)
  2. 再從流對象中拿數(shù)據(jù)

package cn.xxx.web.request;

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.BufferedReader;
import java.io.IOException;

@WebServlet("/requestDemo5")
public class RequestDemo5 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //獲取請求消息體--請求參數(shù)

        //1.獲取字符流
        BufferedReader br = request.getReader();
        //2.讀取數(shù)據(jù)
        String line = null;
        while((line = br.readLine()) != null){
            System.out.println(line);
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
}

示例:防盜鏈

package cn.xxx.web.request;

import javax.servlet.ServletContext;
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("/requestDemo4")
public class RequestDemo4 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //演示獲取請求頭數(shù)據(jù):referer

        String referer = request.getHeader("referer");
        System.out.println(referer);//http://localhost/day14/login.html

        //防盜鏈
        if(referer != null ){
            if(referer.contains("/day14")){
                //正常訪問
               // System.out.println("播放電影....");
                response.setContentType("text/html;charset=utf-8");
                response.getWriter().write("播放電影....");
            }else{
                //盜鏈
                //System.out.println("想看電影嗎?來優(yōu)酷吧...");
                response.setContentType("text/html;charset=utf-8");
                response.getWriter().write("想看電影嗎?來優(yōu)酷吧...");
            }
        }

    }
}

其他功能

獲取請求參數(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 getParameterNames():獲取所有請求的參數(shù)名稱
  4. Map<String,String[]> getParameterMap():獲取所有參數(shù)的map集合
package cn.xxx.web.request;

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.BufferedReader;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;
import java.util.Set;

@WebServlet("/requestDemo6")
public class RequestDemo6 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //post 獲取請求參數(shù)

        //根據(jù)參數(shù)名稱獲取參數(shù)值
        String username = request.getParameter("username");
       /* System.out.println("post");
        System.out.println(username);*/

       //根據(jù)參數(shù)名稱獲取參數(shù)值的數(shù)組
        String[] hobbies = request.getParameterValues("hobby");
        /*for (String hobby : hobbies) {
            System.out.println(hobby);
        }*/

        //獲取所有請求的參數(shù)名稱

        Enumeration<String> parameterNames = request.getParameterNames();
        /*while(parameterNames.hasMoreElements()){
            String name = parameterNames.nextElement();
            System.out.println(name);
            String value = request.getParameter(name);
            System.out.println(value);
            System.out.println("----------------");
        }*/

        // 獲取所有參數(shù)的map集合
        Map<String, String[]> parameterMap = request.getParameterMap();
        //遍歷
        Set<String> keyset = parameterMap.keySet();
        for (String name : keyset) {
            
            //獲取鍵獲取值
            String[] values = parameterMap.get(name);
            System.out.println(name);
            for (String value : values) {
                System.out.println(value);
            }

            System.out.println("-----------------");
        }


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //get 獲取請求參數(shù)
/*
        //根據(jù)參數(shù)名稱獲取參數(shù)值
        String username = request.getParameter("username");
        System.out.println("get");
        System.out.println(username);*/

        this.doPost(request,response);
    }
}

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

package cn.xxx.web.request;

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.util.Enumeration;
import java.util.Map;
import java.util.Set;

@WebServlet("/requestDemo7")
public class RequestDemo7 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.設置流的編碼
        request.setCharacterEncoding("utf-8");

        //獲取請求參數(shù)username
        String username = request.getParameter("username");

        System.out.println(username);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        this.doPost(request,response);
    }
}

請求轉(zhuǎn)發(fā)

一種在服務器內(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ā)到當前服務器內(nèi)部資源中。
    3. 轉(zhuǎn)發(fā)是一次請求
package cn.xxx.web.request;

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.xxx.cn").forward(request,response);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        this.doPost(request,response);
    }
}

共享數(shù)據(jù)

  • 域?qū)ο螅阂粋€有作用范圍的對象,可以在范圍內(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):通過鍵移除鍵值對
package cn.xxx.web.request;

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("/requestDemo9")
public class RequestDemo9 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //獲取數(shù)據(jù)
        Object msg = request.getAttribute("msg");
        System.out.println(msg);

        System.out.println("demo9999被訪問了。。。");

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        this.doPost(request,response);
    }
}

獲取ServletContext

ServletContext getServletContext()文章來源地址http://www.zghlxwxcb.cn/news/detail-689044.html

package cn.xxx.web.request;

import javax.servlet.ServletContext;
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("/requestDemo10")
public class RequestDemo10 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        ServletContext servletContext = request.getServletContext();

        System.out.println(servletContext);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        this.doPost(request,response);
    }
}

到了這里,關于java:詳解http模塊request對象的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權(quán),不承擔相關法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領支付寶紅包贊助服務器費用

相關文章

  • HTTP請求:requests模塊基礎使用必知必會

    HTTP請求:requests模塊基礎使用必知必會

    http請求是常見的一種網(wǎng)頁協(xié)議,我們看到的各種網(wǎng)頁,其實都是發(fā)送了http請求得到了服務器的響應,從而將數(shù)據(jù)庫中復雜的數(shù)據(jù)以簡單、直觀的方式呈現(xiàn)出來,方便大眾閱讀、使用。而如何發(fā)送http請求呢?今天來探討一下使用requests模塊,達到高效、簡單的http請求操作。

    2024年02月09日
    瀏覽(14)
  • Servlet API中使用Request對象獲取HTTP協(xié)議請求內(nèi)容

    Servlet API中使用Request對象獲取HTTP協(xié)議請求內(nèi)容

    在Servlet API中,定義了一個HttpServletRequest接口,它繼承自ServletRequest接口,專門 用來封裝HTTP請求消息。 由于HTTP請求消息分為請求行、請求頭和請求體三部分,因此,在HttpServletRequest接口中定義了獲取請求行、請求頭和請求消息體的相關方法. Web服務器【tomcat】收到客戶端的

    2024年02月11日
    瀏覽(19)
  • Nginx ngx_http_auth_request_module模塊鑒權(quán)

    Nginx ngx_http_auth_request_module模塊鑒權(quán)

    ngx_http_auth_request_module 模塊 實現(xiàn)了基于一子請求的結(jié)果的客戶端的授權(quán)。如果子請求返回2xx響應碼,則允許訪問。如果它返回401或403,則訪問被拒絕并顯示相應的錯誤代碼。子請求返回的任何其他響應代碼都被認為是錯誤的。 auth_request 使用的也是 subrequest 進行子請求。 當我

    2024年02月17日
    瀏覽(19)
  • 【Go 基礎篇】Go語言包詳解:模塊化開發(fā)與代碼復用

    在Go語言中, 包(Package) 是一種用于組織代碼的機制,用于將相關的函數(shù)、類型和變量等組織在一起,以便于模塊化開發(fā)和代碼復用。包的使用能夠使程序結(jié)構(gòu)更加清晰、可維護性更高,同時也是Go語言強調(diào)的一項重要特性。本篇博客將深入探討Go語言中包的相關知識,包括

    2024年02月11日
    瀏覽(88)
  • python—requests模塊詳解

    python—requests模塊詳解

    1、requests簡介 requests是一個很實用的Python HTTP客戶端庫,爬蟲和測試服務器響應數(shù)據(jù)時經(jīng)常會用到,它是python語言的第三方的庫,專門用于發(fā)送HTTP請求,使用起來比urllib更簡潔也更強大。 2、requests庫的安裝 方法1:命令行安裝 windows操作系統(tǒng):pip install requests Mac操作系統(tǒng):p

    2024年02月07日
    瀏覽(17)
  • HTTP--Request詳解

    HTTP--Request詳解

    請求消息數(shù)據(jù)格式 請求行 請求頭 客戶端瀏覽器告訴服務器一些信息 常見的請求頭: User-Agent:瀏覽器告訴服務器,我訪問你使用的瀏覽器版本信息 可以在服務器端獲取該頭的信息,解決瀏覽器的兼容性問題 Referer:http://localhost/login.html 告訴服務器,我(當前請求)從哪里來?

    2024年02月11日
    瀏覽(14)
  • 【網(wǎng)絡原理】HTTP 請求 (Request)詳解

    【網(wǎng)絡原理】HTTP 請求 (Request)詳解

    HTTP 請求報文由請求行、請求頭部、空行 和 請求包體 4 個部分組成 (我們這里學習HTTP可以用到博主在上一篇博客介紹的Fiddler抓包工具,獲取本機的http請求和響應) 本片文章將從以下四個方面對HTTP請求報文進行解析 URL 方法 請求報頭 正文 平時我們俗稱的 “網(wǎng)址” 其實就

    2024年03月27日
    瀏覽(18)
  • 一圖看懂 requests 模塊:用Python編寫、供人類使用的HTTP庫, 資料整理+筆記(大全)

    一圖看懂 requests 模塊:用Python編寫、供人類使用的HTTP庫, 資料整理+筆記(大全)

    本文由 大俠(AhcaoZhu)原創(chuàng),轉(zhuǎn)載請聲明。 鏈接: https://blog.csdn.net/Ahcao2008 全文介紹系統(tǒng)內(nèi)置 requests 模塊、函數(shù)、類及類的方法和屬性。 它通過代碼抓取并經(jīng)AI智能翻譯和人工校對。 是一部不可多得的權(quán)威字典類工具書。它是系列集的一部分。后續(xù)陸續(xù)發(fā)布、敬請關注?!驹瓌?chuàng)

    2024年02月06日
    瀏覽(24)
  • 接口自動化測試之Requests模塊詳解

    接口自動化測試之Requests模塊詳解

    Python中,系統(tǒng)自帶的urllib和urllib2都提供了功能強大的HTTP支持,但是API接口確實太難用了。Requests 作為更高一層的封裝,在大部分情況下對得起它的slogan——HTTP for Humans。 讓我們一起來看看 Requests 這個 HTTP庫在我們接口自動化測試中的使用吧 發(fā)送請求 在使用 Requests發(fā)送網(wǎng)絡

    2024年02月08日
    瀏覽(22)
  • python爬蟲——request模塊講解,Python詳解

    python爬蟲——request模塊講解,Python詳解

    對于GET方式的請求,瀏覽器會把http header和data一并發(fā)送出去,服務器響應200(返回數(shù)據(jù)); 而對于POST, 瀏覽器先發(fā)送header,服務器響應100 continue,瀏覽器再發(fā)送data,服務器響應200 ok(返回數(shù)據(jù))。 (二)http常見請求參數(shù) url:請求url地址 headers:請求頭 **data:發(fā)送編碼為表

    2024年04月26日
    瀏覽(18)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包