1

This is a very basic question that i would like to understand.

I am running Fresh Apache tomcat server on port 8080, and when i type URL http://localhost:8080, i see that browser sends following request to tomcat.

GET / HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8

I see the below http response with Content-type:text/html

enter image description here

My question:

1) How / parameter of GET request mapped to this above html page as response at tomcat side, when tomcat server received this GET request? Is this something to do with below xml element in tomcat/conf/web.xml? What is the flow on tomcat side after receiving this request?

<!-- The mapping for the default servlet -->
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
overexchange
  • 15,768
  • 30
  • 152
  • 347
  • this query [link](http://stackoverflow.com/questions/14223150/mapping-a-specific-servlet-to-be-the-default-servlet-in-tomcat?rq=1) talks about specific servlet getting mapped as default, But my question is to understand the high level flow from tomcat receiving the request and sending the response. – overexchange May 31 '14 at 07:25
  • About what parameter are you talking? – Roman C May 31 '14 at 07:44
  • @RomanC i would like to understand, how {GET /} request gets mapped to above html response. Where is this html located? – overexchange Jun 01 '14 at 00:47
  • The default is `webapps/ROOT`, inside it you will find `index.jsp`. – Roman C Jun 01 '14 at 13:19
  • @RomanC so trigering this html has nothing to do with any web.xml right? – overexchange Jun 01 '14 at 13:39
  • Except that one mentioned in your question. – Roman C Jun 01 '14 at 13:44
  • @RomanC so that means, servlet container piicks default servlet using the mentioned web.xml and default servlet will display the html that i mentioned above? Is that the flow? – overexchange Jun 01 '14 at 16:23
  • yes the default servlet picks up that jsp and return to the browser. – Roman C Jun 01 '14 at 17:42
  • @RomanC Generally, web.xml sit in webapps folder confined to some particular app. But Why this web.xml is sitting in tomcat/conf folder? Because servlet container generally picks web.xml from tomcat/webapps/someapp folder, right? – overexchange Jun 02 '14 at 05:59

2 Answers2

2

Tomcat Architecture

A UML sequence diagram of the Request Process Flow will help you to understand it.

Read more about the following sections directly from official document of Apache Tomcat 7:

  • Overview - An overview of the Tomcat server architecture with key terms and concepts.
  • Server Startup - A detailed description, with sequence diagrams, of how the Tomcat server starts up.
  • Request Process Flow - A detailed description of how Tomcat handles a request.

It might help you to understand the url-pattern

Servlet Matching Procedure

A request may match more than one servlet-mapping in a given context. The servlet container uses a straightforward matching procedure to determine the best match.

The matching procedure has four simple rules.

  • First, the container prefers an exact path match over a wildcard path match.

  • Second, the container prefers to match the longest pattern.

  • Third, the container prefers path matches over filetype matches.

  • Finally, the pattern <url-pattern>/</url-pattern> always matches any request that no other pattern matches.


Have a look at my post How does a servlets filter identify next destination is another filter or a servlet/jsp? for detailed description to understand it visually.

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
  • we have web.xml provided by tomcat in tomcat/conf directlory. If we write our own app, it sits in tomcat/webapps/app/web-inf/web.xml so How does tomcat understand whether / parameter in GET request refer to default servlet of tomcat/conf/web.xml or default servlet of tomcat/webapps/app/web-inf/web.xml? – overexchange May 31 '14 at 14:41
  • It might help you [Tomcat Default Servlet Reference](http://tomcat.apache.org/tomcat-5.5-doc/default-servlet.html) – Braj May 31 '14 at 14:52
  • where is Catalina.Servlets.Defaultservlet class located? Which jar file or directory? – overexchange May 31 '14 at 16:04
  • It's inside the `catalina.jar` reside in tomcat lib folder. Read more on [The Apache Tomcat 5.5 Servlet/JSP Container](http://tomcat.apache.org/tomcat-5.5-doc/class-loader-howto.html) – Braj May 31 '14 at 17:25
  • the html response that is shown above is due to this default servlet class that is sitting in catalina.jar? Because we said that defaultservlet got executed for my {GET /} req. How is this html got triggered for my {GET /} request? Where is this html sitting? – overexchange Jun 01 '14 at 00:16
  • first thing, i did not deploy any app yet. based on pattern filtering using the above 4 rules, i think exact path match rule is picking url pattern / for which there is a catalina.jar default servlet, but how html is triggered as response? I did not understand that – overexchange Jun 01 '14 at 09:20
1

Right.This is all with this web.xml.this is one of the most important file in a Java web application.The / paramter of GET is mapped by a servlet which is build in.To better understand this you need to learn a bit JSP/Servlet thing

Abhinab Kanrar
  • 1,532
  • 2
  • 20
  • 46
  • as you said, this because of web.xml, but web.xml is pointing to default servlet, how do i understand, about the trigger of html as response? – overexchange Jun 01 '14 at 06:02
  • Whenever your app is deployed and request is coming for the first time,automatically this servlet will be called and this serrvlet will forward the request to other servlet through chain. – Abhinab Kanrar Jun 01 '14 at 07:11
  • here the scenario is, where there is no app yet deployed. am trying to understand how html response got triggered as shown above for 'GET /' request – overexchange Jun 01 '14 at 08:34