自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

原來HTTP協(xié)議這么簡單!一文讀懂HttpServletRequest

網(wǎng)絡 網(wǎng)絡管理
HttpServletRequest對象代表客戶端的請求,當客戶端通過HTTP協(xié)議訪問服務器時,HTTP請求頭中的所有信息都封裝在這個對象中,通過這個對象提供的方法,可以獲得客戶端請求的所有信息。

 一、HttpServletRequest介紹

HttpServletRequest對象代表客戶端的請求,當客戶端通過HTTP協(xié)議訪問服務器時,HTTP請求頭中的所有信息都封裝在這個對象中,通過這個對象提供的方法,可以獲得客戶端請求的所有信息。

[[265155]]

二、jsp頁面引入js,css文件的方式

在eclipse中新建一個web項目,目錄結(jié)構(gòu)如下:

 

 

原來HTTP協(xié)議這么簡單!一文讀懂HttpServletRequest(周末快樂)

 

 

在jsp頁面的最開始,獲取項目的根路徑:

  1. <% 
  2.  String path = request.getContextPath(); 
  3.  String basePath = request.getScheme() + "://" 
  4.  + request.getServerName() + ":" + request.getServerPort() 
  5.  + path + "/"
  6. %> 

在中,插入下述代碼:

  1. <base href="<%=basePath%>" /> 

這句代碼的作用是將整個頁面的根路徑設置為項目路徑。

三、Request常用方法

1、獲得客戶機信息

