HttpServletRequest對象代表客戶端的請求,當客戶端通過HTTP協議訪問服務器時,HTTP請求頭中的所有信息都封裝在這個對象中,通過這個對象提供的方法,可以獲得客戶端請求的所有信息。
獲得客戶機信息getRequestURL方法返回客戶端發出請求時的完整URL。getRequestURI方法返回請求行中的資源名部分。getQueryString 方法返回請求行中的參數部分。getPathInfo方法返回請求URL中的額外路徑信息。額外路徑信息是請求URL中的位于Servlet的路徑之后和查詢參數之前的內容,它以“/”開頭。getRemoteAddr方法返回發出請求的客戶機的ip地址。getRemoteHost方法返回發出請求的客戶機的完整主機名。getRemotePort方法返回客戶機所使用的網絡端口號。getLocalAddr方法返回WEB服務器的IP地址。getLocalName方法返回WEB服務器的主機名。
通過request對象獲取客戶端請求信息
public class RequestDemo01 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /** * 1.獲得客戶機信息 */ String requestUrl = request.getRequestURL().toString();//得到請求的URL地址 String requestUri = request.getRequestURI();//得到請求的資源 String queryString = request.getQueryString();//得到請求的URL地址中附帶的參數 String remoteAddr = request.getRemoteAddr();//得到來訪者的IP地址 String remoteHost = request.getRemoteHost(); int remotePort = request.getRemotePort(); String remoteUser = request.getRemoteUser(); String method = request.getMethod();//得到請求URL地址時使用的方法 String pathInfo = request.getPathInfo(); String localAddr = request.getLocalAddr();//獲取WEB服務器的IP地址 String localName = request.getLocalName();//獲取WEB服務器的主機名 response.setCharacterEncoding("UTF-8");//設置將字符以"UTF-8"編碼輸出到客戶端瀏覽器 //通過設置響應頭控制瀏覽器以UTF-8的編碼顯示數據,如果不加這句話,那么瀏覽器顯示的將是亂碼 response.setHeader("content-type", "text/html;charset=UTF-8"); PRintWriter out = response.getWriter(); out.write("獲取到的客戶機信息如下:"); out.write("<hr/>"); out.write("請求的URL地址:"+requestUrl); out.write("<br/>"); out.write("請求的資源:"+requestUri); out.write("<br/>"); out.write("請求的URL地址中附帶的參數:"+queryString); out.write("<br/>"); out.write("來訪者的IP地址:"+remoteAddr); out.write("<br/>"); out.write("來訪者的主機名:"+remoteHost); out.write("<br/>"); out.write("使用的端口號:"+remotePort); out.write("<br/>"); out.write("remoteUser:"+remoteUser); out.write("<br/>"); out.write("請求使用的方法:"+method); out.write("<br/>"); out.write("pathInfo:"+pathInfo); out.write("<br/>"); out.write("localAddr:"+localAddr); out.write("<br/>"); out.write("localName:"+localName); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }}
getHeader(string name)方法:StringgetHeaders(String name)方法:EnumerationgetHeaderNames()方法
/** * @author gacl * 獲取客戶端請求頭信息 * 客戶端請求頭: * */public class RequestDemo02 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("UTF-8");//設置將字符以"UTF-8"編碼輸出到客戶端瀏覽器 //通過設置響應頭控制瀏覽器以UTF-8的編碼顯示數據 response.setHeader("content-type", "text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); Enumeration<String> reqHeadInfos = request.getHeaderNames();//獲取所有的請求頭 out.write("獲取到的客戶端所有的請求頭信息如下:"); out.write("<hr/>"); while (reqHeadInfos.hasMoreElements()) { String headName = (String) reqHeadInfos.nextElement(); String headValue = request.getHeader(headName);//根據請求頭的名字獲取對應的請求頭的值 out.write(headName+":"+headValue); out.write("<br/>"); } out.write("<br/>"); out.write("獲取到的客戶端Accept-Encoding請求頭的值:"); out.write("<hr/>"); String value = request.getHeader("Accept-Encoding");//獲取Accept-Encoding請求頭對應的值 out.write(value); Enumeration<String> e = request.getHeaders("Accept-Encoding"); while (e.hasMoreElements()) { String string = (String) e.nextElement(); System.out.println(string); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }}
getParameter(String)方法(常用)
getParameterValues(String name)方法(常用)
getParameterNames()方法(不常用)
getParameterMap()方法(編寫框架時常用)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><fieldset style="width:500px;"> <legend>Html的Form表單元素</legend>
<form action="${pageContext.request.contextPath}/servlet/RequestDemo03" method="post"> 編 號(文本框):<input type="text" name="userid" value="NO." size="2" maxlength="2"><br>用戶名(文本框):<input type="text" name="username" value="請輸入用戶名"><br> 密 碼(密碼框):<input type="passWord" name="userpass" value="請輸入密碼"><br>
性 別(單選框): <input type="radio" name="sex" value="男" checked>男 <input type="radio" name="sex" value="女">女<br> 部 門(下拉框): <select name="dept"> <option value="技術部">技術部</option> <option value="銷售部" SELECTED>銷售部</option> <option value="財務部">財務部</option> </select><br> 興 趣(復選框): <input type="checkbox" name="inst" value="唱歌">唱歌 <input type="checkbox" name="inst" value="游泳">游泳 <input type="checkbox" name="inst" value="跳舞">跳舞 <input type="checkbox" name="inst" value="編程" checked>編程 <input type="checkbox" name="inst" value="上網">上網 <br>
說 明(文本域): <textarea name="note" cols="34" rows="5"> </textarea> <br> <input type="hidden" name="hiddenField" value="hiddenvalue"/> <input type="submit" value="提交(提交按鈕)"> <input type="reset" value="重置(重置按鈕)"></form><!--表單結束--></fieldset>
在服務器端使用getParameter方法和getParameterValues方法接收表單參數:
/** * 獲取客戶端通過Form表單提交上來的參數 */public class RequestDemo03 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //客戶端是以UTF-8編碼提交表單數據的,所以需要設置服務器端以UTF-8的編碼進行接收,否則對于中文數據就會產生亂碼 request.setCharacterEncoding("UTF-8"); /** * 編 號(文本框): <input type="text" name="userid" value="NO." size="2" maxlength="2"> */ String userid = request.getParameter("userid");//獲取填寫的編號,userid是文本框的名字,<input type="text" name="userid"> /** * 用戶名(文本框):<input type="text" name="username" value="請輸入用戶名"> */ String username = request.getParameter("username");//獲取填寫的用戶名 /** * 密 碼(密碼框):<input type="password" name="userpass" value="請輸入密碼"> */ String userpass = request.getParameter("userpass");//獲取填寫的密碼 String sex = request.getParameter("sex");//獲取選中的性別 String dept = request.getParameter("dept");//獲取選中的部門 //獲取選中的興趣,因為可以選中多個值,所以獲取到的值是一個字符串數組,因此需要使用getParameterValues方法來獲取 String[] insts = request.getParameterValues("inst"); String note = request.getParameter("note");//獲取填寫的說明信息 String hiddenField = request.getParameter("hiddenField");//獲取隱藏域的內容 String instStr=""; /** * 獲取數組數據的技巧,可以避免insts數組為null時引發的空指針異常錯誤! */ for (int i = 0; insts!=null && i < insts.length; i++) { if (i == insts.length-1) { instStr+=insts[i]; }else { instStr+=insts[i]+","; } } String htmlStr = "<table>" + "<tr><td>填寫的編號:</td><td>{0}</td></tr>" + "<tr><td>填寫的用戶名:</td><td>{1}</td></tr>" + "<tr><td>填寫的密碼:</td><td>{2}</td></tr>" + "<tr><td>選中的性別:</td><td>{3}</td></tr>" + "<tr><td>選中的部門:</td><td>{4}</td></tr>" + "<tr><td>選中的興趣:</td><td>{5}</td></tr>" + "<tr><td>填寫的說明:</td><td>{6}</td></tr>" + "<tr><td>隱藏域的內容:</td><td>{7}</td></tr>" + "</table>"; htmlStr = MessageFormat.format(htmlStr, userid,username,userpass,sex,dept,instStr,note,hiddenField); response.setCharacterEncoding("UTF-8");//設置服務器端以UTF-8編碼輸出數據到客戶端 response.setContentType("text/html;charset=UTF-8");//設置客戶端瀏覽器以UTF-8編碼解析數據 response.getWriter().write(htmlStr);//輸出htmlStr里面的內容到客戶端瀏覽器顯示 } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }}在服務器端使用getParameterNames方法接收表單參數:
Enumeration<String> paramNames = request.getParameterNames();//獲取所有的參數名 while (paramNames.hasMoreElements()) { String name = paramNames.nextElement();//得到參數名 String value = request.getParameter(name);//通過參數名獲取對應的值 System.out.println(MessageFormat.format("{0}={1}", name,value)); }在服務器端使用getParameterMap方法接收表單參數:
//request對象封裝的參數是以Map的形式存儲的 Map<String, String[]> paramMap = request.getParameterMap(); for(Map.Entry<String, String[]> entry :paramMap.entrySet()){ String paramName = entry.getKey(); String paramValue = ""; String[] paramValueArr = entry.getValue(); for (int i = 0; paramValueArr!=null && i < paramValueArr.length; i++) { if (i == paramValueArr.length-1) { paramValue+=paramValueArr[i]; }else { paramValue+=paramValueArr[i]+","; } } System.out.println(MessageFormat.format("{0}={1}", paramName,paramValue)); }以POST方式提交表單中文參數的亂碼問題 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <form action="<%=request.getContextPath()%>/servlet/RequestDemo04" method="post"> 用戶名:<input type="text" name="userName"/> <input type="submit" value="post方式提交表單"> </form>
此時在服務器端接收中文參數時就會出現中文亂碼,如下所示:

