HttpServletRequest看这篇文章就够了
精彩內容推薦
-
一套java架構師學習資源,等你拿
-
你所需要的大數據視頻教程
-
java全套學習視頻教程及源碼
-
微服務資源springboot、springcloud、docker、dubbo項目實戰等傾心分享
一、HttpServletRequest介紹
HttpServletRequest對象代表客戶端的請求,當客戶端通過HTTP協議訪問服務器時,HTTP請求頭中的所有信息都封裝在這個對象中,通過這個對象提供的方法,可以獲得客戶端請求的所有信息。
二、jsp頁面引入js,css文件的方式
在eclipse中新建一個web項目,目錄結構如下:
image
在jsp頁面的最開始,獲取項目的根路徑:
<%String?path?=?request.getContextPath();String?basePath?=?request.getScheme()?+?"://"+?request.getServerName()?+?":"?+?request.getServerPort()+?path?+?"/"; %>在中,插入下述代碼:
<base?href="<%=basePath%>"?/>這句代碼的作用是將整個頁面的根路徑設置為項目路徑。
三、Request常用方法
1、獲得客戶機信息
| getRequestURI() | 返回請求行中的資源名部分。 |
| getQueryString () | 返回請求行中的參數部分。 |
| getRemoteAddr() | 返回發出請求的客戶機的IP地址。 |
| getPathInfo() | 返回請求URL中的額外路徑信息。額外路徑信息是請求URL中的位于Servlet的路徑之后和查詢參數之前的內容,它以"/"開頭。 |
| getRemoteHost() | 返回發出請求的客戶機的完整主機名。 |
| getRemotePort() | 返回客戶機所使用的網絡端口號。 |
| getLocalAddr() | 返回WEB服務器的IP地址。 |
| getLocalName() | 返回WEB服務器的主機名。 |
2.png
2、獲得客戶機請求頭
-
getHeader(string name)方法:String
-
getHeaders(String name)方法:Enumeration
-
getHeaderNames()方法
image
3、獲得客戶機請求參數
| getParameterValues(String name) | 根據name獲取請求參數列表(常用) |
| getParameterMap() | 返回的是一個Map類型的值,該返回值記錄著前端(如jsp頁面)所提交請求中的請求參數和請求參數值的映射關系。(編寫框架時常用) |
如下表單:
<%@?page?language="java"?contentType="text/html;?charset=UTF-8"pageEncoding="UTF-8"%> <%String?path?=?request.getContextPath();String?basePath?=?request.getScheme()?+?"://"+?request.getServerName()?+?":"?+?request.getServerPort()+?path?+?"/"; %> <html> <head> <base?href="<%=basePath%>"?/> <meta?http-equiv="Content-Type"?content="text/html;charset=UTF-8"> <title>表單提交</title> <link?href="css/bootstrap.css"?rel="stylesheet"> <script?src="js/jquery-3.2.1.js"></script> <script?src="js/bootstrap.js"></script> </head> <body><form?class="form-horizontal"?action="<%=request.getContextPath()%>/GetParameterRequest.html"?role="form"?method="post"><div?class="form-group"><label?for="firstname"?class="col-sm-1?control-label">名字</label><div?class="col-sm-3"><input?type="text"?class="form-control"?name="name"placeholder="請輸入名字"></div></div><div?class="form-group"><label?for="lastname"?class="col-sm-1?control-label">年齡</label><div?class="col-sm-3"><input?type="text"?class="form-control"?name="age"placeholder="請輸年齡"></div></div><div?class="form-group"><label?for="lastname"?class="col-sm-1?control-label">性別</label><div?class="col-sm-3"><input?type="radio"?name="sex"?value="男"?checked>男<input?type="radio"?name="sex"?value="女">女</div></div><div?class="form-group"><label?for="lastname"?class="col-sm-1?control-label">愛好</label><div?class="col-sm-3"><input?type="checkbox"?name="aihao"?value="唱歌">唱歌<input?type="checkbox"?name="aihao"?value="上網">上網<input?type="checkbox"?name="aihao"?value="游戲">游戲<input?type="checkbox"?name="aihao"?value="看書">看書</div></div><div?class="form-group"><div?class="col-sm-offset-1?col-sm-3"><button?type="submit"?class="btn?btn-default">提交</button><button?type="reset"?class="btn?btn-default">重置</button></div></div></form> </body> </html>使用getParameter方法和getParameterValues方法接收表單參數:
public?class?GetParameterRequest?extends?HttpServlet{private?static?final?long?serialVersionUID?=?3903946972744326948L;protected?void?doGet(HttpServletRequest?req,?HttpServletResponse?resp)?throws?ServletException,?IOException?{this.doPost(req,?resp);}protected?void?doPost(HttpServletRequest?req,?HttpServletResponse?resp)?throws?ServletException,?IOException?{//客戶端是以UTF-8編碼提交表單數據的,所以需要設置服務器端以UTF-8的編碼進行接收,否則對于中文數據就會產生亂碼req.setCharacterEncoding("UTF-8");//獲取名字String?name?=?req.getParameter("name");//獲取年齡String?age?=?req.getParameter("age");//獲取性別String?sex?=?req.getParameter("sex");//獲取愛好,因為可以選中多個值,所以獲取到的值是一個字符串數組,因此需要使用getParameterValues方法來獲取String[]?aihaos?=?req.getParameterValues("aihao");String?aihao?=?"";if(aihaos?!=?null){for?(int?i?=?0;?i?<?aihaos.length;?i++)?{if(i?==?aihaos.length?-?1){aihao?+=?aihaos[i];}?else?{aihao?+=?aihaos[i]?+?",";}}}System.out.println("名字:"?+?name);System.out.println("年齡:"?+?age);System.out.println("性別:"?+?sex);System.out.println("愛好:"?+?aihao);req.setAttribute("aihao",?aihao);//設置服務器端以UTF-8編碼輸出數據到客戶端resp.setCharacterEncoding("UTF-8");this.getServletContext().getRequestDispatcher("/request.jsp").forward(req,?resp);} }響應頁面:
<%@?page?language="java"?contentType="text/html;?charset=UTF-8"pageEncoding="UTF-8"%> <%String?path?=?request.getContextPath();String?basePath?=?request.getScheme()?+?"://"+?request.getServerName()?+?":"?+?request.getServerPort()+?path?+?"/"; %> <html> <head> <base?href="<%=basePath%>"?/> <meta?http-equiv="Content-Type"?content="text/html;charset=UTF-8"> <title>表單提交</title> <link?href="css/bootstrap.css"?rel="stylesheet"> <script?src="js/jquery-3.2.1.js"></script> <script?src="js/bootstrap.js"></script> </head> <body> <table?class="table"><thead><tr><th>名稱</th><th>結果</th></tr></thead><tbody><tr><td>姓名</td><td><%=request.getParameter("name")?%></td></tr><tr><td>年齡</td><td><%=request.getParameter("age")?%></td></tr><tr><td>性別</td><td><%=request.getParameter("sex")?%></td></tr><tr><td>愛好</td><td><%=request.getAttribute("aihao")?%></td></tr></tbody> </table> </body> </html>提交如下表單:
image
后臺打印:
image
運行結果如下:
image
四、request接收表單提交中文參數亂碼問題
1、以POST方式提交表單中文參數的亂碼問題
有如下表單:
<%@?page?language="java"?contentType="text/html;?charset=UTF-8"pageEncoding="UTF-8"%> <%String?path?=?request.getContextPath();String?basePath?=?request.getScheme()?+?"://"+?request.getServerName()?+?":"?+?request.getServerPort()+?path?+?"/"; %> <html> <head> <base?href="<%=basePath%>"?/> <meta?http-equiv="Content-Type"?content="text/html;charset=UTF-8"> <title>表單提交</title> <link?href="css/bootstrap.css"?rel="stylesheet"> <script?src="js/jquery-3.2.1.js"></script> <script?src="js/bootstrap.js"></script> </head> <body><form?class="form-horizontal"?action="<%=request.getContextPath()%>/PostRequest.html"?role="form"?method="post"><div?class="form-group"><label?for="firstname"?class="col-sm-1?control-label">名字</label><div?class="col-sm-3"><input?type="text"?class="form-control"?name="name"placeholder="請輸入名字"></div></div><div?class="form-group"><div?class="col-sm-offset-1?col-sm-3"><button?type="submit"?class="btn?btn-default">提交</button><button?type="reset"?class="btn?btn-default">重置</button></div></div></form> </body> </html>后臺接收參數:
public?class?PostRequest?extends?HttpServlet{private?static?final?long?serialVersionUID?=?3903946972744326948L;protected?void?doGet(HttpServletRequest?req,?HttpServletResponse?resp)?throws?ServletException,?IOException?{this.doPost(req,?resp);}protected?void?doPost(HttpServletRequest?req,?HttpServletResponse?resp)?throws?ServletException,?IOException?{String?name?=?req.getParameter("name");System.out.println("名字:"?+?name);} }提交數據:
image
運行結果:
image
之所以會產生亂碼,就是因為服務器和客戶端溝通的編碼不一致造成的,因此解決的辦法是:在客戶端和服務器之間設置一個統一的編碼,之后就按照此編碼進行數據的傳輸和接收。
由于客戶端是以UTF-8字符編碼將表單數據傳輸到服務器端的,因此服務器也需要設置以UTF-8字符編碼進行接收,通過setCharacterEncoding方法統一編碼格式:
public?class?PostRequest?extends?HttpServlet{private?static?final?long?serialVersionUID?=?3903946972744326948L;protected?void?doGet(HttpServletRequest?req,?HttpServletResponse?resp)?throws?ServletException,?IOException?{this.doPost(req,?resp);}protected?void?doPost(HttpServletRequest?req,?HttpServletResponse?resp)?throws?ServletException,?IOException?{//設置服務器以UTF-8的編碼接收數據req.setCharacterEncoding("UTF-8");String?name?=?req.getParameter("name");System.out.println("名字:"?+?name);} }重新提交表單,中文亂碼解決:
image
2、以GET方式提交表單中文參數的亂碼問題
有如下表單:
<%@?page?language="java"?contentType="text/html;?charset=UTF-8"pageEncoding="UTF-8"%> <%String?path?=?request.getContextPath();String?basePath?=?request.getScheme()?+?"://"+?request.getServerName()?+?":"?+?request.getServerPort()+?path?+?"/"; %> <html> <head> <base?href="<%=basePath%>"?/> <meta?http-equiv="Content-Type"?content="text/html;charset=UTF-8"> <title>表單提交</title> <link?href="css/bootstrap.css"?rel="stylesheet"> <script?src="js/jquery-3.2.1.js"></script> <script?src="js/bootstrap.js"></script> </head> <body><form?class="form-horizontal"?action="<%=request.getContextPath()%>/GetRequest.html"?role="form"?method="get"><div?class="form-group"><label?for="firstname"?class="col-sm-1?control-label">名字</label><div?class="col-sm-3"><input?type="text"?class="form-control"?name="name"placeholder="請輸入名字"></div></div><div?class="form-group"><div?class="col-sm-offset-1?col-sm-3"><button?type="submit"?class="btn?btn-default">提交</button><button?type="reset"?class="btn?btn-default">重置</button></div></div></form> </body> </html>后臺接收參數:
public?class?GetRequest?extends?HttpServlet{private?static?final?long?serialVersionUID?=?3903946972744326948L;protected?void?doGet(HttpServletRequest?req,?HttpServletResponse?resp)?throws?ServletException,?IOException?{this.doPost(req,?resp);}protected?void?doPost(HttpServletRequest?req,?HttpServletResponse?resp)?throws?ServletException,?IOException?{String?name?=?req.getParameter("name");System.out.println("名字:"?+?name);} }提交數據:
image
運行結果:
image
之所以會產生亂碼,對于以get方式傳輸的數據,默認的還是使用ISO8859-1這個字符編碼來接收數據,客戶端以UTF-8的編碼傳輸數據到服務器端,而服務器端的request對象使用的是ISO8859-1這個字符編碼來接收數據,服務器和客戶端溝通的編碼不一致因此才會產生中文亂碼的。
解決方法:
在接收到數據后,先獲取request對象以ISO8859-1字符編碼接收到的原始數據的字節數組,然后通過字節數組以指定的編碼構建字符串
public?class?GetRequest?extends?HttpServlet{private?static?final?long?serialVersionUID?=?3903946972744326948L;protected?void?doGet(HttpServletRequest?req,?HttpServletResponse?resp)?throws?ServletException,?IOException?{this.doPost(req,?resp);}protected?void?doPost(HttpServletRequest?req,?HttpServletResponse?resp)?throws?ServletException,?IOException?{String?name?=?req.getParameter("name");//以ISO8859-1字符編碼接收到的原始數據的字節數組,然后通過字節數組以指定的編碼構建字符串name?=?new?String(name.getBytes("ISO8859-1")?,?"UTF-8");System.out.println("名字:"?+?name);} }重新提交表單,中文亂碼解決:
image
五、Request對象實現請求轉發
4.1、請求轉發的基本概念
請求轉發:指一個web資源收到客戶端請求后,通知服務器去調用另外一個web資源進行處理。
請求轉發的應用場景:MVC設計模式
在Servlet中實現請求轉發的兩種方式:
1、通過ServletContext的getRequestDispatcher(String path)方法,該方法返回一個RequestDispatcher對象,調用這個對象的forward方法可以實現請求轉發。
例如:將請求轉發的test.jsp頁面
RequestDispatcher?reqDispatcher?=this.getServletContext().getRequestDispatcher("/test.jsp"); reqDispatcher.forward(request,?response);** 2、通過request對象提供的getRequestDispatche(String path)方法,該方法返回一個RequestDispatcher對象,調用這個對象的forward方法可以實現請求轉發。**
例如:將請求轉發的test.jsp頁面
request.getRequestDispatcher("/test.jsp").forward(request,?response);request對象同時也是一個域對象(Map容器),開發人員通過request對象在實現轉發時,把數據通過request對象帶給其它web資源處理。
例如:請求RequestDemo06 Servlet,RequestDemo06將請求轉發到test.jsp頁面
package?gacl.request.study;import?java.io.IOException; import?javax.servlet.ServletException; import?javax.servlet.http.HttpServlet; import?javax.servlet.http.HttpServletRequest; import?javax.servlet.http.HttpServletResponse;public?class?RequestDemo06?extends?HttpServlet?{public?void?doGet(HttpServletRequest?request,?HttpServletResponse?response)throws?ServletException,?IOException?{String?data="大家好,我是孤傲蒼狼,我正在總結JavaWeb";/***?將數據存放到request對象中,此時把request對象當作一個Map容器來使用*/request.setAttribute("data",?data);//客戶端訪問RequestDemo06這個Servlet后,RequestDemo06通知服務器將請求轉發(forward)到test.jsp頁面進行處理request.getRequestDispatcher("/test.jsp").forward(request,?response);}public?void?doPost(HttpServletRequest?request,?HttpServletResponse?response)throws?ServletException,?IOException?{doGet(request,?response);} }test.jsp頁面代碼如下:
<%@?page?language="java"?import="java.util.*"?pageEncoding="UTF-8"%> <html><head><title>Request對象實現請求轉發</title></head><body>使用普通方式取出存儲在request對象中的數據:<h3?style="color:red;"><%=(String)request.getAttribute("data")%></h3>使用EL表達式取出存儲在request對象中的數據:<h3?style="color:red;">${data}</h3></body> </html>request對象作為一個域對象(Map容器)使用時,主要是通過以下的四個方法來操作
- setAttribute(String name,Object o)方法,將數據作為request對象的一個屬性存放到request對象中,例如:request.setAttribute("data", data);
- getAttribute(String name)方法,獲取request對象的name屬性的屬性值,例如:request.getAttribute("data")
- removeAttribute(String name)方法,移除request對象的name屬性,例如:request.removeAttribute("data")
- getAttributeNames方法,獲取request對象的所有屬性名,返回的是一個,例如:Enumeration attrNames = request.getAttributeNames();
4.2、請求重定向和請求轉發的區別
一個web資源收到客戶端請求后,通知服務器去調用另外一個web資源進行處理,稱之為請求轉發/307。
一個web資源收到客戶端請求后,通知瀏覽器去訪問另外一個web資源進行處理,稱之為請求重定向/302。
參考資料
http://www.cnblogs.com/xdp-gacl/p/3798347.html https://www.cnblogs.com/Zender/p/7647503.html精彩內容推薦
-
一套java架構師學習資源,等你拿
-
你所需要的大數據視頻教程
-
java全套學習視頻教程及源碼
-
微服務資源springboot、springcloud、docker、dubbo項目實戰等傾心分享
文章有不當之處,歡迎指正,如果喜歡微信閱讀,你也可以關注我的微信公眾號:好好學java,獲取優質學習資源。
總結
以上是生活随笔為你收集整理的HttpServletRequest看这篇文章就够了的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 轻松看懂java设计模式简单工厂模式
- 下一篇: 面试官问的hibernate和mybat