1

My question is based on understanding of how webserver - servlet container interacts. So please correct me if my basic understanding is wrong.

  1. consider a request coming in: www.bank.com/credit-cards.

  2. As soon as the Tomcat Server gets the request, it forwards it to the servlet Container whose web.xml maps to the incoming URI which is /credit-cards above

  3. Servlet Container does its routine of instantiating the servlet (if this the first request). if not, it will create a Thread for this request and hands it over to Servlet, which handles generating the response. Tomcat then returns the response to client browser.

  4. Now suppose I have another request at www.bank.com/accounts. This is served by a different application with-in the same server. It is NOT a different servlet with in the same application.

  5. Now how is the url-mapping kept? How does the server knows which application it should forward the request to? The server does not hold any descriptor files. Application 1 and Application 2 has web.xml files that maps the incoming the url to servlets. Does all the URL-mapping and servlets gets registered somewhere in the server for a look up?

  6. I guess each application should have its own container. That is there will be two servlet containers in the above case.

  7. Is this a common scenario? I don't know any real world examples where servlets/JSPs are used that holds mulitple applications with in a server (probably a user cannot differentiate if the two request come from the same or different appications anyway)

brain storm
  • 30,124
  • 69
  • 225
  • 393
  • `context-path` is the key differentiator – jmj Jul 10 '14 at 19:07
  • @JigarJoshi: where does the context-path set? can you elaborate more.. – brain storm Jul 10 '14 at 19:11
  • 1
    http://javapapers.com/servlet/what-is-servlet-mapping/ – jmj Jul 10 '14 at 19:15
  • 1
    the context path be the name of the war file or defined in a context.xml or in a Context tag in the server.xml. http://tomcat.apache.org/tomcat-7.0-doc/config/context.html#Defining_a_context – Hank Lapidez Jul 10 '14 at 19:16
  • 1
    You can have many web applications running in the same Tomcat instance. Each web application has its own `ServletContext`. If there is a conflicting path mapping, the container will complain. – Sotirios Delimanolis Jul 10 '14 at 19:17
  • @SotiriosDelimanolis: how does Tomcat instance know which application serves the url. I understand web.xml has url-mapping to servlets. But how does the Tomcat instance which application serves the incoming url request – brain storm Jul 10 '14 at 19:23
  • 1
    Tomcat owns/manages the url-mappings from the various deployment descriptors. – Sotirios Delimanolis Jul 10 '14 at 19:24
  • @SotiriosDelimanolis: so there is only one servlet container irrespective of any number of applications? – brain storm Jul 10 '14 at 19:28
  • That's up to you. You can have as many servlet container instances as you want (provided you change port(s)). A servlet container can be running multiple web applications. – Sotirios Delimanolis Jul 10 '14 at 19:30
  • @SotiriosDelimanolis: so is there a configuration to set how many servlet container instances I want? If I am correct, when the traffic to your website is high, having mulitple servlet container instances helps in handling the request faster.. – brain storm Jul 10 '14 at 19:37
  • No. This is something you do manually. Launching an instance of a servlet container means starting a `java` process. You can launch as many as you want. – Sotirios Delimanolis Jul 10 '14 at 19:39
  • and would that decrease load on server or handles the requests effectively? – brain storm Jul 10 '14 at 19:41

1 Answers1

2

Each applications has its own folder under webapps.

The application credit-cards is under .../webapps/credit-cards/.

The application accounts is under .../webapps/accounts/.

Any file directly on the server root in the URL, like http://localhost:8080/index.jsp is under the root application in the folder .../webapps/ROOT/. That's how Tomcat knows.

Servlet mappings within each folder's ./WEB-INF/web.xml map the servlet underneath the application.

Where you could get into trouble is if you created folders under .../webapps/ROOT/ or mapped servlets there that would conflict with the URL of another application.

Edit: As Bruno pointed out in a comment, this answer only applies when auto deployment is on.

developerwjk
  • 8,619
  • 2
  • 17
  • 33
  • so for a request of `/credit-cards`, I need to have a webapp folder with exactly same name. That makes it tighly coupled right? – brain storm Jul 10 '14 at 19:27
  • You're talking about the case where [auto deployment is used here](http://tomcat.apache.org/tomcat-7.0-doc/config/host.html#Automatic_Application_Deployment), you can have more complex situations if you tweak the [context configuration](http://tomcat.apache.org/tomcat-7.0-doc/config/context.html). – Bruno Jul 10 '14 at 19:29
  • I find it confusing to understand. In the link posted above jigar Joshi, it mentions that servlet container determines which application to forward the request to. But the context path setting is that of Tomcat I suppose and that serves the URL mapped by web-applications. Servlet Container and Tomcat instances are two seperate things. so which does the APP mapping is the point of my confusion.. – brain storm Jul 10 '14 at 19:40
  • @brain storm, Although technically Tomcat is the Servlet Container, some people may use "servlet container" to refer to the application (which contains servlets obviously), thus confusing the terminology. *Or maybe I have the terminology backwards myself.* – developerwjk Jul 10 '14 at 19:50
  • Having verified the proper terminology, Tomcat is the Servlet Container, and it spawns several Context Containers (which contain one application each). – developerwjk Jul 10 '14 at 19:59