Servlet-HttpServletRequest类
HttpServletRequest类
一、HttpServletRequest类的作用
每次只要有请求进入Tomcat服务器,Tomcat服务器就会把请求过来的http协议内容解析好封装到Request对象中。
然后传递到service方法(doGet、doPost)中给我们使用。我们可以通过HttpServletRequest对象获取到所有请求的信息。
(Tomcat服务器创建,每次请求创建一个)
二、HttpServletRequest常用方法
(1)getRequestURI():获取请求的资源路径
(2)getRequestURL():获取请求的统一资源定位符(绝对路径)
(3)getRemoteHost():获取客户端的ip地址
(4)getHeader(String s):获取请求头
(5)getParameter(String s):获取请求的参数
(6)getParameterValues(String s):获取请求的参数(多个值时使用)
(7)getMethod():获取请求的方式(GET/POST)
(8)setAttribute(String var1, Object var2):设置域数据
(9)getAttribute(String var1):获取域数据
(10)getRequestDispatcher(String var1):获取请求转发对象
相关代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.orzjiangxiaoyu.servlet; import jakarta.servlet.ServletException; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.Arrays; /** * HttpServletRequest类的作用 * * 每次只要有请求进入Tomcat服务器,Tomcat服务器就会把请求过来的http协议内容解析好封装到Request对象中。 * 然后传递到service方法(doGet、doPost)中给我们使用。我们可以通过HttpServletRequest对象获取到所有请求的信息。 * (Tomcat服务器创建,每次请求创建一个) * * * HttpServletRequest常用方法 * * getRequestURI():获取请求的资源路径 * getRequestURL():获取请求的统一资源定位符(绝对路径) * getRemoteHost():获取客户端的ip地址 * getHeader(String s):获取请求头 * getParameter(String s):获取请求的参数 * getParameterValues(String s):获取请求的参数(多个值时使用) * getMethod():获取请求的方式(GET/POST) * setAttribute(String var1, Object var2):设置域数据 * getAttribute(String var1):获取域数据 * getRequestDispatcher(String var1):获取请求转发对象 * @author orz * @create 2020-09-10 22:02 */ public class RequestServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doGet(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("POST"); // getRequestURI():获取请求的资源路径 System.out.println("请求的资源路径:"+req.getRequestURI()); // getRequestURL():获取请求的统一资源定位符(绝对路径) System.out.println("获取请求的统一资源定位符(绝对路径):"+req.getRequestURL()); // getRemoteHost():获取客户端的ip地址 System.out.println("客户端的ip地址:"+req.getRemoteHost()); // getHeader(String s):获取请求头 System.out.println("获取请求头User-Agent:"+req.getHeader("User-Agent")); // getParameter(String s):获取请求的参数 System.out.println("获取请求的参数username:"+req.getParameter("username")); // getParameterValues(String s):获取请求的参数(多个值时使用) System.out.println("获取请求的参数(多个值)hobby:"+ Arrays.toString(req.getParameterValues("hobby"))); // getMethod():获取请求的方式(GET/POST) System.out.println("请求的方式:"+req.getMethod()); // setAttribute(String var1, Object var2):设置域数据 req.setAttribute("key1","value1"); // getAttribute(String var1):获取域数据 System.out.println("域数据 key=key1 value1="+req.getAttribute("key1")); // getRequestDispatcher(String var1):获取请求转发对象 System.out.println("请求转发对象:"+req.getRequestDispatcher("/")); } }
web.xml
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?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>Servlet1</servlet-name> <servlet-class>com.orzjiangxiaoyu.servlet.Servlet1</servlet-class> </servlet> <servlet-mapping> <servlet-name>Servlet1</servlet-name> <url-pattern>/servlet1</url-pattern> </servlet-mapping> <servlet> <servlet-name>Servlet2</servlet-name> <servlet-class>com.orzjiangxiaoyu.servlet.Servlet2</servlet-class> </servlet> <servlet-mapping> <servlet-name>Servlet2</servlet-name> <url-pattern>/servlet2</url-pattern> </servlet-mapping> <servlet> <servlet-name>RequestServlet</servlet-name> <servlet-class>com.orzjiangxiaoyu.servlet.RequestServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>RequestServlet</servlet-name> <url-pattern>/requestServlet</url-pattern> </servlet-mapping> </web-app>
form1.html
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<!DOCTYPE html> <html lang="zh_CN"> <head> <meta charset="UTF-8"> <title>requestServlet-Form</title> </head> <body> <form action="http://localhost:8090/Servlet_GetPost_war_exploded/requestServlet" method="post"> 用户名<input type="text" value="" name="username"/><br/> 密 码<input type="text" value="" name="password"/><br/> 爱 好<input type="checkbox" name="hobby" value="java"/>java <input type="checkbox" name="hobby" value="c++"/>c++ <input type="checkbox" name="hobby" value="php"/>php <br/> <input type="submit"/> </form> </body> </html>