Tomcat

Prerequisite, before we dwell in tomcat one should clear the below concept.

related to JRE and JDK,

JDK ( Java development Tool Kit) it comes with Java libraries which are

used/referenced by software develpers in their code. The code is then compiled

using the javac utility in the JDK, once compiled the code the javac creates

class files, The class files are then executed by JVM of the JRE.

JRE (Java Runtime Environment) : Contains the JVM (Java Virtual Machine),

class libraries. It does not contain any development tools.

Its job is to run programs i.e class files created by the above said JDK.

Tomcat is an application server developed by Apache Software

Foundation. It is used to run the JSP and Java servlets.

Installation :

You could download the installer from here.

After the download install it, make sure you have installed the JRE on your system.

You could download the jre from here

While installation it would ask for the admin password for tomcat manager, using which

you could easily deploy your application.

After installed you have the following directory structure is created in the installed directory

bin : Contains the scripts for Startup and shutdown of the tomcat server

conf : Contains the various configuration files

Server.xml = Main configuration file of the tomcat server

web.xml = This sets the default values for the various webapps

deployed in the tomcat server

logs : Here it places all the log files

src : The Servlets API's source files

webapps : Here we place our web application, in specific format we'll discuss later

work : Tomcat places the compiled jsp files during its work

Now lets start the practical

We will create a sample jsp file "index.jsp"

<head>

<title>JSP Test page.</title>

</head>

<body>

<p align="center"><font color="#FF0000" size="6"><%="Tomcat"%></font></p>

<p align="center"><font color="#800000" size="6"><%="Hello JSP"%> </font></p>

</body>

</html>

After the page has been created it has to be deployed on the tomcat server

1) create a directory name "test"

2) copy the index.jsp in there

3) Create the following directory structure

└───test

└───WEB-INF

├───classes

└───lib

4) copy the following web.xml file in the WEB-INF directory

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app>

</web-app>

5) Create a war file by executing the following command in the one the test directory

jar cvf test.war .

Above will create war file ready to be deployed on to server

6) Start the tomcat server , go to the admin console

http://<ip-address>:8080

7) In bottom you will find path to upload war file and deploy it

8) Now click on the link of the deployed war file.

Servlets :

Servlets are java classes which run on the server (application server) and produce html which

is sent to the requesting browser.

these contain java code with html tags.

Lets start implementing sample servlet

1) Write the code for sample java servlet

example

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class hello extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws IOException, ServletException

{

response.setContentType("text/html");

PrintWriter out = response.getWriter();

out.println("<html>");

out.println("<head>");

out.println("<title>Hello World!</title>");

out.println("</head>");

out.println("<body>");

out.println("<h1>First step in work of servlets</h1>");

out.println("</body>");

out.println("</html>");

}

}

2) Compile the above code to create a class file

use the following command

javac -classpath "c:\~\~\j2ee.jar" hello.java

above would compile the code to produce hello.class file

3) Our code is now created, but now we have to create a application i.e we have the package it

as war file so that it could be deployed to the application server.

For that create following directory structure

hello

└───hello

└───WEB-INF

├───classes\hello.class

└───lib

.\web.xml

About web.xml

web.xml is the descriptor it tells what we have in this application.

here is our web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app>

<display-name>Test hello world</display-name>

<description>

Testing java servlets

</description>

<servlet>

<servlet-name>hello</servlet-name>

<servlet-class>hello</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>hello</servlet-name>

<url-pattern>/hello</url-pattern>

</servlet-mapping>

</web-app>

4) Now create a war file for deployment

JAR -cvf hello.war WEB-INF

5) Login to the administration deploy and deploy the war file, as for tomcat go to the tomcat management console i.e at http://<host>:8080

in the select war file field select our newly created war file and deploy it.

6) Thats it ! war file deployed we could acces our servlet at the urll http://<host>:8080/hello/hello

In the background

The file web.xml has the description of the what are the contents of the deployment application

<servlet-name> tag gives a identity to the class file inside the applicatoin

and in the <servlet-mapping> maps the class through name to the url i.e http://<host>:8080/<app-name>/<url-pattern>

and the class file is execute to produce the html which is send to the requesting browser

Again for the jsp , the above routine is followed, first the java file is created which is compiled to produce class file

which is then executed to produce the html file copy of which is then to the requesting browser.