AndreaNobili
AndreaNobili

Reputation: 42957

What exactly do the service() method of this HttpServlet?

this is my first time that I work on a Java project that use HttpServlet.

So I know that an HttpServlet is a program that run on a Web Application server and act as a middle layer between a request coming from a Web browser or other HTTP client and databases or applications on the HTTP server. So the servlet extend the competence of my application server.

I have some doubt to understand how exactly work this servlet founded into my project, into web.xml file I found this configuration:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd">    
<web-app>

    <display-name>My Project</display-name>

    <listener>
        <listener-class>it.sistinf.ediweb.quartz.QuartzListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>edimon</servlet-name>    
        <servlet-class>it.sistinf.ediweb.monitor.servlets.Monitoraggio</servlet-class>   
        <load-on-startup>0</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>edimon</servlet-name>    
        <url-pattern>/edimon.do/*</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>/logon.jsp</welcome-file>
    </welcome-file-list>

    <taglib>
        <taglib-uri>displaytag</taglib-uri>
        <taglib-location>/WEB-INF/displaytag-11.tld</taglib-location>
    </taglib>

</web-app>

So reading some documentation it seem to understand that I have to tell the servlet container (or application server) what servlets to deploy, and what URL's to map the servlets to.

In the previous case I am configuring a servlet named edimon implemented by the Monitoraggio class.

Then it is mapped the servlet to a URL or URL pattern. In this case the edimon servlet is mapping with the /edimon.do/* URL pattern. So when it is called something that match with the previous pattern the edimon servlet is performed.

Then into my Monitoraggio class that implement the HttpServlet I found the service() method:

public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        LoggerMDC.setup(req, res);
        Logger logger = (Logger) Logger.getStdLogger(Monitoraggio.class); // do not declare 'logger' as static field in order to work with MDC

        String service = req.getParameter("serv");

        char serviceId = Utility.getServizio(req.getParameter("serv"));

        if (checkSession(req, serviceId) == false) {
            gotoPage(ConfigurationFactory.getPropertiesPages().getProperty("pagina_errore_session"), req, res);
            return;
        }

        LoggerWatch loggerWatch = new LoggerWatch(Monitoraggio.class, Long.valueOf(System.getProperty(Constants.Keys.CONFIG_STATS_WARNING_THRESHOLD, String.valueOf(LoggerWatch.DEFAULT_WARNING_THRESHOLD))).longValue());
        if (logger.isTraceEnabled())
            logger.trace("lanciaServizio() | logger threshold: " + loggerWatch.getWarningThreshold());

        loggerWatch.start();
        loggerWatch.info(new StringBuffer("service() | servizio: [").append(service).append("] | service start").toString());

        String paginaDaLanciare = lanciaServizio(serviceId, req, res);
        String executionTime = loggerWatch.getInfoTime();

        //Modifica per export
        if (req.getSession().getAttribute("export") == null) {
            gotoPage(paginaDaLanciare, req, res);
        }

        loggerWatch.info(new StringBuffer("service() | servizio: [").append(service).append("] | [").append(executionTime).append("] after forward to ").append(paginaDaLanciare).toString(), true);
        loggerWatch.stop();

        req.getSession().removeAttribute("export");
        req.getSession().removeAttribute("stringaXML");
        req.getSession().removeAttribute("downbyte");

        return;
    }

Reading on the documentation it receives standard HTTP requests from the public service method and dispatches them to the doXXX methods defined in this class

So what exatly do this method? I can't understand how the servlet load the JSP

Upvotes: 0

Views: 279

Answers (1)

JB Nizet
JB Nizet

Reputation: 691755

The documentation you read describes what the service() method of HttpServlet does by default. Since your servlet overrides the service() method and provides a different implementation, it doesn't do that anymore. Instead, it does... what the code in the method does.

A servlet doesn't "load a JSP". I don't see how JSPs have any relation to the servlet code you posted. Maybe gotoPage() does tell the container to forward the request to a JSP. You should look at the documentation and/or code of that method to know what it does.

Upvotes: 1

Related Questions