日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Service 和 doGet 和 doPost 方法的区别

發布時間:2025/4/16 编程问答 11 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Service 和 doGet 和 doPost 方法的区别 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Service 和 doGet 和 doPost 方法的區別

新建一個JSP文件:


get方式


post方式?


service方法、doGet方法和doPost方法的區別:



在上面的步驟中,把下圖中的method方法給改了?


改變上圖,改成下圖的樣子。重復上述操作:

將會得到以下結果:

service方法、doGet方法和doPost方法的區別


?* service方法:可以處理get/post方式的請求,如果servlet中有service方法,會優先調用service方法
?* doGet方法: 處理get方法的請求
?* doPost方法: 處理post方式的請求
?* 注意:如果在覆寫的service方法中調用了父類的service方法 ?super.service(arg0, arg1);
?*? ? ? ? ? 則service方法處理完后,會再次根據請求方式響應的doGet和doPost方法執行
?*? ? ? ? ?所以,一般情況下,是不在覆寫的service中調用父類的service方法的,避免出現405錯誤

package com.cl.servlet;import java.io.IOException;import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;/*** service方法、doGet方法和doPost方法的區別* service方法:可以處理get/post方式的請求,如果servlet中有service方法,會優先調用service方法* doGet方法: 處理get方法的請求* doPost方法: 處理post方式的請求* 注意:如果在覆寫的service方法中調用了父類的service方法 super.service(arg0, arg1);* 則service方法處理完后,會再次根據請求方式響應的doGet和doPost方法執行* 所以,一般情況下,是不在覆寫的service中調用父類的service方法的,避免出現405錯誤* @author Administrator**/ public class ServletMethod extends HttpServlet {@Overrideprotected void service(HttpServletRequest arg0, HttpServletResponse arg1)throws ServletException, IOException {System.out.println("我是service");super.service(arg0, arg1);}@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {System.out.println("我是doGet方法");}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {System.out.println("我是doPost方法");} }

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"><display-name>02-MyServlet</display-name><servlet><description>This is the description of my J2EE component</description><display-name>This is the display name of my J2EE component</display-name><servlet-name>servletLife</servlet-name><servlet-class>com.cl.servlet.servletLife</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet><description>This is the description of my J2EE component</description><display-name>This is the display name of my J2EE component</display-name><servlet-name>ServletMethod</servlet-name><servlet-class>com.cl.servlet.ServletMethod</servlet-class></servlet><servlet-mapping><servlet-name>servletLife</servlet-name><url-pattern>/life</url-pattern></servlet-mapping><servlet-mapping><servlet-name>ServletMethod</servlet-name><url-pattern>/method</url-pattern></servlet-mapping><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list> </web-app>

?

總結

以上是生活随笔為你收集整理的Service 和 doGet 和 doPost 方法的区别的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。