Get HttpServletRequest Request information, HTTP protocol information
Often used, simply record ...
/** * HttpServletRequest Request parameter Get Test * @author Niu on July 17, 2017 pm 2:45:01 */@WebServlet ("/dispense") public class Dispenseser Vlet extends HttpServlet {@Override protected void doget (HttpServletRequest req, HttpServletResponse resp) throws S Ervletexception, IOException {//Get Request method: Get System.out.println ("Req.getmethod ():" + Req.getmethod ()); Get project Name:/dispense System.out.println ("Req.getcontextpath ():" + Req.getcontextpath ()); Get the full request path: Http://localhost:8888/dispense/dispense System.out.println ("Req.getrequesturl ():" + Req.getrequesturl ( )); Get request data except for domain name:/dispense/dispense System.out.println ("Req.getrequesturi ():" + Req.getrequesturi ()); Get request Parameters: Name=codingme.net System.out.println ("req.getquerystring ():" + req.getquerystring ()); Get Request Header: System.out.println ("Req.getheader (\" user-agent\ "):" + Req.getheader ("user-agent")); System.out.println ("--------------------------------------------------------"); Gets all the message header names enumeration<string> headernames = Req.getheadernames (); Gets the message header name obtained, gets the corresponding value, and outputs the while (Headernames.hasmoreelements ()) {String nextelement = Headernames.nextel Ement (); System.out.println (nextelement + ":" + Req.getheader (nextelement)); } System.out.println ("----------------------------------------------------------"); Get all data System.out.println ("Req.getheader (\" accept\): "+ Req.getheader (" accept ") for this name, according to the names; Get request host name System.out.println ("Req.getheader (\" host\ "):" + Req.getheader ("host")); } @Override protected void DoPost (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, Ioexcep tion {doget (req, resp); }}
Request URL:
Http://localhost:8888/dispense/dispense?name=codingme.net
Output:
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
Original address: Http://blog.codingme.net/post/servlet-HttpServletRequest-http-info
Get HttpServletRequest Request information, HTTP protocol information