为什么自定义的Servlet类中,只要继承了HttpServlet就只需要重写doGet和doPost即可

在这里插入图片描述

Serlvet接口只定义了一个服务方法就是service,而HttpServlet类实现了该方法并且要求调用下列的方法之一:
doGet:处理GET请求
doPost:处理POST请求
当发出客户端请求的时候,调用service 方法并传递一个请求和响应对象。Servlet首先判断该请求是GET 操作还是POST 操作。然后它调用下面的一个方法:doGet 或 doPost。如果请求是GET就调用doGet方法,如果请求是POST就调用doPost方法。doGet和doPost都接受请求(HttpServletRequest)和响应(HttpServletResponse)。

doget 是接收网页用get方法时调用的 ==
dopost 是用来
接收post方法的 ==
get方法就象你在网页的地址栏里看到的一堆乱码,也就是url后面有参数
post就是用表单传过去的,就好象把数据都打成包发过去一样
如果不知道用的什么方法,你可以在servlet里把功能都写到一起
然后在一个方法里调用另一个方法就可以了
比如

public void doPost(HttpServletRequest request,HttpServletResponse) 
throws ServletException,IOException 
{ 
doGet(request,response); 
}  

doGet()和doPost()方法源码解析

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
2.	        //获取协议 
3.	        String protocol = req.getProtocol();
4.	        //获取http.method_get_not_supported的国际化字符串
5.	        String msg = lStrings.getString("http.method_get_not_supported");
6.	        if(protocol.endsWith("1.1")) {
7.	        //如果是HTTP/1.1,返回405禁止访问方法错误
8.	            resp.sendError(405, msg);
9.	        } else {
10.	        //如果不是HTTP/1.1,返回400错误的请求错误  
11.	            resp.sendError(400, msg);
12.	        }
13.	 
14.	    }
15.	 
16.	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
17.	        String protocol = req.getProtocol();
18.	        String msg = lStrings.getString("http.method_post_not_supported");
19.	        if(protocol.endsWith("1.1")) {
20.	            resp.sendError(405, msg);
21.	        } else {
22.	            resp.sendError(400, msg);
23.	        }
24.	 
25.	}
 
 
 
 
 

两个方法如果不被重写,那执行时默认会调用HttpServlet的代码,首先获取协议,然后获取国际化字符串,最后判断协议,如果协议为HTTP/1.1,返回405禁止访问方法错误,//如果不是HTTP/1.1,返回400错误的请求错误。

Servlet -doGet() doPost()原理
一、自定义类只需要重写doGet(HttpServletRequest request, HttpServletResponse response) 和doPost(HttpServletRequest request, HttpServletResponse, response)的原因:
1、在HttpServlet接口中重写了父类的service(ServletRequest request, ServletResponse response),在这里面主要完成了将父类的request和response对象转化成专门用于处理HTTP请求响应的HttpServletRequest和HttpServletResponse对象,然后再调用一个重载的service方法,该方法接收HttpServletRequest和HttpServletResponse对象:

 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);
    }
}

2、在重载的service方法中,调用了doGet(HttpServletRequest request, HttpServletResponse)和doPost(HttpServletRequest request, HttpServletResponse),用于处理Get和Post请求和响应:

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 = req.getDateHeader(HEADER_IFMODSINCE);
        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);
        }
        }

    }
 
 
 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException
    {
    String protocol = req.getProtocol();
    String msg = lStrings.getString("http.method_get_not_supported");
    if (protocol.endsWith("1.1")) {
        resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg);
    } else {
        resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);
    }
    }
 

由上可知,Servlet引擎在处理用户请求时所调用的service(ServletRequest req, ServletResponse rep)最终会执行doGet和doPost方法中的代码完成。这就是在自定义的Servlet类中,只要继承了HttpServlet就只需要重写doGet和doPost即可。

二、Servlet的生命周期:
1、加载和实例化Servlet对象:由Servlet窗口负责加载Servlet,当Servlet窗口启动或是检测到需要这个Servlet响应第一个请求时,将加载这个Servlet。加载的方式是通过反射的API去调用Servelet的无参构造方法完成,所以在自定义的Servlet中不应该出现带参数的构造方法,如果需要定义有参的构造方法,则一定要把无参的也写出来。
2、初始化Servlet对象:调用init()方法完成相应的初始化,如读取初始化参数、连接DB等。
3、处理请求:调用service()方法处理请求。之前一这是init()方法执行成功。
4、销毁Servlet对象:调用detroy()方法。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值