Servlet中request的一些关于方法

我们知道:HttpServletRequest对象可以获取许多有用的信息,但如果你不去细究,到用的时候你就会很纠结,现在总结于下。

//获取请求的action的URL地址
StringBuffer requestURL =ServletActionContext.getRequest().getRequestURL(); System.out.println("requestURL-------->>" + requestURL);//结果:  http://localhost:8080/weatherweb2.0/pages/theme/theme_add.action

//获取URI地址,相对于当前主机 String requestURI = ServletActionContext.getRequest().getRequestURI(); System.out.println("requestURI-------->>" + requestURI);//结果:  /weatherweb2.0/pages/theme/theme_add.action

//获取本地地址 String localAddr = ServletActionContext.getRequest().getLocalAddr(); System.out.println("loaclAddr--------->>" + localAddr);//结果:127.0.0.1

//获取用户提交请求的页面 String referer = ServletActionContext.getRequest().getHeader("Referer"); System.out.println("referer--------->>" + referer);//结果:  http://localhost:8080/weatherweb2.0/pages/theme/theme_add.html

//获取远程主机地址 String remoteAddr=ServletActionContext.getRequest().getRemoteAddr(); System.out.println("remoteAddr-------->>"+remoteAddr);//结果:  127.0.0.1

//获取远程主机名: String remoteHost=ServletActionContext.getRequest().getRemoteHost(); System.out.println("remoteHost--------->>"+remoteHost);//结果:  127.0.0.1

//获取当前服务器名 String serverName = ServletActionContext.getRequest().getServerName(); System.out.println("serverName------>>" + serverName);//结果:  localhost

  

你可能感兴趣的:(servlet)