servlet中service doGet doPost 的关系
程序员文章站
2022-07-15 10:03:22
...
在servlet中默认情况下,无论你是get还是post 提交过来 都会经过service()方法来处理,然后转向到doGet
或是doPost方法,可以看HttpServlet 类的service方法:
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String method = req.getMethod(); if(method.equals("GET")) { long lastModified = getLastModified(req); if(lastModified == -1L) { doGet(req, resp); } else { long ifModifiedSince = req.getDateHeader("If-Modified-Since"); if(ifModifiedSince < (lastModified / 1000L) * 1000L) { maybeSetLastModified(resp, lastModified); doGet(req, resp); } else { resp.setStatus(304); } } } else if(method.equals("HEAD")) { long lastModified = getLastModified(req); maybeSetLastModified(resp, lastModified); doHead(req, resp); } else if(method.equals("POST")) doPost(req, resp); else if(method.equals("PUT")) doPut(req, resp); else if(method.equals("DELETE")) doDelete(req, resp); else if(method.equals("OPTIONS")) doOptions(req, resp); else if(method.equals("TRACE")) { doTrace(req, resp); } else { String errMsg = lStrings.getString("http.method_not_implemented"); Object errArgs[] = new Object[1]; errArgs[0] = method; errMsg = MessageFormat.format(errMsg, errArgs); resp.sendError(501, errMsg); } }
从上面可以看出 这里的service是用来转向的,但是如果你在自己的servlet类中覆盖了service方法,比如说你的service是这样的:
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { res.getOutputStream().print( "image is <img src='images/downcoin.gif'></img><br>"); }
那么这时service就不是用来转向的,而是用来处理业务的,现在不论你的客户端是用pos还是get来请求此servlet
都会执行service方法也只能执行servlet方法,不会去执行doPost或是doGet方法。
比如说:你的客户端代码是:
<%@page contentType="text/html; charset=utf-8"%> <html> <head><title>选择</title></head> <body> 请选择你喜欢的水果:<br> <form action = "Test" method = "post"> <input type="checkbox" name="fruit" value ="apple" >苹果<br> <input type="checkbox" name="fruit" value ="orange">桔子<br> <input type="checkbox" name="fruit" value ="mango">芒果<br> <input type="submit" value="提交"> </form> </body> </html> 服务端servlet是:Test类 import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * 演示service方法 */ public class Test extends HttpServlet { public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { res.getOutputStream().print("This is the service"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletOutputStream out=response.getOutputStream(); String[] args=(String[])request.getParameterValues("fruit"); for(int i=0;i<args.length;i++){ out.print(args[i]+"<br>"); } } }
推荐阅读
-
在servlet中如何使用被Spring管理的service
-
在servlet中如何使用被Spring管理的service
-
servlet中service doGet doPost 的关系
-
读源码学Servlet(5)GenericServlet中的service 方法
-
servlet中service doGet doPost 的关系
-
servlet中的service, doGet, doPost方法的区别和联系
-
servlet中service doGet doPost 的关系
-
Servlet中doGet与doPost的区别
-
Servlet Service doPost doGet
-
service、doGet和doPost的区别