HttpServletRequest

获得客户机信息

  getRequestURL方法返回客户端发出请求时的完整URL。
  getRequestURI方法返回请求行中的资源名部分。
  getQueryString 方法返回请求行中的参数部分。
  getPathInfo方法返回请求URL中的额外路径信息。额外路径信息是请求URL中的位于Servlet的路径之后和查询参数之前的内容,它以“/”开头。
  getRemoteAddr方法返回发出请求的客户机的IP地址。
  getRemoteHost方法返回发出请求的客户机的完整主机名。
  getRemotePort方法返回客户机所使用的网络端口号。
  getLocalAddr方法返回WEB服务器的IP地址。
  getLocalName方法返回WEB服务器的主机名。

获得客户机请求头

  getHeader(string name)方法:String 

  getHeaders(String name)方法:Enumeration

  getHeaderNames()方法

获得客户机请求参数(客户端提交的数据)

  • getParameter(String)方法(常用)
  • getParameterValues(String name)方法(常用)
  • getParameterNames()方法(不常用)
  • getParameterMap()方法(编写框架时常用) Map<String, String[]>

 

提交中文数据乱码问题总结

 

  1、如果提交方式为post,想不乱码,只需要在服务器端设置request对象的编码即可,客户端以哪种编码提交的,服务器端的request对象就以对应的编码接收,比如客户端是以UTF-8编码提交的,那么服务器端request对象就以UTF-8编码接收(request.setCharacterEncoding("UTF-8"))

 

  2、如果提交方式为get,设置request对象的编码是无效的,request对象还是以默认的tomcat编码接收数据,因此要想不乱码,只能在接收到数据后再手工转换

setCharacterEncoding

public void setCharacterEncoding(java.lang.String env)
                          throws java.io.UnsupportedEncodingException

Overrides the name of the character encoding used in the body of this request. This method must be called prior to reading request parameters or reading input using getReader().

请求转发

request对象作为一个域对象(Map容器)使用时,主要是通过以下的四个方法来操作

  • setAttribute(String name,Object o)方法,将数据作为request对象的一个属性存放到request对象中,例如:request.setAttribute("data", data);
  • getAttribute(String name)方法,获取request对象的name属性的属性值,例如:request.getAttribute("data")
  • removeAttribute(String name)方法,移除request对象的name属性,例如:request.removeAttribute("data")
  • getAttributeNames方法,获取request对象的所有属性名,返回的是一个,例如:Enumeration<String> attrNames = request.getAttributeNames();

一个web资源收到客户端请求后,通知服务器去调用另外一个web资源进行处理,称之为请求转发/307。
一个web资源收到客户端请求后,通知浏览器去访问另外一个web资源进行处理,称之为请求重定向/302。

 

posted on 2015-08-27 10:30  cbwleft  阅读(65)  评论(0)    收藏  举报