之所以會產生亂碼,就是因為服務器和客戶端溝通的編碼不一致造成的,因此解決的辦法是:在客戶端和服務器之間設置一個統一的編碼,之后就按照此編碼進行數據的傳輸和接收。
由于客戶端是以UTF-8字符編碼將表單數據傳輸到服務器端的,因此服務器也需要設置以UTF-8字符編碼進行接收,要想完成此操作,服務器可以直接使用從ServletRequest接口繼承而來的"setCharacterEncoding(charset)"方法進行統一的編碼設置。
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /** * 客戶端是以UTF-8編碼傳輸數據到服務器端的,所以需要設置服務器端以UTF-8的編碼進行接收,否則對于中文數據就會產生亂碼 */ request.setCharacterEncoding("UTF-8"); String userName = request.getParameter("userName"); System.out.println("userName:"+userName);}使用request.setCharacterEncoding("UTF-8");設置服務器以UTF-8的編碼接收數據后,此時就不會產生中文亂碼問題了。
以GET方式提交表單中文參數的亂碼問題對于以get方式傳輸的數據,request即使設置了以指定的編碼接收數據也是無效的(至于為什么無效我也沒有弄明白),默認的還是使用ISO8859-1這個字符編碼來接收數據,客戶端以UTF-8的編碼傳輸數據到服務器端,而服務器端的request對象使用的是ISO8859-1這個字符編碼來接收數據,服務器和客戶端溝通的編碼不一致因此才會產生中文亂碼的。解決辦法:在接收到數據后,先獲取request對象以ISO8859-1字符編碼接收到的原始數據的字節數組,然后通過字節數組以指定的編碼構建字符串,解決亂碼問題。
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /** * 對于以get方式傳輸的數據,request即使設置了以指定的編碼接收數據也是無效的,默認的還是使用ISO8859-1這個字符編碼來接收數據 */ String name = request.getParameter("name");//接收數據 name =new String(name.getBytes("ISO8859-1"), "UTF-8") ;//獲取request對象以ISO8859-1字符編碼接收到的原始數據的字節數組,然后通過字節數組以指定的編碼構建字符串,解決亂碼問題 System.out.println("name:"+name); }以超鏈接形式傳遞中文參數的亂碼問題 客戶端想傳輸數據到服務器,可以通過表單提交的形式,也可以通過超鏈接后面加參數的形式:
<a href="${pageContext.request.contextPath}/servlet/RequestDemo05?userName=gacl&name=張三">點擊</a>點擊超鏈接,數據是以get的方式傳輸到服務器的,所以接收中文數據時也會產生中文亂碼問題,而解決中文亂碼問題的方式與上述的以get方式提交表單中文數據亂碼處理問題的方式一致,如下所示:
String name = request.getParameter("name");name =new String(name.getBytes("ISO8859-1"), "UTF-8");另外,需要提的一點就是URL地址后面如果跟了中文數據,那么中文參數最好使用URL編碼進行處理:
<a href="${pageContext.request.contextPath}/servlet/RequestDemo05?userName=gacl&name=<%=URLEncoder.encode("張三", "UTF-8")%>">點擊</a>提交中文數據亂碼問題總結 1、如果提交方式為post,想不亂碼,只需要在服務器端設置request對象的編碼即可,客戶端以哪種編碼提交的,服務器端的request對象就以對應的編碼接收,比如客戶端是以UTF-8編碼提交的,那么服務器端request對象就以UTF-8編碼接收(request.setCharacterEncoding("UTF-8"))
2、如果提交方式為get,設置request對象的編碼是無效的,request對象還是以默認的ISO8859-1編碼接收數據,因此要想不亂碼,只能在接收到數據后再手工轉換,步驟如下:
1).獲取獲取客戶端提交上來的數據,得到的是亂碼字符串,data="???è?????"
String data = request.getParameter("paramName");2).查找ISO8859-1碼表,得到客戶機提交的原始數據的字節數組
byte[] source = data.getBytes("ISO8859-1");3).通過字節數組以指定的編碼構建字符串,解決亂碼
data = new String(source, "UTF-8");
通過字節數組以指定的編碼構建字符串,這里指定的編碼是根據客戶端那邊提交數據時使用的字符編碼來定的,如果是GB2312,那么就設置成data = new String(source, "GB2312"),如果是UTF-8,那么就設置成data = new String(source, "UTF-8")
請求轉發的基本概念請求轉發:指一個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頁面
public class RequestDemo06 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String data="拉拉"; /** * 將數據存放到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容器)使用時,主要是通過以下的四個方法來操作
一個web資源收到客戶端請求后,通知服務器去調用另外一個web資源進行處理,稱之為請求轉發/307。一個web資源收到客戶端請求后,通知瀏覽器去訪問另外一個web資源進行處理,稱之為請求重定向/302。
我是天王蓋地虎的分割線參考:http://m.survivalescaperooms.com/xdp-gacl/p/3798347.html
新聞熱點
疑難解答