首页 > 试题广场 >

J2EE中,当把来自客户机的HTTP请求委托给servlet

[单选题]

J2EE中,当把来自客户机的HTTP请求委托给servlet时,会调用HttpServlet的( )方法

  • service
  • doget
  • dopost
  • init
A

HttpServlet容器响应Web客户请求流程如下:

1)Web客户向Servlet容器发出Http请求;

2)Servlet容器解析Web客户的Http请求;

3)Servlet容器创建一个HttpRequest对象,在这个对象中封装Http请求信息;

4)Servlet容器创建一个HttpResponse对象;

5)Servlet容器调用HttpServlet的service方法,这个方法中会根据request的Method来判断具体是执行doGet还是doPost,把HttpRequest和HttpResponse对象作为service方法的参数传给HttpServlet对象;

6)HttpServlet调用HttpRequest的有关方法,获取HTTP请求信息;

7)HttpServlet调用HttpResponse的有关方法,生成响应数据;

8)Servlet容器把HttpServlet的响应结果传给Web客户。

doGet() doPost() 是创建HttpServlet时需要覆盖的方法.

编辑于 2015-03-04 09:44:12 回复(40)
根据HTTP请求行的方法有一个选择的过程:
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);
        }
        }

    } 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);
    }
    }
发表于 2017-07-22 17:13:45 回复(0)
D   

Servlet生命周期分为三个阶段:

1.初始化阶段  调用init()方法

2.响应客户请求阶段  调用service()方法

3.终止阶段  调用destroy()方法

发表于 2015-03-19 23:25:20 回复(3)
A.首先Servlet通过HttpRequest对象封装http请求信息,然后Servlet容器调用HttpServlet的service方法,它会根据用户请求的方式调用具体的方法。如果请求方式是Get则调用doGet方法,如果请求方式是POST则调用doPost方法,执行完后,通过HttpRespones对象生成相应数据相应客户的请求,一般要重写doGet方法和doPost方法
发表于 2015-08-10 10:50:40 回复(0)
要修改数据用doPost,获取数据就用doGet
发表于 2014-10-29 15:15:18 回复(2)
答案:BC
HttpServlet将Servlet接口的service方法细化为doGet和doPost方法,客户端请求到达服务器后,tomcat调用Servlet的service方法,HttpServlet对service方法进行封装,判断请求是get请求还是post请求,get请求调用doGet方法,post请求调用doPost方法。
发表于 2015-01-29 12:33:56 回复(1)
HttpServlet容器响应Web客户请求流程如下: 1)Web客户向Servlet容器发出Http请求; 2)Servlet容器解析Web客户的Http请求; 3)Servlet容器创建一个HttpRequest对象,在这个对象中封装Http请求信息; 4)Servlet容器创建一个HttpResponse对象; 5)Servlet容器调用HttpServlet的service方法,这个方法中会根据request的Method来判断具体是执行doGet还是doPost,把HttpRequest和HttpResponse对象作为service方法的参数传给HttpServlet对象; 6)HttpServlet调用HttpRequest的有关方法,获取HTTP请求信息; 7)HttpServlet调用HttpResponse的有关方法,生成响应数据; 8)Servlet容器把HttpServlet的响应结果传给Web客户。 doGet() 或 doPost() 是创建HttpServlet时需要覆盖的方法. Servlet生命周期分为三个阶段: 1.初始化阶段  调用init()方法 2.响应客户请求阶段 调用service()方法 3.终止阶段 调用destroy()方法
编辑于 2021-10-15 18:08:10 回复(0)
init()是在委托之前 ,传递请求响应对象的过程是在sevice中完成的。
发表于 2017-09-29 10:00:30 回复(0)
HttpServlet容器响应Web客户请求流程如下:

1)Web客户向Servlet容器发出Http请求;

2)Servlet容器解析Web客户的Http请求;

3)Servlet容器创建一个HttpRequest对象,在这个对象中封装Http请求信息;

4)Servlet容器创建一个HttpResponse对象;

5)Servlet容器调用HttpServlet的service方法,这个方法中会根据request的Method来判断具体是执行doGet还是doPost,把HttpRequest和HttpResponse对象作为service方法的参数传给HttpServlet对象;

6)HttpServlet调用HttpRequest的有关方法,获取HTTP请求信息;

7)HttpServlet调用HttpResponse的有关方法,生成响应数据;

8)Servlet容器把HttpServlet的响应结果传给Web客户。

doGet()  doPost() 是创建HttpServlet时需要覆盖的方法.
发表于 2016-08-17 15:41:41 回复(1)
客户机的HTTP请求 对应 Servlet 的响应客户请求阶段(即客户机请求)所以调用service()方法
发表于 2017-09-07 19:31:12 回复(0)
init()在委托前执行,仅执行一次。
发表于 2023-06-07 11:35:14 回复(0)
已经到委托阶段了,必然是service
发表于 2020-09-04 08:18:18 回复(0)
A HttpServlet容器响应Web客户请求流程如下: 1)Web客户向Servlet容器发出Http请求; 2)Servlet容器解析Web客户的Http请求; 3)Servlet容器创建一个HttpRequest对象,在这个对象中封装Http请求信息; 4)Servlet容器创建一个HttpResponse对象; 5)Servlet容器调用HttpServlet的service方法,这个方法中会根据request的Method来判断具体是执行doGet还是doPost,把HttpRequest和HttpResponse对象作为service方法的参数传给HttpServlet对象; 6)HttpServlet调用HttpRequest的有关方法,获取HTTP请求信息; 7)HttpServlet调用HttpResponse的有关方法,生成响应数据; 8)Servlet容器把HttpServlet的响应结果传给Web客户。 doGet() 或 doPost() 是创建HttpServlet时需要覆盖的方法.
发表于 2017-05-22 17:28:21 回复(0)
首先Servlet通过HttpRequest对象封装http请求信息,然后Servlet容器调用HttpServlet的service方法,它会根据用户请求的方式调用具体的方法。如果请求方式是Get则调用doGet方法,如果请求方式是POST则调用doPost方法,执行完后,通过HttpRespones对象生成相应数据相应客户的请求,一般要重写doGet方法和doPost方法 ...
编辑于 2024-03-15 09:50:55 回复(0)
当来自客户端的HTTP请求委托给servlet时,会调用HttpServlet中的service()方法。该方法负责处理客户端的请求,并根据请求类型(GET,POST),调用doGet(),doPost()方法。
发表于 2023-08-04 20:51:54 回复(0)
卡的题目都看不见
发表于 2023-02-07 21:37:00 回复(0)
我开始记得是servcie方方法的,但是我又读了一遍这一意思是初始化之前的事情嘛,就选择了int 唉 被字面意思骗了
发表于 2022-12-14 08:51:54 回复(0)
HttpServlet容器响应Web客户请求流程如下:

1)Web客户向Servlet容器发出Http请求;

2)Servlet容器解析Web客户的Http请求;

3)Servlet容器创建一个HttpRequest对象,在这个对象中封装Http请求信息;

4)Servlet容器创建一个HttpResponse对象;

5)Servlet容器调用HttpServlet的service方法,这个方法中会根据request的Method来判断具体是执行doGet还是doPost,把HttpRequest和HttpResponse对象作为service方法的参数传给HttpServlet对象;

6)HttpServlet调用HttpRequest的有关方法,获取HTTP请求信息;

7)HttpServlet调用HttpResponse的有关方法,生成响应数据;

8)Servlet容器把HttpServlet的响应结果传给Web客户。

doGet()  doPost() 是创建HttpServlet时需要覆盖的方法.
发表于 2022-08-05 10:59:54 回复(0)

init()

service()

destroy()

发表于 2022-05-18 13:59:34 回复(0)
请求会先进入service方法,由service方法去调用doGet或doPost,不过这个题很古老了啊,牛客有没有新一点的题啊,分布式中间件和微服务框架啥的
发表于 2021-03-30 10:27:47 回复(0)