您好,登錄后才能下訂單哦!
閱讀目錄(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表單代碼
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的名字相同:
2.運(yùn)行結(jié)果 2.1 輸入數(shù)據(jù)
2.2 打印出數(shù)據(jù)
3.post方法
只需要將table表單中method改為post:
servlet中有這樣的一行代碼:
同樣能打印出:只是出現(xiàn)了亂碼
4.對(duì)比 4.1 在輸出頁(yè)面按下F12查看
post跟get的差異,優(yōu)先選post
post的缺點(diǎn):
更新web版本可以避免錯(cuò)誤
servlet代碼分析
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è)參考,也希望大家多多支持億速云。
免責(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)容。