建立时间:2019.6.28 &6.29

1.HttpServletRequest概述

我们在创建Servlet时会覆盖service()方法,或doGet()/doPost(),这些方法都有两个参数,一个为代表请求的request和代表响应response。

service方法中的request的类型是ServletRequest,而doGet/doPost方法的request的类型是HttpServletRequest,HttpServletRequest是ServletRequest的子接口。

2.request的运行流程

3.通过抓包工具抓取Http请求

因为request代表请求,所以我们可以通过该对象分别获得Http请求的请求行,请  求头和请求体

4.通过request获得请求行

获得客户端的请求方式:String getMethod()

获得请求的资源:

String getRequestURI() 所有资源地址,例如:本地磁盘地址,网络地址,相对位置

StringBuffer getRequestURL()
网络地址

String getContextPath() ---web应用的名称

String getQueryString() ---- get提交url地址后的参数字符串(当提交方法是post,因为参数信息不在地址中,所以返回null

username=zhangsan&password=123

注意:request获得客户机(客户端)的一些信息

request.getRemoteAddr() --- 获得访问的客户端IP地址

5.通过request获得请求头

long getDateHeader(String name)

String getHeader(String name)

Enumeration getHeaderNames()

Enumeration getHeaders(String name)

int getIntHeader(String name)

referer头的作用:执行该此访问的的来源

做防盗链

6.通过request获得请求体

请求体中的内容是通过post提交的请求参数,格式是:

username=zhangsan&password=123&hobby=football&hobby=basketball

key ---------------------- value

username                    [zhangsan]

password                    [123]

hobby                         [football,basketball]

以上面参数为例,通过一下方法获得请求参数:

String getParameter(String name) 获得一个参数值

String[] getParameterValues(String name)  获得多个参数值

Enumeration getParameterNames()

Map<String,String[]> getParameterMap()

注意:get请求方式的请求参数 上述的方法一样可以获得

解决post提交方式的乱码:request.setCharacterEncoding("UTF-8");

解决get提交的方式的乱码:

parameter = new
String(parameter.getbytes("iso8859-1"),"UTF-8");

7.request的其他功能

(1)request是一个域对象

request对象也是一个存储数据的区域对象,所以也具有如下方法:

setAttribute(String name, Object o)

getAttribute(String name)

removeAttribute(String name)

注意:request域的作用范围:一次请求中

(2)request完成请求转发

获得请求转发器----path是转发的地址

RequestDispatcher getRequestDispatcher(String path)

通过转发器对象转发

requestDispathcer.forward(ServletRequest
request,
ServletResponse response)

 

 

没必要写成/WEB15/Servlet2,因为是访问内部资源

 

 

 

 

 

注意:ServletContext域与Request域的生命周期比较?

         ServletContext

            创建:服务器启动

            销毁:服务器关闭

            域的作用范围:整个web应用

         request

            创建:访问时创建request

            销毁:响应结束request销毁

            域的作用范围:一次请求中

 

      注意:转发与重定向的区别?

         1)重定向两次请求,转发一次请求

         2)重定向地址栏的地址变化,转发地址不变

         3)重新定向可以访问外部网站 转发只能访问内部资源

         4)转发的性能要优于重定向(速度快)

      注意:客户端地址与服务器端地址的写法?

         客户端地址:

            是客户端去访问服务器的地址,服务器外部的地址,特点:写上web应用名      

           

            直接输入地址,

            重定向

           

 

         服务器端地址:

            服务器内部资源的跳转的地址,特点:不需要写web应用的名称

 

            转发

 

 

 

 

总结:

request获得行的内容

      request.getMethod()

      request.getRequestURI()

      request.getRequestURL()

      request.getContextPath()

      request.getRemoteAddr()

request获得头的内容

      request.getHeader(name)

request获得体(请求参数)

      String request.getParameter(name)

      Map<String,String[]>
request.getParameterMap();

      String[] request.getParameterValues(name);

      注意:客户端发送的参数 到服务器端都是字符串

 

      获得中文乱码的解决:

         post:request.setCharacterEncoding(“UTF-8”);

         get:先用iso8859-1编码,再用UTF-8解码)

parameter
= new String(parameter.getBytes(“iso8859-1”),”UTF-8”);

 

 

request转发和域

      request.getRequestDispatcher(转发的地址).forward(req,resp);

      request.setAttribute(name,value)

      request.getAttribute(name)

*自动生成随机字符串(在企业里不常用int自增长类型定义id)

*div标签中写java代码

