Servlet--HttpServlet实现doGet和doPost请求的原理
本文转载自两个文章,主要介绍了Servlet和HttpServlet中的service()函数,他们的区别和不能覆盖的原因。以及当访问一个地址时,doGet()和doPost()函数都是如何工作的。其中还有一个实例以及两个service的源码。
https://blog.csdn.net/truong/article/details/17038687
https://blog.csdn.net/m0_38039437/article/details/75264012
一、HttpServlet简介
1、HttpServlet是GenericServlet的子类,又是在GenericServlet的基础上做了增强。
2、HttpServlet方法
二、HTTP实现doGet或doPost请求项目介绍
1、通过实现doGet请求和doPost请求实例来了解内部的工作原理。
2、doGet请求和doPost请求实例代码介绍:
A:创建一个Servlet类继承HttpServlet类
B:在index.jsp页面创建一个超链接请求
3、doGet请求和doPost请求实例实施介绍:
A、创建一个webproject项目。
B、创建一个Servlet类的名称为HttpServ继承HttpServlet类同时覆写doGet方法和doPost方法。
1、
2、
3、配置web.xml文件
4、创建Servlet代码展示
- package httpserve;
- import java.io.IOException;
- import java.io.PrintWriter;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- public class HttpServ extends HttpServlet {
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- System.out.println("发送get请求。。。。。。。。。。。");
- }
- protected void doPost(HttpServletRequest req, HttpServletResponse resp)
- throws ServletException, IOException {
- // TODO Auto-generated method stub
- resp.setContentType("text/html;charset=UTF-8");
- System.out.println("发送post方法。。。。。。。。。。");
- }
- }
5、web.xml配置文件代码展示
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="3.0"
- xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
- <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>HttpServ</servlet-name>
- <servlet-class>httpserve.HttpServ</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>HttpServ</servlet-name>
- <url-pattern>/http</url-pattern>
- </servlet-mapping>
- </web-app>
C、在index.jsp页面创建一个超链接请求
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>My JSP 'index.jsp' starting page</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <!--
- <link rel="stylesheet" type="text/css" href="styles.css">
- -->
- </head>
- <body>
- This is my JSP page. <br>
- <a href="http://localhost:8080/test06/http">get请求1</a><br/>
- <!-- 对于一个html页面来说,如果没有以http开始,则默认的前面会加上
- 协议类型://目前这个页面所在的服务器:目前端口/目前项目/你给的这个名称 -->
- <a href="http">get请求2</a><hr/>
- <form method = "post" action="http">
- <input type="submit" value="提交"/>
- </form>
- </body>
- </html>
D、发送doGet请求和doPost请求
a、在浏览器中输入测试地址,http://127.0.0.1:8080/test06/
b、打开项目的首页后分别点击get请求1、get请求2、和 post请求(提交按钮)
c、在控制台可以观察到分别调用了doGet和doPost方法。
三、HTTP实现doGet或doPost请求原理介绍
1、浏览器发送请求到HttpServ类调用HttpServ的service(servletRequest, servletReponse)方法
2、由于没有找到这个方法,去调用父类(HttpServlet) 的同名方法。
3、父类的service方法将ServletRequest req请求转换成HttpServletRequest请求,再去调用service(request, response) 方法。
该方法在HttpSevlet的父类中。
将ServletRequest req请求转换成HttpServletRequest请求再调用service(request, response) 方法源码如下:
- public void service(ServletRequest req, ServletResponse res)
- throws ServletException, IOException {
- HttpServletRequest request;
- HttpServletResponse response;
- try {
- request = (HttpServletRequest) req;
- response = (HttpServletResponse) res;
- } catch (ClassCastException e) {
- throw new ServletException("non-HTTP request or response");
- }
- service(request, response);
- }
4、调用的service(request, response) 方法功能是判断用户发出是什么请求,如果是get则调用子类(HttpSevr)的doGet方法,如果是post则调用子类(HttpSevr)的doPost方法。该方法是在HttpServlet中。
service(request, response) 方法源码如下:
- protected void service(HttpServletRequest req, HttpServletResponse resp)
- throws ServletException, IOException {
- String method = req.getMethod();
- if (method.equals(METHOD_GET)) {
- long lastModified = getLastModified(req);
- if (lastModified == -1) {
- // servlet doesn't support if-modified-since, no reason
- // to go through further expensive logic
- doGet(req, resp);
- } else {
- long ifModifiedSince;
- try {
- ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE);
- } catch (IllegalArgumentException iae) {
- // Invalid date header - proceed as if none was set
- ifModifiedSince = -1;
- }
- if (ifModifiedSince < (lastModified / 1000 * 1000)) {
- // If the servlet mod time is later, call doGet()
- // Round down to the nearest second for a proper compare
- // A ifModifiedSince of -1 will always be less
- maybeSetLastModified(resp, lastModified);
- doGet(req, resp);
- } else {
- resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
- }
- }
- } else if (method.equals(METHOD_HEAD)) {
- long lastModified = getLastModified(req);
- maybeSetLastModified(resp, lastModified);
- doHead(req, resp);
- } else if (method.equals(METHOD_POST)) {
- doPost(req, resp);
- } else if (method.equals(METHOD_PUT)) {
- doPut(req, resp);
- } else if (method.equals(METHOD_DELETE)) {
- doDelete(req, resp);
- } else if (method.equals(METHOD_OPTIONS)) {
- doOptions(req,resp);
- } else if (method.equals(METHOD_TRACE)) {
- doTrace(req,resp);
- } else {
- //
- // Note that this means NO servlet supports whatever
- // method was requested, anywhere on this server.
- //
- String errMsg = lStrings.getString("http.method_not_implemented");
- Object[] errArgs = new Object[1];
- errArgs[0] = method;
- errMsg = MessageFormat.format(errMsg, errArgs);
- resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, errMsg);
- }
- }
解释:如果自己写的servlet中覆盖了service函数,就无法实现doGet()等函数的原因?
- 从上面可以看出 这里的service是用来转向的,但是如果你在自己的servlet类中覆盖了service方法,比如说你的service是这样的:
- Java代码
- 1.publicvoid service(ServletRequest req, ServletResponse res)
- 2. throws ServletException, IOException {
- 3. res.getOutputStream().print(
- 4. "image is <img src='images/downcoin.gif'></img><br>");
- 5. }
-
- 那么这时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.publicclass Testextends HttpServlet {
- 31.
- 32.publicvoid service(ServletRequest req, ServletResponse res)
- 33. throws ServletException, IOException {
- 34. res.getOutputStream().print("This is the service");
- 35.
- 36. }
- 37.
- 38.protectedvoid doGet(HttpServletRequest request,
- 39. HttpServletResponse response)throws ServletException, IOException {
- 40. doPost(request,response);
- 41.
- 42. }
- 43.protectedvoid 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. }
- 点击提交后:页面输出结果为“This is the service“;
- 所以,我们在写servlet的时候,一般都是重写doGet或doPost方法,不会管service方法。
5、调用关系图
四、注意事项:
1、如果在HttpServ中覆盖了service(ServletRequest,SerlvetResonse)方法则这个类的所实现的doGet/doPost都不会再执了。
因为service(ServletRequest,SerlvetResonse)是最高接口Servlet定义规范。
在tomcat调用时,一定会在最终的子类中去找这个方法且调用它。
如果最终的子类没有则会调用父的service(ServletRequest,SerlvetResonse)。
具体原因上面提到了。
2.、如果继承了HttpServlet没有实现任何的doXxx方法则会抛出一个异常