標籤: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協議資訊