HttpServletRequest获取URL信息

//获取ip
System.out.println("getLocalAddr : " + request.getLocalAddr());
//获取访问路径
System.out.println("getRequestURI : " + request.getRequestURI());
//获取访问协议http
System.out.println("getScheme : " + request.getScheme());
//获取域名
System.out.println("getServerName : " + request.getServerName());
//获取端口号
System.out.println("getServerPort : " + request.getServerPort());
//获取项目名称
System.out.println("getContextPath : " + request.getContextPath());
//获取Servlet路径
System.out.println("getServletPath : " + request.getServletPath());
//获取参数
System.out.println("getQueryString : " + request.getQueryString());

例如一个名为demo的java web项目,访问路径:http://localhost:8080/demo/test?a=1

getLocalAddr : 127.0.0.1
getRequestURI : /demo/test
getScheme : http
getServerName : localhost
getServerPort : 7200
getContextPath : /demo
getServletPath : /test
getQueryString : a=1

你可能感兴趣的:(java,web)