Web: Request -07

Request概述

代表HTTP请求的对象。

Request继承结构

ServletRequest

|-HttpServletRequest

  • ServletRequest

request的根接口,定义了请求相关的方法

  • HttpServletRquest

ServletRequest接口的实现类,在ServletRequest接口的基础上增加了很多和HTTP协议相关的方法

Request功能详解

  • 获取客户机相关信息

StringBuffer

getRequestURL()

          Reconstructs the URL the client used to make the request.

获取客户端访问的完整URL

String

getRequestURI()

          Returns the part of this request's URL from the protocol name up to the query string in the first line of the HTTP request.

获取客户端访问的资源路径

String

getQueryString()

          Returns the query string that is contained in the request URL after the path.

获取请求地址中的参数部分作为字符串返回

String

getRemoteAddr()

          Returns the Internet Protocol (IP) address of the client or last proxy that sent the request.

获取客户机ip地址信息

String

getMethod()

          Returns the name of the HTTP method with which this request was made, for example, GET, POST, or PUT.

获取请求方式

String

getContextPath()

          Returns the portion of the request URI that indicates the context of the request.

获取当前访问的应用名称

  • 获取请求头信息 

String

getHeader(String name)

          Returns the value of the specified request header as a String.

获取指定请求头的值

Enumeration

getHeaders(String name)

          Returns all the values of the specified request header as an Enumeration of String objects.

获取指定请求头的值们

Enumeration

getHeaderNames()

          Returns an enumeration of all the header names this request contains.

获取所有请求头的名字们

int

getIntHeader(String name)

          Returns the value of the specified request header as an int.

以int类型获取指定请求头的值

long

getDateHeader(String name)

          Returns the value of the specified request header as a long value that represents a Date object.

以时间毫秒值获取请求头的值

  • 获取请求参数 

String

getParameter(String name)

          Returns the value of a request parameter as a String, or null if the parameter does not exist.

获取指定名称的请求参数的值

 String[]

getParameterValues(String name)

          Returns an array of String objects containing all of the values the given request parameter has, or null if the parameter does not exist.

获取指定名称请求参数的值的集合

请求中可能包含多个同名请求参数,可以通过这个方法来获取

Enumeration

getParameterNames()

          Returns an Enumeration of String objects containing the names of the parameters contained in this request.

获取所有请求参数名们

Map

getParameterMap()

          Returns a java.util.Map of the parameters of this request.

获取所有请求参数组成的Map<String,String[]>,其中键为请求参数名,值为请求参数值的数组

  • 请求参数乱码原理

浏览器发送请求参数时采用的编码取决于表单页面的编码。

如果没有表单页面,直接在地址栏上敲地址,则采用系统码。

服务器在解析请求参数时,解析用的编码如下:

GET提交

请求参数在地址之后

看server.xml中连接器的配置

Tomcat8默认为utf-8

Tomcat7及以前的版本为iso8859-1

POST提交

请求参数在实体内容

看request.setCharacterEncoding()

默认为iso8859-1

必须在任何获取请求参数操作之前设置

在产生请求参数乱码时,可以以上原理通过改变表单页面编码、tomcat连接器配置(GET乱码)、设置request.setCharacterEncoding(POST乱码)来解决。
如果因为各种原因,乱码已经产生,此时可以通过手动编解码解决乱码。

注意仅限ios8859-1产生的乱码可以通过这种方式来解决。

String str = new String(??????.getBytes("iso8859-1"),"utf-8")

EasyMall注册功能 - 获取表单数据实现

举例:

  • 实现请求转发

请求转发时javaweb开发中的三种资源跳转方式之一

  • 实现请求转发

服务器内部的资源跳转机制

一次请求一次响应

可以利用request域传递数据

浏览器地址栏不会发生变化

  • 转发的实现

注意,请求转发是服务器内部的资源跳转,目标地址不需要写应用名称

RequestDispatcher dispatcher=request.getRequestDispatcher("/MyServlet05");

dispatcher.forward(request,response);

简写

request.getRequestDispatcher("/MyServlet05").forward(request,response);

  • 转发注意事项
    • 无论转发多少次,只有最后一个资源可以对外输出数据,其他资源的输出在转发时会被清空
    • 可以多次转发,但不能循环转发

举例

EasyMall注册功能 - 数据校验功能实现

  • 作为域对象使用

request是一个域对象

request域是javaweb开发中四大作用域之一

利用request域可以在转发的过程中传递数据

操作API:

setAttribute(String name,Object obj)

向域中存入数据

getAttribute(String name)

从域中获取数据

removeAttribute(String name)

删除域中指定数据

getAttributeNames()

获取域中所有属性名组成的枚举

特点:

在一次请求范围内有效

请求开始 request对象创建 作用域创建

请求结束 request对象销毁 作用域销毁

在一个请求范围内(转发、包含)共享数据

举例:

EasyMall注册功能 - 数据校验功能实现 - 传递错误消息

请求包含可以将其他资源的输出包含到当前资源中。

包含的实现:

request.getRequestDispacher(path).include(request,response);

举例:

EasyMall主页通过包含引入头和尾。

  • 27
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值