建立时间: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. 关于gcd

    内容: \(gcd(a,b)=gcd(b,a\% b)\) 用途: 这不废话嘛,当然是用来求最大公约数啊 证明:(这还是四月份的时候cdx巨佬给我讲的qwq) 设\(d=gcd(a.b)\) 则有\( ...

  2. java 获取用户输入

    import java.util.Scanner; public class Sample { public static void main(String[] args) { int num; Sc ...

  3. Java后端传Long类型给前端导致的精度丢失

    问题:实体属性是Long类型,在后端值本来是1119102511023023410,但是返回给前端的却是1119102511023023400 解决方案:添加序列化注解 import com.fast ...

  4. linux操作记录

    cd ~ 是返回根目录cd .. 跳转到上一页cd 目录 是跳转到目录文件 php -i | grep php.ini  是查看php.ini在哪个文件 vi 是查看文件内容,:i 是编辑文件内容 : ...

  5. Python 标准数据类型

    标准数据类型: Number(数字)----int float bool complex(复数) String(字符串) List(列表) Tuple(元组) Dictionary(字典) Set(集 ...

  6. StackExchange.Redis Timeout performing 超时问题

    最近在做的一个项目,用的.net core 2.1,然后缓存用的Redis,缓存相关封装是同事写的,用的驱动是StackExchange.Redis version 2.0.571 ,一直听说这个驱动 ...

  7. ECMAScript 初探 - 基础篇

    ECMAScript 语言的标准是由 Netscape.Sun.微软.Borland 等公司基于 JavaScript 和 JScript 锤炼.定义出来的. ECMAScript 仅仅是一个描述,定 ...

  8. bash: telnet: command not found (Linux安装telnet)

    问题描述: centos 系统没有 telnet 命令 bash: telnet: command not found 1.安装telnet服务 (3个) yum install xinetd tel ...

  9. windows环境中hbase源码编译遇到的问题

    转载请注明出处 问题一 [ERROR] Failed to execute goal org.codehaus.mojo:findbugs-maven-plugin:3.0.0:findbugs (d ...

  10. 循环节 + 矩阵快速幂 - HDU 4291 A Short problem

    A Short problem Problem's Link Mean: 给定一个n,求:g(g(g(n))) % 1000000007 其中:g(n) = 3g(n - 1) + g(n - 2), ...