欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

servlet中service doGet doPost 的联系和区别

程序员文章站 2022-03-03 12:39:30
...

在servlet中默认情况下,无论你是get还是post 提交过来 都会经过service()方法来处理,然后转向到doGet

或是doPost方法,可以看HttpServlet 类的service方法:
Java代码

1. protected void service(HttpServletRequest req, HttpServletResponse resp)
2. throws ServletException, IOException
3. {
4. String method = req.getMethod();
5. if(method.equals("GET"))
6. {
7. long lastModified = getLastModified(req);
8. if(lastModified == -1L)
9. {
10. doGet(req, resp);
11. } else
12. {
13. long ifModifiedSince = req.getDateHeader("If-Modified-Since");
14. if(ifModifiedSince < (lastModified / 1000L) * 1000L)
15. {
16. maybeSetLastModified(resp, lastModified);
17. doGet(req, resp);
18. } else
19. {
20. resp.setStatus(304);
21. }
22. }
23. } else
24. if(method.equals("HEAD"))
25. {
26. long lastModified = getLastModified(req);
27. maybeSetLastModified(resp, lastModified);
28. doHead(req, resp);
29. } else
30. if(method.equals("POST"))
31. doPost(req, resp);
32. else
33. if(method.equals("PUT"))
34. doPut(req, resp);
35. else
36. if(method.equals("DELETE"))
37. doDelete(req, resp);
38. else
39. if(method.equals("OPTIONS"))
40. doOptions(req, resp);
41. else
42. if(method.equals("TRACE"))
43. {
44. doTrace(req, resp);
45. } else
46. {
47. String errMsg = lStrings.getString("http.method_not_implemented");
48. Object errArgs[] = new Object[1];
49. errArgs[0] = method;
50. errMsg = MessageFormat.format(errMsg, errArgs);
51. resp.sendError(501, errMsg);
52. }
53. }

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是这样的:

Java代码

1. public void service(ServletRequest req, ServletResponse res)
2. throws ServletException, IOException {
3. res.getOutputStream().print(
4. "image is <img src='images/downcoin.gif'></img><br>");
5. }

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方法。

比如说:你的客户端代码是:
Java代码

1. <%@page contentType="text/html; charset=utf-8"%>
2. <html>
3. <head><title>选择</title></head>
4. <body>
5. 请选择你喜欢的水果:<br>
6. <form action = "Test" method = "post">
7. <input type="checkbox" name="fruit" value ="apple" >苹果<br>
8. <input type="checkbox" name="fruit" value ="orange">桔子<br>
9. <input type="checkbox" name="fruit" value ="mango">芒果<br>
10. <input type="submit" value="提交">
11. </form>
12. </body>
13. </html>
14.
15. 服务端servlet是:Test类
16.
17. import java.io.IOException;
18.
19. import javax.servlet.ServletException;
20. import javax.servlet.ServletOutputStream;
21. import javax.servlet.ServletRequest;
22. import javax.servlet.ServletResponse;
23. import javax.servlet.http.HttpServlet;
24. import javax.servlet.http.HttpServletRequest;
25. import javax.servlet.http.HttpServletResponse;
26.
27. /**
28. * 演示service方法
29. */
30. public class Test extends HttpServlet {
31.
32. public void service(ServletRequest req, ServletResponse res)
33. throws ServletException, IOException {
34. res.getOutputStream().print("This is the service");
35.
36. }
37.
38. protected void doGet(HttpServletRequest request,
39. HttpServletResponse response) throws ServletException, IOException {
40. doPost(request,response);
41.
42. }
43. protected void doPost(HttpServletRequest request,
44. HttpServletResponse response) throws ServletException, IOException {
45. ServletOutputStream out=response.getOutputStream();
46. String[] args=(String[])request.getParameterValues("fruit");
47. for(int i=0;i<args.length;i++){
48. out.print(args[i]+"<br>");
49. }
50.
51. }
52. }


相关标签: Servlet