getRequestURL()返回客戶端發(fā)出請求時的完整URL。getRequestURI()返回請求行中的資源名部分。getQueryString ()返回請求行中的參數(shù)部分。getRemoteAddr()返回發(fā)出請求的客戶機的IP地址。getPathInfo()返回請求URL中的額外路徑信息。額外路徑信息是請求URL中的位于Servlet的路徑之后和查詢參數(shù)之前的內(nèi)容,它以"/"開頭。getRemoteHost()返回發(fā)出請求的客戶機的完整主機名。getRemotePort()返回客戶機所使用的網(wǎng)絡端口號。getLocalAddr()返回WEB服務器的IP地址。getLocalName()返回WEB服務器的主機名。

  1. private void RequestMessages(HttpServletRequest req, HttpServletResponse resp) throws IOException{ 
  2.  String reqUrl = req.getRequestURL().toString();//得到請求的URL地址 
  3.  String reqUri = req.getRequestURI();//得到請求的資源 
  4.  String queryString = req.getQueryString();//得到請求的URL地址中附帶的參數(shù) 
  5.  String remoteAddr = req.getRemoteAddr();//得到來訪者的IP地址 
  6.  String remoteHost = req.getRemoteHost(); 
  7.  int remotePort = req.getRemotePort(); 
  8.  String remoteUser = req.getRemoteUser(); 
  9.  String method = req.getMethod();//得到請求URL地址時使用的方法 
  10.  String pathInfo = req.getPathInfo(); 
  11.  String localAddr = req.getLocalAddr();//獲取WEB服務器的IP地址 
  12.  String localName = req.getLocalName();//獲取WEB服務器的主機名 
  13.  resp.setCharacterEncoding("UTF-8");//設置將字符以"UTF-8"編碼輸出到客戶端瀏覽器 
  14.  //通過設置響應頭控制瀏覽器以UTF-8的編碼顯示數(shù)據(jù),如果不加這句話,那么瀏覽器顯示的將是亂碼 
  15.  resp.setHeader("content-type""text/html;charset=UTF-8"); 
  16.  PrintWriter out = resp.getWriter(); 
  17.  out.write("獲取到的客戶機信息如下:"); 
  18.  out.write("<br/>"); 
  19.  out.write("請求的URL地址:"+reqUrl); 
  20.  out.write("<br/>"); 
  21.  out.write("請求的資源:"+reqUri); 
  22.  out.write("<br/>"); 
  23.  out.write("請求的URL地址中附帶的參數(shù):"+queryString); 
  24.  out.write("<br/>"); 
  25.  out.write("來訪者的IP地址:"+remoteAddr); 
  26.  out.write("<br/>"); 
  27.  out.write("來訪者的主機名:"+remoteHost); 
  28.  out.write("<br/>"); 
  29.  out.write("使用的端口號:"+remotePort); 
  30.  out.write("<br/>"); 
  31.  out.write("remoteUser:"+remoteUser); 
  32.  out.write("<br/>"); 
  33.  out.write("請求使用的方法:"+method); 
  34.  out.write("<br/>"); 
  35.  out.write("pathInfo:"+pathInfo); 
  36.  out.write("<br/>"); 
  37.  out.write("localAddr:"+localAddr); 
  38.  out.write("<br/>"); 
  39.  out.write("localName:"+localName); 

原來HTTP協(xié)議這么簡單!一文讀懂HttpServletRequest(周末快樂)

2、獲得客戶機請求頭

  • getHeader(string name)方法:String
  • getHeaders(String name)方法:Enumeration
  • getHeaderNames()方法
    1. private void RequestHead(HttpServletRequest req, HttpServletResponse resp) throws IOException{ 
    2.  resp.setCharacterEncoding("UTF-8");//設置將字符以"UTF-8"編碼輸出到客戶端瀏覽器 
    3.  //通過設置響應頭控制瀏覽器以UTF-8的編碼顯示數(shù)據(jù) 
    4.  resp.setHeader("content-type""text/html;charset=UTF-8"); 
    5.  PrintWriter out = resp.getWriter(); 
    6.  Enumeration<String> reqHeadInfos = req.getHeaderNames();//獲取所有的請求頭 
    7.  out.write("獲取到的客戶端所有的請求頭信息如下:"); 
    8.  out.write("<br/>"); 
    9.  while (reqHeadInfos.hasMoreElements()) { 
    10.  String headName = (String) reqHeadInfos.nextElement(); 
    11.  String headValue = req.getHeader(headName);//根據(jù)請求頭的名字獲取對應的請求頭的值 
    12.  out.write(headName+":"+headValue); 
    13.  out.write("<br/>"); 
    14.  } 
    15.  out.write("<br/>"); 
    16.  out.write("獲取到的客戶端Accept-Encoding請求頭的值:"); 
    17.  out.write("<br/>"); 
    18.  String value = req.getHeader("Accept-Encoding");//獲取Accept-Encoding請求頭對應的值 
    19.  out.write(value); 
    20.  Enumeration<String> e = req.getHeaders("Accept-Encoding"); 
    21.  while (e.hasMoreElements()) { 
    22.  String string = (String) e.nextElement(); 
    23.  System.out.println(string); 
    24.  } 

 

原來HTTP協(xié)議這么簡單!一文讀懂HttpServletRequest(周末快樂)

 

3、獲得客戶機請求參數(shù)

getParameter(String name)根據(jù)name獲取請求參數(shù)(常用)getParameterValues(String name)根據(jù)name獲取請求參數(shù)列表(常用)getParameterMap()返回的是一個Map類型的值,該返回值記錄著前端(如jsp頁面)所提交請求中的請求參數(shù)和請求參數(shù)值的映射關系。(編寫框架時常用)

  1. <%@ page language="java" contentType="text/html; charset=UTF-8" 
  2.  pageEncoding="UTF-8"%> 
  3. <% 
  4.  String path = request.getContextPath(); 
  5.  String basePath = request.getScheme() + "://" 
  6.  + request.getServerName() + ":" + request.getServerPort() 
  7.  + path + "/"
  8. %> 
  9. <html> 
  10. <head> 
  11. <base href="<%=basePath%>" /> 
  12. <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"
  13. <title>表單提交</title> 
  14. <link href="css/bootstrap.css" rel="stylesheet"
  15. <script src="js/jquery-3.2.1.js"></script> 
  16. <script src="js/bootstrap.js"></script> 
  17. </head> 
  18. <body> 
  19.  <form class="form-horizontal" action="<%=request.getContextPath()%>/GetParameterRequest.html" role="form" method="post"
  20.  <div class="form-group"
  21.  <label for="firstname" class="col-sm-1 control-label">名字</label> 
  22.  <div class="col-sm-3"
  23.  <input type="text" class="form-control" name="name" 
  24.  placeholder="請輸入名字"
  25.  </div> 
  26.  </div> 
  27.  <div class="form-group"
  28.  <label for="lastname" class="col-sm-1 control-label">年齡</label> 
  29.  <div class="col-sm-3"
  30.  <input type="text" class="form-control" name="age" 
  31.  placeholder="請輸年齡"
  32.  </div> 
  33.  </div> 
  34.  <div class="form-group"
  35.  <label for="lastname" class="col-sm-1 control-label">性別</label> 
  36.  <div class="col-sm-3"
  37.  <input type="radio" name="sex" value="男" checked>男 
  38.  <input type="radio" name="sex" value="女">女 
  39.  </div> 
  40.  </div> 
  41.  <div class="form-group"
  42.  <label for="lastname" class="col-sm-1 control-label">愛好</label> 
  43.  <div class="col-sm-3"
  44.  <input type="checkbox" name="aihao" value="唱歌">唱歌 
  45.  <input type="checkbox" name="aihao" value="上網(wǎng)">上網(wǎng) 
  46.  <input type="checkbox" name="aihao" value="游戲">游戲 
  47.  <input type="checkbox" name="aihao" value="看書">看書 
  48.  </div> 
  49.  </div> 
  50.  <div class="form-group"
  51.  <div class="col-sm-offset-1 col-sm-3"
  52.  <button type="submit" class="btn btn-default">提交</button> 
  53.  <button type="reset" class="btn btn-default">重置</button> 
  54.  </div> 
  55.  </div> 
  56.  </form> 
  57. </body> 
  58. </html> 

使用getParameter方法和getParameterValues方法接收表單參數(shù):

  1. public class GetParameterRequest extends HttpServlet{ 
  2.  private static final long serialVersionUID = 3903946972744326948L; 
  3.  @Override 
  4.  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
  5.  this.doPost(req, resp); 
  6.  } 
  7.  @Override 
  8.  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
  9.  //客戶端是以UTF-8編碼提交表單數(shù)據(jù)的,所以需要設置服務器端以UTF-8的編碼進行接收,否則對于中文數(shù)據(jù)就會產(chǎn)生亂碼 
  10.  req.setCharacterEncoding("UTF-8"); 
  11.  //獲取名字 
  12.  String name = req.getParameter("name"); 
  13.  //獲取年齡 
  14.  String age = req.getParameter("age"); 
  15.  //獲取性別 
  16.  String sex = req.getParameter("sex"); 
  17.  //獲取愛好,因為可以選中多個值,所以獲取到的值是一個字符串數(shù)組,因此需要使用getParameterValues方法來獲取 
  18.  String[] aihaos = req.getParameterValues("aihao"); 
  19.  String aihao = ""
  20.  if(aihaos != null){ 
  21.  for (int i = 0; i < aihaos.length; i++) { 
  22.  if(i == aihaos.length - 1){ 
  23.  aihao += aihaos[i]; 
  24.  } else { 
  25.  aihao += aihaos[i] + ","
  26.  } 
  27.  } 
  28.  } 
  29.  System.out.println("名字:" + name); 
  30.  System.out.println("年齡:" + age); 
  31.  System.out.println("性別:" + sex); 
  32.  System.out.println("愛好:" + aihao); 
  33.  req.setAttribute("aihao", aihao); 
  34.  //設置服務器端以UTF-8編碼輸出數(shù)據(jù)到客戶端 
  35.  resp.setCharacterEncoding("UTF-8"); 
  36.  this.getServletContext().getRequestDispatcher("/request.jsp").forward(req, resp); 
  37.  } 

響應頁面:

  1. <%@ page language="java" contentType="text/html; charset=UTF-8" 
  2.  pageEncoding="UTF-8"%> 
  3. <% 
  4.  String path = request.getContextPath(); 
  5.  String basePath = request.getScheme() + "://" 
  6.  + request.getServerName() + ":" + request.getServerPort() 
  7.  + path + "/"
  8. %> 
  9. <html> 
  10. <head> 
  11. <base href="<%=basePath%>" /> 
  12. <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"
  13. <title>表單提交</title> 
  14. <link href="css/bootstrap.css" rel="stylesheet"
  15. <script src="js/jquery-3.2.1.js"></script> 
  16. <script src="js/bootstrap.js"></script> 
  17. </head> 
  18. <body> 
  19. <table class="table"
  20.  <thead> 
  21.  <tr> 
  22.  <th>名稱</th> 
  23.  <th>結(jié)果</th> 
  24.  </tr> 
  25.  </thead> 
  26.  <tbody> 
  27.  <tr> 
  28.  <td>姓名</td> 
  29.  <td><%=request.getParameter("name") %></td> 
  30.  </tr> 
  31.  <tr> 
  32.  <td>年齡</td> 
  33.  <td><%=request.getParameter("age") %></td> 
  34.  </tr> 
  35.  <tr> 
  36.  <td>性別</td> 
  37.  <td><%=request.getParameter("sex") %></td> 
  38.  </tr> 
  39.  <tr> 
  40.  <td>愛好</td> 
  41.  <td><%=request.getAttribute("aihao") %></td> 
  42.  </tr> 
  43.  </tbody> 
  44. </table
  45. </body> 
  46. </html> 

提交如下表單:

 

原來HTTP協(xié)議這么簡單!一文讀懂HttpServletRequest(周末快樂)

 

后臺打?。?/p>

 

原來HTTP協(xié)議這么簡單!一文讀懂HttpServletRequest(周末快樂)

 

運行結(jié)果如下:

 

原來HTTP協(xié)議這么簡單!一文讀懂HttpServletRequest(周末快樂)

 

四、request接收表單提交中文參數(shù)亂碼問題

1、以POST方式提交表單中文參數(shù)的亂碼問題

有如下表單:

  1. <%@ page language="java" contentType="text/html; charset=UTF-8" 
  2.  pageEncoding="UTF-8"%> 
  3. <% 
  4.  String path = request.getContextPath(); 
  5.  String basePath = request.getScheme() + "://" 
  6.  + request.getServerName() + ":" + request.getServerPort() 
  7.  + path + "/"
  8. %> 
  9. <html> 
  10. <head> 
  11. <base href="<%=basePath%>" /> 
  12. <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"
  13. <title>表單提交</title> 
  14. <link href="css/bootstrap.css" rel="stylesheet"
  15. <script src="js/jquery-3.2.1.js"></script> 
  16. <script src="js/bootstrap.js"></script> 
  17. </head> 
  18. <body> 
  19.  <form class="form-horizontal" action="<%=request.getContextPath()%>/PostRequest.html" role="form" method="post"
  20.  <div class="form-group"
  21.  <label for="firstname" class="col-sm-1 control-label">名字</label> 
  22.  <div class="col-sm-3"
  23.  <input type="text" class="form-control" name="name" 
  24.  placeholder="請輸入名字"
  25.  </div> 
  26.  </div> 
  27.  <div class="form-group"
  28.  <div class="col-sm-offset-1 col-sm-3"
  29.  <button type="submit" class="btn btn-default">提交</button> 
  30.  <button type="reset" class="btn btn-default">重置</button> 
  31.  </div> 
  32.  </div> 
  33.  </form> 
  34. </body> 
  35. </html> 

后臺接收參數(shù):

  1. public class PostRequest extends HttpServlet{ 
  2.  private static final long serialVersionUID = 3903946972744326948L; 
  3.  @Override 
  4.  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
  5.  this.doPost(req, resp); 
  6.  } 
  7.  @Override 
  8.  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
  9.  String name = req.getParameter("name"); 
  10.  System.out.println("名字:" + name); 
  11.  } 

