溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

java web學(xué)習(xí)_淺談request對(duì)象中g(shù)et和post的差異

發(fā)布時(shí)間:2020-09-26 12:29:20 來(lái)源:腳本之家 閱讀:179 作者:jingxian 欄目:編程語(yǔ)言

閱讀目錄(Content)

•1.get與post的區(qū)別

•1.1 get方法 jsp中的代碼form表單代碼

•1.2 action包中servlet的doGet方法中的代碼

•2.運(yùn)行結(jié)果

•2.1 輸入數(shù)據(jù)

•2.2 打印出數(shù)據(jù)

•3.post方法

•4.對(duì)比

•4.1 在輸出頁(yè)面按下F12查看

•5.分析

1.get與post的區(qū)別

Get和Post方法都是對(duì)服務(wù)器的請(qǐng)求方式,只是他們傳輸表單的方式不一樣。

下面我們就以傳輸一個(gè)表單的數(shù)據(jù)為例,來(lái)分析get與Post的區(qū)別

1.1 get方法  jsp中的代碼form表單代碼

java web學(xué)習(xí)_淺談request對(duì)象中g(shù)et和post的差異

1.2 action包中servlet的doGet方法中的代碼

protected void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  response.setContentType("text/html;charset=gbk");//設(shè)置響應(yīng)正文的mime類型
  request.setCharacterEncoding("gbk");//設(shè)置請(qǐng)求的編碼格式
  response.setCharacterEncoding("gbk"); 

  String username = request.getParameter("userName");//
  String password = request.getParameter("password");
  String sex = request.getParameter("sex");
  String classes = request.getParameter("class");
  String hobby[] = request.getParameterValues("hobby");// 獲取checkbox的數(shù)據(jù)保存到hobby數(shù)組中

  PrintWriter out = response.getWriter();
  
  if (hobby != null) {
   for (String x: hobby) {
    out.println("doGet被調(diào)用");
    out.println("name:"+username+"password:"+password+"sex"+sex+"classes"+classes);
    out.println("hobby:" + x);  
   }
  }else{
   out.println("此人沒(méi)愛(ài)好!"); 
  }
 }

注意:action包中servlet命名與form表單action的名字相同:

java web學(xué)習(xí)_淺談request對(duì)象中g(shù)et和post的差異

2.運(yùn)行結(jié)果  2.1 輸入數(shù)據(jù)

java web學(xué)習(xí)_淺談request對(duì)象中g(shù)et和post的差異

  2.2 打印出數(shù)據(jù)

   java web學(xué)習(xí)_淺談request對(duì)象中g(shù)et和post的差異

3.post方法

   只需要將table表單中method改為post:

java web學(xué)習(xí)_淺談request對(duì)象中g(shù)et和post的差異

servlet中有這樣的一行代碼:

java web學(xué)習(xí)_淺談request對(duì)象中g(shù)et和post的差異

同樣能打印出:只是出現(xiàn)了亂碼

java web學(xué)習(xí)_淺談request對(duì)象中g(shù)et和post的差異

4.對(duì)比   4.1 在輸出頁(yè)面按下F12查看

java web學(xué)習(xí)_淺談request對(duì)象中g(shù)et和post的差異

 

   

   java web學(xué)習(xí)_淺談request對(duì)象中g(shù)et和post的差異

   post跟get的差異,優(yōu)先選post

    post的缺點(diǎn):

    java web學(xué)習(xí)_淺談request對(duì)象中g(shù)et和post的差異

  更新web版本可以避免錯(cuò)誤

servlet代碼分析

java web學(xué)習(xí)_淺談request對(duì)象中g(shù)et和post的差異

5.分析

servlet作為控制器是不應(yīng)該輸出內(nèi)容的,我們應(yīng)該把要打印的內(nèi)容放到j(luò)sp文件中

以上這篇java web學(xué)習(xí)_淺談request對(duì)象中g(shù)et和post的差異就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI