DSP

JavaWeb基础系列(三)Request

2019-07-13 14:46发布

class="markdown_views prism-kimbie-light">

一、HttpServletRequest概述

我们在创建Servlet时会覆盖service()方法,或doGet()/doPost()方法,这些方法都有两个参数,一个代表请求的request和代表响应response
service方法中的request的类型是ServletRequest,而doGet()/doPost()方法的request的类型是HttpServletRequest,HttpServletRequest是ServletRequest的子类接口,功能和方法更加强大。

二、Request的运行流程

这里写图片描述

三、通过Request获得请求信息

Http请求:
这里写图片描述
因为request代表请求,所以我们可以通过该对象分别获得Http请求的请求行,请 求头和请求体。

1、通过request获得请求行

获得客户端的请求方式:String getMethod()
获得请求的资源:
String getRequestURI()
StringBuffer getRequestURL()
String getContextPath() —web应用的名称
String getQueryString() —- get提交url地址后的参数字符串username=zhangsan&password=123
注意:request获得客户机(客户端)的一些信息
request.getRemoteAddr() — 获得访问的客户端IP地址
案例演示:
login.html:
<html> <head> <meta charset="UTF-8"> <title>title> head> <body> <form action="/Servlet4" method="get"> 用户名:<input type="text" name="username"/><br /> 密码:<input type="password" name="password" /><br /> <input type="submit" value="登录" /> form> body> html> Servlet4: @WebServlet(name = "Servlet4", urlPatterns = {"/Servlet4"}) public class Servlet4 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取请求方式 String method = request.getMethod(); System.out.println(method); //获得请求的资源 String requestURI = request.getRequestURI(); StringBuffer requestURL = request.getRequestURL(); System.out.println("URI:"+requestURI); System.out.println("URL:"+requestURL); //获得WEB应用名称 String contextPath = request.getContextPath(); System.out.println("WEB应用:"+contextPath); //地址后的参数字符串 String queryString = request.getQueryString(); System.out.println(queryString); } } 注意:request获得客户机(客户端)的一些信息
request.getRemoteAddr() — 获得访问的客户端IP地址

2、通过request获得请求头

主要方法有:
long getDateHeader(String name)
String getHeader(String name)
Enumeration getHeaderNames()
Enumeration getHeaders(String name)
int getIntHeader(String name)

3、通过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”);

案例演示:
login1.html:
<html> <head> <meta charset="UTF-8"> <title>title> head> <body> <form action="/Servlet5" method="get"> 用户名:<input type="text" name="username"/><br /> 密码:<input type="password" name="password" /><br /> <input type="checkbox" name="hobby" value="zq">足球 <input type="checkbox" name="hobby" value="pq">排球 <input type="checkbox" name="hobby" value="lq">篮球 <input type="submit" value="提交" /> form> body> html> Servlet5: @WebServlet(name = "Servlet5", urlPatterns = {"/Servlet5"}) public class Servlet5 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获得单个表单值 String username = request.getParameter("username"); System.out.println(username); String password = request.getParameter("password"); System.out.println(password); //获得多个表单值 String[] hobbies = request.getParameterValues("hobby"); for(String hobby:hobbies){ System.out.println(hobby); } System.out.println("---------"); //获得所有的参数,参数封装到一个map中 Map parameterMap = request.getParameterMap(); for(Map.Entry entry:parameterMap.entrySet()){ System.out.println(entry.getKey()); for(String str:entry.getValue()){ System.out.println(str); } } } } 运行结果:
tom
123
zq
lq
~~~~~~~~~~~~~
username
tom
password
123
hobby
zq
lq

四、Request其他功能

4.1、request是一个域对象

request对象也是一个存储数据的区域对象,所以也具有如下方法:
setAttribute(String name, Object o)
getAttribute(String name)
removeAttribute(String name)
注意:request域的作用范围:一次请求中

4.2、request完成请求转发

获得请求转发器—-path是转发的地址
RequestDispatcher getRequestDispatcher(String path)
通过转发器对象转发
requestDispathcer.forward(ServletRequest request, ServletResponse response)
注意:ServletContext域与Request域的生命周期比较?
ServletContext:
    创建:服务器启动
    销毁:服务器关闭
    域的作用范围:整个web应用
request:
    创建:访问时创建request
    销毁:响应结束request销毁
    域的作用范围:一次请求中
注意:转发与重定向的区别?
    1)重定向两次请求,转发一次请求
    2)重定向地址栏的地址变化,转发地址不变
    3)重新定向可以访问外部网站 转发只能访问内部资源
    4)转发的性能要优于重定向
注意:客户端地址与服务器端地址的写法?
客户端地址:
    是客户端去访问服务器的地址,服务器外部的地址,特点:写上web应用名称
服务器端地址:
    服务器内部资源的跳转的地址,特点:不需要写web应用的名称
@WebServlet(name = "Servlet6", urlPatterns = {"/Servlet6"}) public class Servlet6 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //转发到Servlet3 RequestDispatcher dispatcher = request.getRequestDispatcher("/Servlet3"); dispatcher.forward(request,response); } }

五、案例:用户注册功能

前端代码: <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>龙心商城首页title> <link rel="stylesheet" href="css/bootstrap.min.css" type="text/css" /> <script src="js/jquery-1.11.3.min.js" type="text/javascript">script> <script src="js/bootstrap.min.js" type="text/javascript">script> head> <body> <div class="container-fluid"> <jsp:include page="/header.jsp">jsp:include> <div class="container-fluid"> <div id="carousel-example-generic" class="carousel slide" data-ride="carousel"> <ol class="carousel-indicators"> <li data-target="#carousel-example-generic" data-slide-to="0" class="active">li> <li data-target="#carousel-example-generic" data-slide-to="1">li> <li data-target="#carousel-example-generic" data-slide-to="2">li> ol> <div class="carousel-inner" role="listbox"> <div class="item active"> <img src="img/1.jpg"> <div class="carousel-caption"> div> div> <div class="item"> <img src="img/2.jpg"> <div class="carousel-caption"> div> div> <div class="item"> <img src="img/3.jpg"> <div class="carousel-caption"> div> div> div> <a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev"> <span class="glyphicon glyphicon-chevron-left" aria-hidden="true">span> <span class="sr-only">Previousspan> a> <a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next"> <span class="glyphicon glyphicon-chevron-right" aria-hidden="true">span> <span class="sr-only">Nextspan> a> div> div> <div class="container-fluid"> <div class="col-md-12"> <h2>热门商品  <img src="img/title2.jpg"/>h2> div> <div class="col-md-2" style="border:1px solid #E7E7E7;border-right:0;padding:0;"> <img src="products/hao/big01.jpg" width="205" height="404" style="display: inline-block;"/> div> <div class="col-md-10"> <div class="col-md-6" style="text-align:center;height:200px;padding:0px;"> <a href="product_info.htm"> <img src="products/hao/middle01.jpg" width="516px" height="200px" style="display: inline-block;"> a> div> <div class="col-md-2" style="text-align:center;height:200px;padding:10px 0px;"> <a href="product_info.htm"> <img src="products/hao/small03.jpg" width="130" height="130" style="display: inline-block;"> a> <p><a href="product_info.html" style='color:#666'>冬瓜a>p> <p><font color="#E4393C" style="font-size:16px">¥299.00font>p> div> <div class="col-md-2" style="text-align:center;height:200px;padding:10px 0px;"> <a href="product_info.htm"> <img src="products/hao/small04.jpg" width="130" height="130" style="display: inline-block;"> a> <p><a href="product_info.html" style='color:#666'>冬瓜a>p> <p><font color="#E4393C" style="font-size:16px">¥299.00font>p> div> <div class="col-md-2 yes-right-border" style="text-align:center;height:200px;padding:10px 0px;"> <a href="product_info.htm"> <img src="products/hao/small05.jpg" width="130" height="130" style="display: inline-block;"> a> <p><a href="product_info.html" style='color:#666'>冬瓜a>p> <p><font color="#E4393C" style="font-size:16px">¥299.00font>p> div> <div class="col-md-2" style="text-align:center;height:200px;padding:10px 0px;"> <a href="product_info.htm"> <img src="products/hao/small03.jpg" width="130" height="130" style="display: inline-block;"> a> <p><a href="product_info.html" style='color:#666'>冬瓜a>p> <p><font color="#E4393C" style="font-size:16px">¥299.00font>p> div> <div class="col-md-2" style="text-align:center;height:200px;padding:10px 0px;"> <a href="product_info.htm"> <img src="products/hao/small04.jpg" width="130" height="130" style="display: inline-block;"> a> <p><a href="product_info.html" style='color:#666'>冬瓜a>p> <p><font color="#E4393C" style="font-size:16px">¥299.00font>p> div> <div class="col-md-2 yes-right-border" style="text-align:center;height:200px;padding:10px 0px;"> <a href="product_info.htm"> <img src="products/hao/small05.jpg" width="130" height="130" style="display: inline-block;"> a> <p><a href="product_info.html" style='color:#666'>冬瓜a>p> <p><font color="#E4393C" style="font-size:16px">¥299.00font>p> div> <div class="col-md-2" style="text-align:center;height:200px;padding:10px 0px;"> <a href="product_info.htm"> <img src="products/hao/small03.jpg" width="130" height="130" style="display: inline-block;"> a> <p><a href="product_info.html" style='color:#666'>冬瓜a>p> <p><font color="#E4393C" style="font-size:16px">¥299.00font>p> div> <div class="col-md-2" style="text-align:center;height:200px;padding:10px 0px;"> <a href="product_info.htm"> <img src="products/hao/small04.jpg" width="130" height="130" style="display: inline-block;"> a> <p><a href="product_info.html" style='color:#666'>冬瓜a>p> <p><font color="#E4393C" style="font-size:16px">¥299.00font>p> div> <div class="col-md-2" style="text-align:center;height:200px;padding:10px 0px;"> <a href="product_info.htm"> <img src="products/hao/small05.jpg" width="130" height="130" style="display: inline-block;"> a> <p><a href="product_info.html" style='color:#666'>冬瓜a>p> <p><font color="#E4393C" style="font-size:16px">¥299.00font>p> div> div> div> <div class="container-fluid"> <img src="products/hao/ad.jpg" width="100%"/> div> <div class="container-fluid"> <div class="col-md-12"> <h2>最新商品  <img src="img/title2.jpg"/>h2> div> <div class="col-md-2" style="border:1px solid #E7E7E7;border-right:0;padding:0;"> <img src="products/hao/big01.jpg" width="205" height="404" style="display: inline-block;"/> div> <div class="col-md-10"> <div class="col-md-6" style="text-align:center;height:200px;padding:0px;"> <a href="product_info.htm"> <img src="products/hao/middle01.jpg" width="516px" height="200px" style="display: inline-block;"> a> div> <div class="col-md-2" style="text-align:center;height:200px;padding:10px 0px;"> <a href="product_info.htm"> <img src="products/hao/small03.jpg" width="130" height="130" style="display: inline-block;"> a> <p><a href="product_info.html" style='color:#666'>冬瓜a>p> <p><font color="#E4393C" style="font-size:16px">¥299.00font>p> div> <div class="col-md-2" style="text-align:center;height:200px;padding:10px 0px;"> <a href="product_info.htm"> <img src="products/hao/small04.jpg" width="130" height="130" style="display: inline-block;"> a> <p><a href="product_info.html" style='color:#666'>冬瓜a>p> <p><font color="#E4393C" style="font-size:16px">¥299.00font>p> div> <div class="col-md-2 yes-right-border" style="text-align:center;height:200px;padding:10px 0px;"> <a href="product_info.htm"> <img src="products/hao/small05.jpg" width="130" height="130" style="display: inline-block;"> a> <p><a href="product_info.html" style='color:#666'>冬瓜a>p> <p><font color="#E4393C" style="font-size:16px">¥299.00font>p> div> <div class="col-md-2" style="text-align:center;height:200px;padding:10px 0px;"> <a href="product_info.htm"> <img src="products/hao/small03.jpg" width="130" height="130" style="display: inline-block;"> a> <p><a href="product_info.html" style='color:#666'>冬瓜a>p> <p><font color="#E4393C" style="font-size:16px">¥299.00font>p> div> <div class="col-md-2" style="text-align:center;height:200px;padding:10px 0px;"> <a href="product_info.htm"> <img src="products/hao/small04.jpg" width="130" height="130" style="display: inline-block;"> a> <p><a href="product_info.html" style='color:#666'>冬瓜a>p> <p><font color="#E4393C" style="font-size:16px">¥299.00font>p> div> <div class="col-md-2 yes-right-border" style="text-align:center;height:200px;padding:10px 0px;"> <a href="product_info.htm"> <img src="products/hao/small05.jpg" width="130" height="130" style="display: inline-block;"> a> <p>