提交數(shù)據(jù):

 

原來HTTP協(xié)議這么簡單!一文讀懂HttpServletRequest(周末快樂)

 

運行結(jié)果:

 

 

原來HTTP協(xié)議這么簡單!一文讀懂HttpServletRequest(周末快樂)

 

之所以會產(chǎn)生亂碼,就是因為服務器和客戶端溝通的編碼不一致造成的,因此解決的辦法是:在客戶端和服務器之間設置一個統(tǒng)一的編碼,之后就按照此編碼進行數(shù)據(jù)的傳輸和接收。

由于客戶端是以UTF-8字符編碼將表單數(shù)據(jù)傳輸?shù)椒掌鞫说模虼朔掌饕残枰O置以UTF-8字符編碼進行接收,通過setCharacterEncoding方法統(tǒng)一編碼格式:

  1. public class PostRequest extends HttpServlet{ 
  2.  private static final long serialVersionUID = 3903946972744326948L; 
  3.  @Override 
  4.  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
  5.  this.doPost(req, resp); 
  6.  } 
  7.  @Override 
  8.  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
  9.  //設置服務器以UTF-8的編碼接收數(shù)據(jù) 
  10.  req.setCharacterEncoding("UTF-8"); 
  11.  String name = req.getParameter("name"); 
  12.  System.out.println("名字:" + name); 
  13.  } 

