<p>HttpServletRequest 对象代表客户端的请求,当客户端通过 HTTP 协议访问服务器时,HTTP 请求头中的所有信息都封装在这个对象中,通过这个对象提供的方法,可以获得客户端请求的所有信息。<br/>获得客户机信息:</p><table width="759"><thead style="box-sizing: inherit; outline: 0px; line-height: 1.8;"><tr style="box-sizing: inherit; outline: 0px; line-height: 1.8; border-top: 1px solid rgb(204, 204, 204);" class="firstRow"><th style="box-sizing: inherit; outline: 0px; padding: 6px 13px; margin: 0px; line-height: 1.8; border-top-color: rgb(221, 221, 221);">getRequestURL()</th><th style="box-sizing: inherit; outline: 0px; padding: 6px 13px; margin: 0px; line-height: 1.8; border-top-color: rgb(221, 221, 221);">返回客户端发出请求时的完整 URL。</th></tr></thead><tbody style="box-sizing: inherit; outline: 0px; line-height: 1.8;"><tr style="box-sizing: inherit; outline: 0px; line-height: 1.8; border-top: 1px solid rgb(204, 204, 204);"><td style="box-sizing: inherit; outline: 0px; padding: 6px 13px; margin: 0px; line-height: 1.8;">getRequestURI()</td><td style="box-sizing: inherit; outline: 0px; padding: 6px 13px; margin: 0px; line-height: 1.8;">返回请求行中的参数部分。</td></tr><tr style="box-sizing: inherit; outline: 0px; line-height: 1.8; background-color: rgb(248, 248, 248); border-top: 1px solid rgb(204, 204, 204);"><td style="box-sizing: inherit; outline: 0px; padding: 6px 13px; margin: 0px; line-height: 1.8;">getQueryString ()</td><td style="box-sizing: inherit; outline: 0px; padding: 6px 13px; margin: 0px; line-height: 1.8;">返回发出请求的客户机的 IP 地址。</td></tr><tr style="box-sizing: inherit; outline: 0px; line-height: 1.8; border-top: 1px solid rgb(204, 204, 204);"><td style="box-sizing: inherit; outline: 0px; padding: 6px 13px; margin: 0px; line-height: 1.8;">getRemoteHost()</td><td style="box-sizing: inherit; outline: 0px; padding: 6px 13px; margin: 0px; line-height: 1.8;">返回发出请求的客户机的完整主机名。</td></tr><tr style="box-sizing: inherit; outline: 0px; line-height: 1.8; background-color: rgb(248, 248, 248); border-top: 1px solid rgb(204, 204, 204);"><td style="box-sizing: inherit; outline: 0px; padding: 6px 13px; margin: 0px; line-height: 1.8;">getRemoteAddr()</td><td style="box-sizing: inherit; outline: 0px; padding: 6px 13px; margin: 0px; line-height: 1.8;">返回发出请求的客户机的 IP 地址。</td></tr><tr style="box-sizing: inherit; outline: 0px; line-height: 1.8; border-top: 1px solid rgb(204, 204, 204);"><td style="box-sizing: inherit; outline: 0px; padding: 6px 13px; margin: 0px; line-height: 1.8;">getPathInfo()</td><td style="box-sizing: inherit; outline: 0px; padding: 6px 13px; margin: 0px; line-height: 1.8;">返回请求 URL 中的额外路径信息。额外路径信息是请求 URL 中的位于 Servlet 的路径之后和查询参数之前的内容,它以 "/" 开头。</td></tr><tr style="box-sizing: inherit; outline: 0px; line-height: 1.8; background-color: rgb(248, 248, 248); border-top: 1px solid rgb(204, 204, 204);"><td style="box-sizing: inherit; outline: 0px; padding: 6px 13px; margin: 0px; line-height: 1.8;">getRemotePort()</td><td style="box-sizing: inherit; outline: 0px; padding: 6px 13px; margin: 0px; line-height: 1.8;">返回客户机所使用的网络端口号。</td></tr><tr style="box-sizing: inherit; outline: 0px; line-height: 1.8; border-top: 1px solid rgb(204, 204, 204);"><td style="box-sizing: inherit; outline: 0px; padding: 6px 13px; margin: 0px; line-height: 1.8;">getLocalAddr()</td><td style="box-sizing: inherit; outline: 0px; padding: 6px 13px; margin: 0px; line-height: 1.8;">返回 WEB 服务器的 IP 地址。</td></tr><tr style="box-sizing: inherit; outline: 0px; line-height: 1.8; background-color: rgb(248, 248, 248); border-top: 1px solid rgb(204, 204, 204);"><td style="box-sizing: inherit; outline: 0px; padding: 6px 13px; margin: 0px; line-height: 1.8;">getLocalName()</td><td style="box-sizing: inherit; outline: 0px; padding: 6px 13px; margin: 0px; line-height: 1.8;">返回 WEB 服务器的主机名。</td></tr></tbody></table><p>举例:返回客户端发出请求时的完整 URL</p><pre class="brush:java;toolbar:false">import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Servlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse resp) { throws ServletException, IOException { // TODO Auto-generated method stub doPost(request, resp); } } @Override protected void doPost(HttpServletRequest request, HttpServletResponse resp) { throws ServletException, IOException { String requestUrl = request.getRequestURL().toString();// 得到请求的URL地址 resp.setCharacterEncoding("UTF-8");// 设置将字符以"UTF-8"编码输出到客户端浏览器 // 通过设置响应头控制浏览器以UTF-8的编码显示数据,如果不加这句话,那么浏览器显示的将是乱码 resp.setHeader("content-type", "text/html;charset=UTF-8"); PrintWriter out = resp.getWriter(); out.write("请求的URL地址:" + requestUrl); } } }</pre><p><p style="text-align: center;"><img src="/upload/content/20200520/1589990152427807.jpg" alt="HttpServletRequest详解"title="HttpServletRequest详解" width="550px"></p><p>获得客户机请求头</p><table width="759"><thead style="box-sizing: inherit; outline: 0px; line-height: 1.8;"><tr style="box-sizing: inherit; outline: 0px; line-height: 1.8; border-top: 1px solid rgb(204, 204, 204);" class="firstRow"><th style="box-sizing: inherit; outline: 0px; padding: 6px 13px; margin: 0px; line-height: 1.8; border-top-color: rgb(221, 221, 221);">方法</th><th style="box-sizing: inherit; outline: 0px; padding: 6px 13px; margin: 0px; line-height: 1.8; border-top-color: rgb(221, 221, 221);"><br/></th></tr></thead><tbody style="box-sizing: inherit; outline: 0px; line-height: 1.8;"><tr style="box-sizing: inherit; outline: 0px; line-height: 1.8; border-top: 1px solid rgb(204, 204, 204);"><td style="box-sizing: inherit; outline: 0px; padding: 6px 13px; margin: 0px; line-height: 1.8;">getHeader(string name)</td><td style="box-sizing: inherit; outline: 0px; padding: 6px 13px; margin: 0px; line-height: 1.8;">以 String 的形式返回指定请求头的值。如果该请求不包含指定名称的头,则此方法返回 null。如果有多个具有相同名称的头,则此方法返回请求中的第一个头。头名称是不区分大小写的。可以将此方法与任何请求头一起使用</td></tr><tr style="box-sizing: inherit; outline: 0px; line-height: 1.8; background-color: rgb(248, 248, 248); border-top: 1px solid rgb(204, 204, 204);"><td style="box-sizing: inherit; outline: 0px; padding: 6px 13px; margin: 0px; line-height: 1.8;">getHeaders(String name)</td><td style="box-sizing: inherit; outline: 0px; padding: 6px 13px; margin: 0px; line-height: 1.8;">以 String 对象的 Enumeration 的形式返回指定请求头的所有值</td></tr><tr style="box-sizing: inherit; outline: 0px; line-height: 1.8; border-top: 1px solid rgb(204, 204, 204);"><td style="box-sizing: inherit; outline: 0px; padding: 6px 13px; margin: 0px; line-height: 1.8;">getHeaderNames()</td><td style="box-sizing: inherit; outline: 0px; padding: 6px 13px; margin: 0px; line-height: 1.8;">返回此请求包含的所有头名称的枚举。如果该请求没有头,则此方法返回一个空枚举</td></tr></tbody></table><p style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 1.2em; padding: 0px; color: rgb(54, 54, 54); line-height: 1.8; font-family: "Microsoft YaHei", "SF Pro Display", Roboto, Noto, Arial, "PingFang SC", sans-serif; float: inherit; word-break: break-word; white-space: normal; background-color: rgb(255, 255, 255);"><span style="box-sizing: inherit; outline: 0px; font-weight: bolder; line-height: 1.8;">获得客户机请求参数</span></p><table width="759"><thead style="box-sizing: inherit; outline: 0px; line-height: 1.8;"><tr style="box-sizing: inherit; outline: 0px; line-height: 1.8; border-top: 1px solid rgb(204, 204, 204);" class="firstRow"><th style="box-sizing: inherit; outline: 0px; padding: 6px 13px; margin: 0px; line-height: 1.8; border-top-color: rgb(221, 221, 221);">getParameter(String name)</th><th style="box-sizing: inherit; outline: 0px; padding: 6px 13px; margin: 0px; line-height: 1.8; border-top-color: rgb(221, 221, 221);">根据 name 获取请求参数 (常用)</th></tr></thead><tbody style="box-sizing: inherit; outline: 0px; line-height: 1.8;"><tr style="box-sizing: inherit; outline: 0px; line-height: 1.8; border-top: 1px solid rgb(204, 204, 204);"><td style="box-sizing: inherit; outline: 0px; padding: 6px 13px; margin: 0px; line-height: 1.8;">getParameterValues(String name)</td><td style="box-sizing: inherit; outline: 0px; padding: 6px 13px; margin: 0px; line-height: 1.8;">根据 name 获取请求参数列表 (常用)</td></tr><tr style="box-sizing: inherit; outline: 0px; line-height: 1.8; background-color: rgb(248, 248, 248); border-top: 1px solid rgb(204, 204, 204);"><td style="box-sizing: inherit; outline: 0px; padding: 6px 13px; margin: 0px; line-height: 1.8;">getParameterMap()</td><td style="box-sizing: inherit; outline: 0px; padding: 6px 13px; margin: 0px; line-height: 1.8;">返回的是一个 Map 类型的值,该返回值记录着前端(如 jsp 页面)所提交请求中的请求参数和请求参数值的映射关系。(编写框架时常用)</td></tr></tbody></table><p>请求转发:<br/>指一个 web 资源收到客户端请求后,通知服务器去调用另外一个 web 资源进行处理。<br/>1、通过 ServletContext 的 getRequestDispatcher(String path) 方法,该方法返回一个 RequestDispatcher 对象,调用这个对象的 forward 方法可以实现请求转发。<br/>例如:将请求转发的 test.jsp 页面</p><pre class="brush:java;toolbar:false">RequestDispatcher reqDispatcher =this.getServletContext().getRequestDispatcher("/test.jsp"); reqDispatcher.forward(request, response);</pre><p>2、通过 request 对象提供的 getRequestDispatche(String path) 方法,该方法返回一个 RequestDispatcher 对象,调用这个对象的 forward 方法可以实现请求转发。**<br/>例如:将请求转发的 test.jsp 页面</p><pre class="brush:java;toolbar:false">request.getRequestDispatcher("/test.jsp").forward(request, response);</pre><p>request 对象同时也是一个域对象 (Map 容器),开发人员通过 request 对象在实现转发时,把数据通过 request 对象带给其它 web 资源处理。</p><pre class="brush:java;toolbar:false">String data="心若静,风奈何"; //将数据存放到request对象中,此时把request对象当作一个Map容器来使用 request.setAttribute("data", data); //客户端访问RequestDemo06这个Servlet后,RequestDemo06通知服务器将请求转发(forward)到test.jsp页面进行处理 request.getRequestDispatcher("/test.jsp").forward(request, response);</pre><p>request 对象作为一个域对象 (Map 容器) 使用时,主要是通过以下的四个方法来操作setAttribute(String name,Object o) 方法,将数据作为 request 对象的一个属性存放到 request 对象中,例如:request.setAttribute(“data”, data);</p><p><strong>getAttribute(String name) 方法</strong></p><p>获取 request 对象的 name 属性的属性值,例如:request.getAttribute(“data”)</p><p><strong>removeAttribute(String name) 方法</strong></p><p>移除 request 对象的 name 属性,例如:request.removeAttribute(“data”)</p><p><strong>getAttributeNames 方法</strong></p><p>获取 request 对象的所有属性名,返回的是一个,例如:EnumerationattrNames = request.getAttributeNames();</p>