java中HttpServletRequest常用获取url、资源名等方法总结

HttpServletRequest客户端获取请求,客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中。所以我们能够从对象中获取相应信息
 
端口
request.getServerPort() ;
获取服务器名称
request.getContextPath();
获取工程名称
request.getContextPath();
获取servlet路径
request.getServletPath().substring(0,request.getServletPath().lastIndexOf("/"));
 
全路径: http://localhost:8080/iswustserver/iswust/library/borrow
request.getServerName();
System.out.println("ServerName:"+request.getServerName());
request.getServerPort();
System.out.println("ServerPort:"+request.getServerPort());
request.getContextPath();
System.out.println("ContextPath" + request.getContextPath());
request.getServletPath();
System.out.println("ServletPath" + request.getServletPath());
request.getRequestURI();
System.out.println("RequestURI" + request.getRequestURI());
request.getRequestURL();
System.out.println("RequestURL" + request.getRequestURL());
java中HttpServletRequest常用获取url、资源名等方法总结_第1张图片
 
java中HttpServletRequest常用获取url、资源名等方法总结_第2张图片
 
java中HttpServletRequest常用获取url、资源名等方法总结_第3张图片
 
 
/*String beanname = requestPath.substring(requestPath.indexOf("iswust/") + 7,
requestPath.indexOf("iswust/") + 11);*/

转载于:https://www.cnblogs.com/boycelee/p/5930257.html

你可能感兴趣的:(java中HttpServletRequest常用获取url、资源名等方法总结)