擷取HttpServletRequest請求資訊,HTTP協議資訊

來源:互聯網
上載者:User

標籤:cte   method   pat   logs   請求方式   oid   more   gzip   throws   

擷取HttpServletRequest請求資訊,HTTP協議資訊

經常用到,簡單記錄下…

/** * HttpServletRequest請求參數擷取測試 * @author Niu on 2017年7月17日 下午2:45:01 */@WebServlet("/dispense")public class DispenseServlet extends HttpServlet {    @Override    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {        // 擷取請求方式:GET        System.out.println("req.getMethod():" + req.getMethod());        // 擷取項目名稱:/dispense        System.out.println("req.getContextPath():" + req.getContextPath());        // 擷取完整請求路徑:http://localhost:8888/dispense/dispense        System.out.println("req.getRequestURL():" + req.getRequestURL());        // 擷取除了網域名稱外的請求資料:/dispense/dispense        System.out.println("req.getRequestURI():" + req.getRequestURI());        // 擷取請求參數:name=codingme.net        System.out.println("req.getQueryString():" + req.getQueryString());        // 擷取要求標頭:        System.out.println("req.getHeader(\"user-Agent\"):" + req.getHeader("user-Agent"));        System.out.println("--------------------------------------------------------");        // 擷取所有的訊息頭名稱        Enumeration<String> headerNames = req.getHeaderNames();        // 擷取擷取的訊息頭名稱,擷取對應的值,並輸出        while (headerNames.hasMoreElements()) {            String nextElement = headerNames.nextElement();            System.out.println(nextElement + ":" + req.getHeader(nextElement));        }        System.out.println("----------------------------------------------------------");        // 根據名稱擷取此重名的所有資料        System.out.println("req.getHeader(\"accept\"):" + req.getHeader("accept"));        // 擷取請求主機名稱        System.out.println("req.getHeader(\"host\"):" + req.getHeader("host"));    }    @Override    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {        doGet(req, resp);    }}

  

請求URL:

http://localhost:8888/dispense/dispense?name=codingme.net

輸出:

req.getMethod():GETreq.getContextPath():/dispensereq.getRequestURL():http://localhost:8888/dispense/dispensereq.getRequestURI():/dispense/dispensereq.getQueryString():name=codingme.netreq.getHeader("user-Agent"):Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3080.5 Safari/537.36--------------------------------------------------------host:localhost:8888connection:keep-alivecache-control:max-age=0upgrade-insecure-requests:1user-agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3080.5 Safari/537.36accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8accept-encoding:gzip, deflate, braccept-language:zh-CN,zh;q=0.8cookie:_ga=GA1.1.1003706294.1499565784; Hm_lvt_57ccbd5c600ed4e6bdb9458e666b6409=1499849256,1499853602,1499950574; Hm_lvt_1b51c3ea9a3e7b1a2bc55df97ab4efd3=1499952403----------------------------------------------------------req.getHeader("accept"):text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8req.getHeader("host"):localhost:8888

 

原文地址:http://blog.codingme.net/post/servlet-HttpServletRequest-http-info

擷取HttpServletRequest請求資訊,HTTP協議資訊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.