← 返回首页
Servlet基础教程(十四)
发表时间:2020-04-07 02:40:25
讲解Servlet的request

1.什么是request

request是Servlet.service()方法的一个参数,在客户端发出每个请求时,服务器都会创建一个request对象,并把请求数据封装到request中,然后在调用Servlet.service()方法时传递给service()方法

HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,开发人员通过这个对象的方法,可以获得客户这些信息。 .

2.request常用方法

URL各个部分的含义:

package com.servlet;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

@WebServlet(value = "/hello")
public class HelloServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取指定参数
        String username = req.getParameter("username");
        //用来存储一个对象,也可以称之为存储一个域属性
        req.setAttribute("loginName", username);
        //获取名为xx的域属性
        String loginName = (String) req.getAttribute("loginName");
        System.out.println(loginName);
        //获取所有域属性的名称;
        Enumeration attributeNames = req.getAttributeNames();
        //获取请求体的字节数,GET请求没有请求体,没有请求体返回-1;
        long length = req.getContentLength();
        System.out.println("请求体长度:"+length);
        /*
           获取请求类型,如果请求是GET,那么这个方法返回null;如果是POST请求,那么默认
           为application/x-www-form-urlencoded,表示请求体内容使用了URL编码;
        */
        String contentType = req.getContentType();
        //返回请求类型
        System.out.println("请求类型:"+contentType);
        //返回请求方法,例如:GET/POST
        String method = req.getMethod();
        System.out.println("请求方法:"+method);
        //返回请求字符集
        String charset =  req.getCharacterEncoding();
        System.out.println("请求编码:"+charset);
        //返回请求参数
        String queryStr = req.getQueryString();
        System.out.println("请求参数:"+queryStr);
        //返回上下文路径
        String contextPath = req.getContextPath();
        System.out.println("上下文:"+contextPath);
        //返回Servlet路径,例如:/ServletDemo1
        String servletPath = req.getServletPath();
        System.out.println("ServletPath:"+servletPath);
        //返回请求URL路径,例如:http://localhost/Demo1/ServletDemo1即返回除了参数
        StringBuffer requestURL = req.getRequestURL();
        System.out.println("请求URL路径:" + requestURL);
        //返回当前客户端的IP地址
        String ipAddr = req.getRemoteAddr();
        System.out.println("客户端的IP地址:" + ipAddr);
        //返回当前客户端的主机名,但这个方法的实现还是获取IP地址
        String hostName = req.getRemoteHost();
        System.out.println("客户端的主机名:" + hostName);
        //返回请求协议,例如:http
        String schema = req.getScheme();
        System.out.println("请求协议:" + schema);
        //返回主机名,例如:localhost
        String serverName = req.getServerName();
        System.out.println("主机名:" + serverName);
        //返回服务器端口号,例如:8080
        int port = req.getServerPort();
        System.out.println("服务器端口号:" + port);
    }
}

使用表单测试。

<div>
    <form action="hello" method="get">
        <input type="text" name="username" value="zhangsan"/>
        <input type="password" name="password" value="123456"/>
        <input type="submit" value="提交"/>
    </form>
</div>

点击提交按钮后,执行了HelloServlet,控制台输出效果如下:

zhangsan
请求体长度:-1
请求类型:null
请求方法:GET
请求编码:null
请求参数:username=zhangsan&password=123456
上下文:/helloservlet
ServletPath:/hello
请求URL路径:http://localhost:8080/helloservlet/hello
客户端的IP地址:0:0:0:0:0:0:0:1
客户端的主机名:0:0:0:0:0:0:0:1
请求协议:http
主机名:localhost
服务器端口号:8080

3.请求体getContentLength如何计算

首先,我们需要明确对于Get请求,那么请求体长度肯定是-1,因为Get请求是通过URL传参的方式发送数据的。 那么Post请求是如何计算请求体长度的呢?

我们以用户登录为例:有以下表单

<div>
    <form method="post" action="login">
        <table>
            <tr>
                <td>用户名</td>
                <td><input type="text" name="username" value=""/></td>
            </tr>
            <tr>
                <td>密码</td>
                <td><input type="password" name="password" value=""/></td>
            </tr>
            <tr>
                <td colspan="2"><input type="submit" value="登录"/></td>
            </tr>
        </table>
    </form>
</div>

如果用户名和密码都是英文字符集,那么服务器接收到的请求体内容格式如下:

username=zhangsan;password=123456

这样计算其请求长度为33个字节。

如果表单中提交了中文字符,中文字符一律使用utf-8编码,但是提交给服务器一律都是当字符串看待。 比如:‘张’的utf-8编码是‘E5BCA0’,发送请求时还要携带上%前缀,那么就变成‘%E5%BC%A0’,所以一个汉字存储在请求体占用九个字节。

那么,如果用户名是张三,密码是123456,其请求体格式如下:

username=%E5%BC%A0%E4%B8%89;password=123456

这样计算请求体长度为43个字节。