A server like Tomcat is called a servlet container. These are web applications that a servlet container carries and manages. A servlet container must ensure that the web applications operate following Sun's Servlets/JSP Spec.
Components of a web application
Java web application consists of the following files.
- Java Classes, Java archive file(jar)
Some of the above components have a specified location.
- Servlets and Java classes in WEB-INF/classes
- Java archive files in WEB-INF/lib
- The web.xml in WEB-INF
Directories of Web application
The default location for web applications in Tomcat is CATALINA_HOME/webapps. If you want to add a new web application, create a subdirectory with the appropriate name in CATALINA_HOME/webapps. This subdirectory is the root directory of the web application. For Tomcat, this directory is called DocumentBase. You have to create the following subdirectories in DocumentBase.
The web application deployment descriptor, web.xml file must be here. Users cannot access files in the WEB-INF directory and its subdirectory through their web browser.
Java class files (bytecode) must be here.
Java archive file (jar) must be here. Note that the same bytecode can be in the WEB-INF/classes and the WEB-INF/lib. In this case, the Tomcat class loader first searches WEB-INF/classes and loads it into memory. The class loader ignores the bytecode of the Java archive file in the WEB-INF/lib. Hence that bytecode cannot participate in the program.
- Tomcat Class Loader
Tomcat Classloader does not refer to the environment variable, CLASSPATH.
It looks for classes in the following order:
- Java API
- WEB-INF/classes directory of a web application
- WEB-INF/lib directory of a web application
The heart of the web application is the web.xml file, called the deployment descriptor. A web.xml must be in the WEB-INF directory. A web.xml contains all the configuration information on a web application.
- ServletContext initialization parameter
- Servlet Definition
- Servlet Initialization parameters
- Servlet Mapping
- Session Configuration
- welcome file list
- error file list
Example and description of web.xml
Copy the CATALINA_HOME/webapps/ROOT/WEB-INF/web.xml file and paste it into the WEB-INF of your web application. Then open the web.xml file and delete everything in the web-app root element. You can configure your web application by adding subelements like below.
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <servlet> <servlet-name>TestServlet</servlet-name> <servlet-class>net.java_school.TestServlet</servlet-class> <init-param> <param-name>name</param-name> <param-value>value</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>TestServlet</servlet-name> <url-pattern>/Test</url-pattern> </servlet-mapping> <session-config> <session-timeout>30</session-timeout> </session-config> </web-app>
To test a web application that has this web.xml, you need to create the TestServlet. How to create a servlet is covered later.
The servlet element sets the servlet definition and its initial parameters.
If the load-on-startup has zero or positive value, Tomcat creates the servlet object when the web application starts. And then Tomcat calls the init() method of the servlet object to make its service available.
The servlet-mapping element is for servlet-to-URL mapping. As above settings, if you visit http://localhost:8080/contextPath/Test, the net.java_school.TestServlet servlet will respond.
The session-timeout element controls the lifetime of the HttpSession object. If session-timeout is 30, the servlet container will destroy the HttpSession object if it has no action for 30 minutes.
You can use the jar tool (jar.exe) to make your web application a single file. Once development is complete, you can bundle your web application and distribute it to other servlet containers. To do this, you need to create a file with the extension war. If the web application's document base is C:\Program Files\Apache Software Foundation\Tomcat 8.5\webapps\test, go to the document base and run the following:
C:\Program Files\Apache Software Foundation\Tomcat 8.5\webapps\test>jar cvf test.war .
You put the test.war into the default web application folder of another servlet container, the servlet container will automatically deploy and load the test web application.References