(此处为直接在页面输出,加个等号=)<% %>

*使用BeanUtils进行自动映射封装,利用map

*数据库sql语句查询参数?

[转]【HttpServlet】HttpServletRequest接口的更多相关文章

  1. HttpServletRequest接口实例化的使用

    HttpServletRequ接口的使用和jsp内置对象的request对象非常类似,request对象其实 就是HttpServletRequest接口的一个实例,不过气实例化的过程是自动的,无须自 ...

  2. HttpServletRequest 接口、HttpServletResponse 接口、请求转发与重定向

    上篇文章我们讲了servlet的基本原理,这章将讲一下剩余的部分. HttpServletRequest 接口 该接口是 ServletRequest 接口的子接口,封装了 HTTP 请求的相关信息, ...

  3. HttpServletRequest接口

    package com.hongdian; import java.util.Enumeration; import java.io.IOException; import javax.servlet ...

  4. Servlet(6)—HttpServletRequest接口和HttpServletResponse接口

    HttpServletRequest接口和HttpServletResponse接口是继承ServletRequest和ServletResponse接口,是他们的子接口,但是我们在程序中进程看到Se ...

  5. 一、HttpServletRequest接口 二、HttpServletReponse接口 三、POST和GET请求方式及其乱码处理 四、ServletContext对象和ServletConfig对象

    一.HttpServletRequest接口 内部封装了客户端请求的数据信息 接收客户端的请求参数.HTTP请求数据包中配置参数 ###<1>常用方法 getContextPath()重要 ...

  6. javax.servlet.http.httpServletRequest接口

    HttpServletRequest接口中常用的方法:   - String getContentPath();//获取webapp根目录路径,如下图:   下面研究request到底是一个怎样的范围 ...

  7. HttpServletRequest接口是怎么实现的

    request只是规范中的一个名称而已.不是SUN提供的,这是由各个不同的Servlet提供商编写的,SUN只是规定这个类要实现HttpServletRequest接口,并且规定了各个方法的用途,但具 ...

  8. HttpServletRequest接口详解

    般情况下,浏览器(客户端)通过 HTTP 协议来访问服务器的资源,Servlet 主要用来处理 HTTP 请求.Servlet 处理 HTTP 请求的流程如下: Servlet 容器接收到来自客户端的 ...

  9. Java EE HttpServletRequest接口和HttpServletResponse接口

    package javax.servlet.http (https://docs.oracle.com/javaee/7/api/javax/servlet/http/package-summary. ...

随机推荐

  1. 交换机端口与Mac地址绑定(基于Cisco模拟器)

    实验设备: 二层交换机一台,主机三台 实验步骤: 1.进入相应的接口 (以端口1设置Mac地址绑定,PC0接1端口举例) Switch>enable Switch#config Configur ...

  2. django报错:django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module. Did you install mysqlclient?

    django 迁移数据库报错 django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.Did you ins ...

  3. [LeetCode] 827. Making A Large Island 建造一个巨大岛屿

    In a 2D grid of 0s and 1s, we change at most one 0 to a 1. After, what is the size of the largest is ...

  4. [LeetCode] 518. Coin Change 2 硬币找零之二

    You are given coins of different denominations and a total amount of money. Write a function to comp ...

  5. Idea用maven给springboot打jar包

    一.准备工作 1.工具:Idea2018,maven3.5 2.首先得保证pom有maven插件 <plugin> <groupId>org.springframework.b ...

  6. divide two numbers using + opertor

    package testpacknm; import java.util.Scanner; public class testcnm { public static void main(String[ ...

  7. Arcpy中Geometry类与Array类转换的陷阱

    1.现象说明 使用Arcpy.da.searchcursor得到Geometry,将Geometry转换成Array,再从Array转换回Geometry.若Geometry包含内环,这个过程可能导致 ...

  8. ab小工具的Failed requests多的问题

    ab小工具的Failed requests多的问题 这个是PHP返回的length不一致造成的 是ab的bug 所以不用理会.. 测试并发写100就行了 一般100没错误

  9. Java8 新特性 Steam() 中间有状态操作

    中间有状态操作 Java8 新特性 Stream 练习实例   中间操作,就是把数据处理成自己想要的类型,并且有状态操作,是在所有的数据基础上进行操作的.比如dictinct(去重),sorted(排 ...

  10. 解决xunsearch热门搜索,不按照数量排序问题

    public function getHotQuery($limit = 6, $type = 'total') { $ret = array(); $limit = max(1, min(50, i ...