欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java web學習_淺談request對象中g(shù)et和post的差異

 更新時間:2017年05月27日 09:29:29   投稿:jingxian  
下面小編就為大家?guī)硪黄猨ava web學習_淺談request對象中g(shù)et和post的差異。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

閱讀目錄(Content)

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

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

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

•2.運行結(jié)果

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

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

•3.post方法

•4.對比

•4.1 在輸出頁面按下F12查看

•5.分析

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

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

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

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

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

protected void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  response.setContentType("text/html;charset=gbk");//設(shè)置響應正文的mime類型
  request.setCharacterEncoding("gbk");//設(shè)置請求的編碼格式
  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("此人沒愛好!"); 
  }
 }

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

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

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

   

3.post方法

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

servlet中有這樣的一行代碼:

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

4.對比   4.1 在輸出頁面按下F12查看

 

   

   

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

    post的缺點:

    

  更新web版本可以避免錯誤

servlet代碼分析

5.分析

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

以上這篇java web學習_淺談request對象中g(shù)et和post的差異就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論