重新提交表單,中文亂碼解決:

 

原來HTTP協(xié)議這么簡單!一文讀懂HttpServletRequest(周末快樂)

 

2、以GET方式提交表單中文參數(shù)的亂碼問題

有如下表單:

  1. <%@ page language="java" contentType="text/html; charset=UTF-8" 
  2.  pageEncoding="UTF-8"%> 
  3. <% 
  4.  String path = request.getContextPath(); 
  5.  String basePath = request.getScheme() + "://" 
  6.  + request.getServerName() + ":" + request.getServerPort() 
  7.  + path + "/"
  8. %> 
  9. <html> 
  10. <head> 
  11. <base href="<%=basePath%>" /> 
  12. <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"
  13. <title>表單提交</title> 
  14. <link href="css/bootstrap.css" rel="stylesheet"
  15. <script src="js/jquery-3.2.1.js"></script> 
  16. <script src="js/bootstrap.js"></script> 
  17. </head> 
  18. <body> 
  19.  <form class="form-horizontal" action="<%=request.getContextPath()%>/GetRequest.html" role="form" method="get"
  20.  <div class="form-group"
  21.  <label for="firstname" class="col-sm-1 control-label">名字</label> 
  22.  <div class="col-sm-3"
  23.  <input type="text" class="form-control" name="name" 
  24.  placeholder="請輸入名字"
  25.  </div> 
  26.  </div> 
  27.  <div class="form-group"
  28.  <div class="col-sm-offset-1 col-sm-3"
  29.  <button type="submit" class="btn btn-default">提交</button> 
  30.  <button type="reset" class="btn btn-default">重置</button> 
  31.  </div> 
  32.  </div> 
  33.  </form> 
  34. </body> 
  35. </html> 

后臺接收參數(shù):

  1. public class GetRequest extends HttpServlet{ 
  2.  private static final long serialVersionUID = 3903946972744326948L; 
  3.  @Override 
  4.  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
  5.  this.doPost(req, resp); 
  6.  } 
  7.  @Override 
  8.  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
  9.  String name = req.getParameter("name"); 
  10.  System.out.println("名字:" + name); 
  11.  } 

提交數(shù)據(jù):

 

原來HTTP協(xié)議這么簡單!一文讀懂HttpServletRequest(周末快樂)

 

運行結(jié)果:

 

原來HTTP協(xié)議這么簡單!一文讀懂HttpServletRequest(周末快樂)

 

之所以會產(chǎn)生亂碼,對于以get方式傳輸?shù)臄?shù)據(jù),默認的還是使用ISO8859-1這個字符編碼來接收數(shù)據(jù),客戶端以UTF-8的編碼傳輸數(shù)據(jù)到服務器端,而服務器端的request對象使用的是ISO8859-1這個字符編碼來接收數(shù)據(jù),服務器和客戶端溝通的編碼不一致因此才會產(chǎn)生中文亂碼的。

解決方法:

在接收到數(shù)據(jù)后,先獲取request對象以ISO8859-1字符編碼接收到的原始數(shù)據(jù)的字節(jié)數(shù)組,然后通過字節(jié)數(shù)組以指定的編碼構(gòu)建字符串

  1. public class GetRequest extends HttpServlet{ 
  2.  private static final long serialVersionUID = 3903946972744326948L; 
  3.  @Override 
  4.  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
  5.  this.doPost(req, resp); 
  6.  } 
  7.  @Override 
  8.  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
  9.  String name = req.getParameter("name"); 
  10.  //以ISO8859-1字符編碼接收到的原始數(shù)據(jù)的字節(jié)數(shù)組,然后通過字節(jié)數(shù)組以指定的編碼構(gòu)建字符串 
  11.  name = new String(name.getBytes("ISO8859-1") , "UTF-8"); 
  12.  System.out.println("名字:" + name); 
  13.  } 

 

責任編輯:武曉燕 來源: 今日頭條
相關推薦

2019-05-27 14:03:48

開發(fā)技能代碼

2017-05-04 20:29:12

HTTP服務器TCP

2020-03-08 21:22:03

HTTP112

2023-01-09 08:14:08

GoHttpServer

2020-03-14 13:13:02

物聯(lián)網(wǎng)IOT通信協(xié)議

2020-11-27 10:34:01

HTTPHTTPS模型

2021-05-07 09:17:21

HTTPTCP協(xié)議

2023-12-22 19:59:15

2021-08-04 16:06:45

DataOps智領云

2022-07-07 18:03:15

網(wǎng)絡協(xié)議網(wǎng)絡通信

2020-04-20 10:47:57

Redis數(shù)據(jù)開發(fā)

2025-03-18 09:10:00

MCPAI模型上下文協(xié)議

2022-05-11 11:54:55

Http傳送協(xié)議

2018-09-28 14:06:25

前端緩存后端

2022-09-22 09:00:46

CSS單位

2025-04-03 10:56:47

2022-11-06 21:14:02

數(shù)據(jù)驅(qū)動架構(gòu)數(shù)據(jù)

2022-07-05 06:30:54

云網(wǎng)絡網(wǎng)絡云原生

2023-05-20 17:58:31

低代碼軟件

2023-11-27 17:35:48

ComponentWeb外層
點贊
收藏

51CTO技術棧公眾號