Tomcat
Apache Tomcat in open source web server which is used as official reference
Implementation of Java Servlets and Java Server Pages technologies.
- To install Tomcat
- Download Tomcat Server (e.g. jakarta-tomcat-5.0.28.zip) on your D:\ drive from Unzip Tomcat in D:\
Setting up Environment variables to run Tomcat
There are various files (classes, exe etc) needed by the system to run. Environment.
Variables are used to tell your system (in this case your web server Tomcat) where the
Required files are.
To run Tomcat (web server) you need to set two environment variables.
- CATALINA_HOME
- Right click on My Computer icon. Select the advanced tab and click the Environment variables button.
- Create a new User Variable.
- Type CATALINA_HOME as the name of the environment variable.
- Its value should be the path till your top-level Tomcat directory. If you Have unzipped the Tomcat in D drive. It should be D:\jakarta-tomcat-5.0.28
- JAVA_HOME.
- Next step is to tell Tomcat where JDK is (i.e. to set JAVA_HOME)
- Create a new User Variable
- Set name of variable JAVA_HOME
- The value is the installation directory of JDK (in LUMS lab the value is Something likes C:\Program Files\j2sdk_nb\j2sdk1.4.2)
Now Tomcat web server setup is complete.
- Open the D:\jakarta-tomcat-5.0.28 \bin folder and locate the startup.bat file.
- - Double clicking on this file will open up a DOS window, which will disappear, and another DOS window will appear, the second window will stay there. If it does not your paths are not correct.
- Now to check whether your server is working or not open up a browser and type
http://localhost:8080. This should open the default page of Tomcat.
Setting up Environment variables to compile servlets
To compile servlets you need to import javax.servlet package, which is not included in The standard JDK. However it comes along Tomcat.
- CLASSPATH
- This variable tells compiler where to find Servlets Class Library (i.e.servlet.jar).
- Create a new User Variable.
- Type CLASSPATH as the name of the environment variable
- The value should be the path where servlet.jar file resides. In our case it is
D:\jakarta-tomcat-5.0.28\common\lib\servlet.jar
Tomcat Standard Directory Structure
A web application is defined as hierarchy of directories and files in a standard layout. Such hierarchies can be accessed in two forms
- Unpacked
- Where each directory & file exists in the file system separately.
- Used mostly during development
- Pack
- Known as Web Archive (WAR) file
- Mostly used to deploy web applications
The webapps folder in top-level Tomcat directory contains all the web applications Deployed on the server. Each application is deployed in a separate folder often referred as Context.
To make a new application in tomcat you need a specific folder hierarchy.
Step by step:
Step 1: Create a folder named myapp in D:\jakarta-tomcat-5.0.28\webapps folder.
Step 2: This name will also appear in the URL for your application. For example
Step 3: All JSP and html files will be kept in main application folder (D:\jakarta-tomcat- .0.28\webapps\myapp)
Step 4: Configuration files such as web.xml will go in WEB-INF folder (D:\jakartatomcat-
5.0.28\webapps\myapp\WEB-INF)
Step 5: Servlets and Java Beans will go in class’s folder (D:\jakarta-tomcat-5.0.28\webapps\myapp\WEB-INF\classes)
Step 6: To test application hierarchy, make simple index.html file. Place this file in main Application directory. Access using URL http://localhost:8080/myapp/index.html
Server’s Behavior:
- Restart Tomcat every time you create a new directory structure, a servlet or a java bean .So that it can recognize it.
- For JSP and html files you don’t have to restart the server.
Writing Sample Servlets
Servlet Types
- Servlets are based on two main packages
- javax.servlet.
=
- Javax.servlet. Http.
Every servlet must implement the javax.servlet. Servlet interface, it contains the
- servlet’s life cycle methods etc.
- GenericServlet class Implements javax.servlet.Servlet
- Extend this class for writing protocol independent servlets HttpServlet class
- Extends from GenericServlet class
- Adds functionality for writing HTTP specific servlets
- Extend from this class for writing HTTP based servlets
Types of HTTP requests
- GET: Requests a page from the server. This is the normal request used when browsing web pages.
- POST: This request is used to pass information to the server. Its most common Use is with HTML forms.
- PUT: Used to put a new web page on a server.
- DELETE: Used to delete a web page from the server.
- OPTIONS: Intended for use with the web server, listing the supported options.
- TRACE: Used to trace servers
How HTTP sends requests
- GET
Attribute-Value pair is attached with requested URL after ‘?’.
For example if attribute is ‘name’ and value is ‘ali’ then the request will be
- For HTTP servlet override doGet() methods of HttpServlet class to handle these type of requests.
- POST
Attribute-Value pair attached within the request body
Override doPost () method of HttpServlet class to handle POST requests.
Sample Hello World Servlet
1. Create a directory structure for your application (i.e. helloapp). This is a one time Process for any application
2. Create a HelloWorldServlet source file by extending this class from HttpServlet and overriding your desired method
- Compile it (If get error of not having required packages, check your class path)
4. Place the class file in the classes folder of your web application (i.e. helloapp). If you have packages then create a complete structure under classes folder
- Create a deployment descriptor (web.xml) and put it inside WEB-INF folder.
- Restart your server if already running
- Access it using Web browser
HelloWorldServlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorldServlet extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException
{
PrintWriter out = response.getWriter();
out.println(“<h1>Hello World!</h1>”);
}
}
Web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app>
<servlet>
<servlet-name>HelloWorldServlet</servlet-name>
<servlet-class>HelloWorldServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorldServlet</servlet-name>
<url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>
</web-app>
Compiling and Invoking Servlets
- Compile HelloWorldServlet.java using javac command.
- Put HelloWorldServlet class in D:\jakarta-tomcat-5.0.28/webapps/helloapp/WEBINF/Classes.
- Put web.xml file in D:\jakarta-tomcat-5.0.28/webapps/helloapp/WEB-INF
- Invoke your servlet by writing following command in web browser
Hello World!
You May Download The Entire Document From Here - Download
0 comments:
Post a Comment