HttpServletRequest类的介绍以及Servlet-Request常用API、获取请求参数、请求转发
a)HttpServletRequest类有什么作用?
每次只要有请求进入Tomcat服务器,Tomcat服务器就会把请求过来的HTTP协议信息解析好封装到Request对象中。
然后传递到service方法(doGet和doPost)中给我们使用。我们可以通过HttpServletRequest对象,获取到所有请求的信息。
b)HttpServletRequest类的常用方法
RequestAPIServlet.java
package com.atguigu.servlet;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
/** * Create By LiFang on 2020/7/14. */
public class RequestAPIServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// super.doGet(req, resp);
// i.getRequestURL() 获取请求的资源路径
System.out.println("URI => " + req.getRequestURI());
// ii.getRequestURL 获取请求的统一资源定位符(绝对路径)
System.out.println("URL => " + req.getRequestURL());
// iii.getRemoteHost() 获取客户端的ip地址
// iv.getHeader() 获取请求头
// vii.getMethod() 获取请求的方式GET或POST
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>RequestAPIServlet</servlet-name>
<servlet-class>com.atguigu.servlet.RequestAPIServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RequestAPIServlet</servlet-name>
<url-pattern>/requestAPIServlet</url-pattern>
</servlet-mapping>
</web-app>
控制台结果:
浏览器客户端:
对程序进行热部署,直接刷新浏览器后,得到结果 :
------------------------------------------------------------分割线-----------------------------------------------------------------
控制台运行结果:
此时,你就会发现客户端 ip地址 为什么不是 127.0.0.1 ?
别慌,看这篇文章: https://www.cnblogs.com/m3Lee/p/3808002.html 你会得到答案。
继续获取:
结果:
服务器结果:
与浏览器(客户端)获取的是一样的:
在浏览器敲回车,得到GET请求,
控制台信息:
c)如何获取请求参数
ParameterServlet.java
package com.atguigu.servlet;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
/** * Create By LiFang on 2020/7/14. */
public class ParameterServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// super.doGet(req, resp);
//设置请求体的字符集为UTF-8,从而解决post请求的中文乱码问题
//也要在获取请求参数之前调用才有效
req.setCharacterEncoding("UTF-8");
System.out.println("--------Get-----------");
//获取请求参数
String username = req.getParameter("username");
String password = req.getParameter("password");
String[] hobby = req.getParameterValues("hobby");
System.out.println("用户名:" + username);
System.out.println("密码:" + password);
System.out.println("兴趣爱好:" + hobby);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// super.doPost(req, resp);
//设置请求体的字符集为UTF-8,从而解决post请求的中文乱码问题
//也要在获取请求参数之前调用才有效
req.setCharacterEncoding("UTF-8");
System.out.println("---------Post-----------");
//获取请求参数
String username = req.getParameter("username");
String password = req.getParameter("password");
String[] hobby = req.getParameterValues("hobby");
System.out.println("用户名:" + username);
System.out.println("密码:" + password);
System.out.println("兴趣爱好:" + Arrays.asList(hobby));
}
}
form.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="http://localhost:8080/04_servlet/parameterServlet" method="get">
用户名:<input type="text" name="username"><br/>
密码:<input type="password" name="password"><br/>
兴趣爱好:<input type="checkbox" name="hobby" value="cpp">C++
<input type="checkbox" name="hobby" value="java">Java
<input type="checkbox" name="hobby" value="js">Js<br/>
<input type="submit">
</form>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>RequestAPIServlet</servlet-name>
<servlet-class>com.atguigu.servlet.RequestAPIServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RequestAPIServlet</servlet-name>
<url-pattern>/requestAPIServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ParameterServlet</servlet-name>
<servlet-class>com.atguigu.servlet.ParameterServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ParameterServlet</servlet-name>
<url-pattern>/parameterServlet</url-pattern>
</servlet-mapping>
服务器运行结果:
d)请求的转发
请求转发是指,服务器收到请求后,从一个资源,跳转到另一个服务器资源的操作。
项目部署到浏览器运行:
控制台输出结果:
总结: