Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is 'service' method in HttpServlet class?

Tags:

java

servlets

Below is a simple servlet written for learning.

package com.example.tutorial;

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 ServletExample extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        out.println("Hello Java!");
    }

}

When a browser hits this URI: http://localhost:8081/ServletsJSPExample/servletexample,

By analyzing the request header of http packet, it shows GET request sent from the browser. But, In my servlet, I do not have GET request to process.

So,

When does service method gets invoked?

Why does service method receives this GET request?

like image 339
overexchange Avatar asked May 30 '15 19:05

overexchange


People also ask

How many of service method is present in HttpServlet abstract class?

As Interface has 3 main methods ( init() , service() and destroy() ) which is implemented by the HttpServlet (abstract class) which is extended by your servlet class which process the browser requests made to the server using these three methods.

What is service method in servlet?

service() method: The service() method of the Servlet is invoked to inform the Servlet about the client requests. This method uses ServletRequest object to collect the data requested by the client. This method uses ServletResponse object to generate the output content.

Which methods are defined in HttpServlet class?

The HttpServlet class extends the GenericServlet class and implements Serializable interface. It provides http specific methods such as doGet, doPost, doHead, doTrace etc.

What is the syntax of service () method?

4) service method is invoked Notice that servlet is initialized only once. The syntax of the service method of the Servlet interface is given below: public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException.


1 Answers

HttpServlet implements Servlet whose service method javadoc states

Called by the servlet container to allow the servlet to respond to a request.

This is the entry point of all Servlet handling. The Servlet container instantiates your Servlet class and invokes this method on the generated instance if it determines that your Servlet should handle a request.

HttpServlet is an abstract class which implements this method by delegating to the appropriate doGet, doPost, doXyz methods, depending on the HTTP method used in the request.

@Override
public void service(ServletRequest req, ServletResponse res)
    throws ServletException, IOException
{
    HttpServletRequest  request;
    HttpServletResponse response;

    if (!(req instanceof HttpServletRequest &&
            res instanceof HttpServletResponse)) {
        throw new ServletException("non-HTTP request or response");
    }

    request = (HttpServletRequest) req;
    response = (HttpServletResponse) res;

    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 = req.getDateHeader(HEADER_IFMODSINCE);
            if (ifModifiedSince < lastModified) {
                // 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);
    }
}

If you override the service method from HttpServlet, you lose that behavior and revert back to a single handling of all Servlet requests.

like image 190
Sotirios Delimanolis Avatar answered Sep 19 '22 09:09

Sotirios Delimanolis