一、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、獲得客戶機信息
getRequestURL() | 返回客戶端發出請求時的完整URL。 |
---|---|
getRequestURI() | 返回請求行中的資源名部分。 |
getQueryString () | 返回請求行中的參數部分。 |
getRemoteAddr() | 返回發出請求的客戶機的IP地址。 |
getPathInfo() | 返回請求URL中的額外路徑信息。額外路徑信息是請求URL中的位於Servlet的路徑之後和查詢參數之前的內容,它以"/"開頭。 |
getRemoteHost() | 返回發出請求的客戶機的完整主機名。 |
getRemotePort() | 返回客戶機所使用的網絡端口號。 |
getLocalAddr() | 返回WEB服務器的IP地址。 |
getLocalName() | 返回WEB服務器的主機名。 |
private void RequestMessages(HttpServletRequest req, HttpServletResponse resp) throws IOException{ String reqUrl = req.getRequestURL().toString();//得到請求的URL地址 String reqUri = req.getRequestURI();//得到請求的資源 String queryString = req.getQueryString();//得到請求的URL地址中附帶的參數 String remoteAddr = req.getRemoteAddr();//得到來訪者的IP地址 String remoteHost = req.getRemoteHost(); int remotePort = req.getRemotePort(); String remoteUser = req.getRemoteUser(); String method = req.getMethod();//得到請求URL地址時使用的方法 String pathInfo = req.getPathInfo(); String localAddr = req.getLocalAddr();//獲取WEB服務器的IP地址 String localName = req.getLocalName();//獲取WEB服務器的主機名 resp.setCharacterEncoding("UTF-8");//設置將字符以"UTF-8"編碼輸出到客戶端瀏覽器 //通過設置響應頭控制瀏覽器以UTF-8的編碼顯示數據,如果不加這句話,那麼瀏覽器顯示的將是亂碼 resp.setHeader("content-type", "text/html;charset=UTF-8"); PrintWriter out = resp.getWriter(); out.write("獲取到的客戶機信息如下:"); out.write(" "); out.write("請求的URL地址:"+reqUrl); out.write(" "); out.write("請求的資源:"+reqUri); out.write(" "); out.write("請求的URL地址中附帶的參數:"+queryString); out.write(" "); out.write("來訪者的IP地址:"+remoteAddr); out.write(" "); out.write("來訪者的主機名:"+remoteHost); out.write(" "); out.write("使用的端口號:"+remotePort); out.write(" "); out.write("remoteUser:"+remoteUser); out.write(" "); out.write("請求使用的方法:"+method); out.write(" "); out.write("pathInfo:"+pathInfo); out.write(" "); out.write("localAddr:"+localAddr); out.write(" "); out.write("localName:"+localName); }
2.png
2、獲得客戶機請求頭
- getHeader(string name)方法:String
- getHeaders(String name)方法:Enumeration
- getHeaderNames()方法
private void RequestHead(HttpServletRequest req, HttpServletResponse resp) throws IOException{ resp.setCharacterEncoding("UTF-8");//設置將字符以"UTF-8"編碼輸出到客戶端瀏覽器 //通過設置響應頭控制瀏覽器以UTF-8的編碼顯示數據 resp.setHeader("content-type", "text/html;charset=UTF-8"); PrintWriter out = resp.getWriter(); Enumeration reqHeadInfos = req.getHeaderNames();//獲取所有的請求頭 out.write("獲取到的客戶端所有的請求頭信息如下:"); out.write(" "); while (reqHeadInfos.hasMoreElements()) { String headName = (String) reqHeadInfos.nextElement(); String headValue = req.getHeader(headName);//根據請求頭的名字獲取對應的請求頭的值 out.write(headName+":"+headValue); out.write(" "); } out.write(" "); out.write("獲取到的客戶端Accept-Encoding請求頭的值:"); out.write(" "); String value = req.getHeader("Accept-Encoding");//獲取Accept-Encoding請求頭對應的值 out.write(value); Enumeration e = req.getHeaders("Accept-Encoding"); while (e.hasMoreElements()) { String string = (String) e.nextElement(); System.out.println(string); } }
image
3、獲得客戶機請求參數
getParameter(String name) | 根據name獲取請求參數(常用) |
---|---|
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; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp); } @Override 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; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp); } @Override 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; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp); } @Override 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; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp); } @Override 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; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp); } @Override 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"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <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