Tomcat Web Server
Install Tomcat server
Download latest zip file of Tomcat Server from its official website http://tomcat.apache.org
Unzip downloaded apache-tomcat-9.x.zip file into c:\program files\java folder. It will create c:\program files\java\apache-tomcat-9.x folder.
Unzipped Tomcat directory will have following folder structure. It is important that you should know what each directory does:
It is important that you should know what each directory does:
\bin: It contains shell scripts and batch files that used to start and stop the server.
\conf: It contains global and server configuration XML files. It also contains user authentication setting.
\lib: It contains common jar files those are used by all deployed web applications.
\logs: It contains log files. Server errors are recorded in these log files.
\webapps: All web applications are deployed in this folder. Tomcat may have multiple web applications. For each web application \webapps folder contains a separate folder. A web application contains JSP, Servlet, HTML, Images, JavaScript, and other files.
\work: It contains temporary files those are used to execute a JSP file.
Make sure that JAVA_HOME environment variable point to your JDK home director. Please refer J2EE Environment Setup
Congratulation now your server is ready to start.
Go to .\bin folder and run startup.bat ( startup.sh in case of Unix/Linux) file. A command line window will be opened with information messages. It will show port 8080 in a message when the server is started.
Now time to test your server. Open a browser and enter URL http://localhost:8080. If a page is displayed with a TOMCAT logo then your server is successfully started.
Structure of a Web Application
A web application is deployed in \webapps folder of Tomcat. As per J2EE specifications, when a web application is deployed it will have following standard directory structure.
Steps to Deploy a Web Application
Before you assemble the application components, you must define the directory structure for the application. To do so:
Create a root directory with an appropriate name for the application, say \onlineresult
In the root directory \onlineresult, create the following directory structure.
\onlineresult :
Application JSP, HTML, JavaScript, Images, and Applet files
\WEB-INF
web.xml : application configuration file
\classes : contains all compiled servlets and java classes
\lib : contains library jars like Database Driver and JAVA Mail API jars.
Now start your Tomcat server.
Open a browser and enter url http://localhost:8080/onlineresult/index.html. You will see your page.
A special directory exists within the application hierarchy named “WEB-INF”. It is important to note that the WEB-INF directory is not part of the public document tree of the application. A browser can not access any document from URL that exists within WEB-INF directory.
WEB-INF contains
/WEB-INF/web.xml deployment descriptor, configures application.
/WEB-INF/classes/* directory for servlet and utility classes. The classes in this directory are used by the application.
/WEB-INF/lib/* contains library jars like database driver. All such archive files are used by the web application class loader to load classes from.
FAQ
Q: How do you start and stop Tomcat server?
Ans: Using .\bin\startup.bat and .\bin\shutdown.bat.
Q: Where do you deploy your application in Tomcat?
Ans: We deploy web application as war or exploded format in .\webapps folder.
Q: What is WAR file?
Ans: WAR is stands for Web-application ARchive. These files are simply zipped files using the java jar tool. It is used to distribute a collection of JSP, Servlets, classes, XML files, tag libraries, static web pages (HTML and related files) and other resources that together make a web application.
Q: What is the default port number of Tomcat server?
Ans: 8080.
Q: What is the configuration file of Tomcat?
Ans: Configuration file is .\conf\server.xml. You can configure port number, HTTPS protocol and other server optimization parameters in this file.
Q: How can you change the default port of Tomcat server?
Ans: Modify .\conf\server.xml and change port number in
<Connector port="8080" protocol="HTTP/1.1" tag.
Q: What is the deployment folder structure of a web application in Tomcat Server?
Ans: See above 'Structure of Web Application'.
Q: How do you configure data connection pool in Tomcat?
Ans: Using <datasource> tag in web.xml file.