Docsity

Java Servlets - Web Design and Development - Lecture Handouts, Lecture notes of Web Design and Development

Partial preview of the text

Download Java Servlets - Web Design and Development - Lecture Handouts and more Lecture notes Web Design and Development in PDF only on Docsity!

Web Design & Development CS-

HANDOUTS

WEB DESIGN AND DEVELOPMENT

Web Design & Development CS-

Lecture 26

Java Servlets

Servlets are java technology’s answer to CGI programming. CGI was widely used for generating dynamic content before Servlets arrived. They were programs written mostly in C,C++ that run on a web server and used to build web pages.

As you can see in the figure below, a client sends a request to web server, server forwards that request to a servlet, servlet generates dynamic content, mostly in the form of HTML pages, and returns it back to the server, which sends it back to the client. Hence we can say that servlet is extending the functionality of the webserver (The job of the earlier servers was to respond only to request, by may be sending the required html file back to the client, and generally no processing was performed on the server)

What Servlets can do?

— Servlets can do anything that a java class can do. For example, connecting with database, reading/writing data to/from file etc.

— Handles requests sent by the user (clients) and generates response dynamically (normally HTML pages).

— The dynamically generated content is send back to the user through a webserver (client)

Client/

browser

Server

Request

Response

Servlet

JVM

Extends the functionality of the server by generating HTML pages dynamically

Request

Response

Web Design & Development CS-

Jakarta Servlet Engine (Tomcat)

Jakarta is an Apache project and tomcat is one of its subprojects. Apache Tomcat is an open source web server, which is used as an official reference implementation of Java Servlets and Java Server Pages technologies.

Tomcat is developed in an open and participatory environment and released under the Apache software license

Environment Setup

To work with servlets and JSP technologies, you first need to set up the environment. Tomcat installation can be performed in two different ways (a) using .zip file (b) using .exe file. This setup process is broken down into the following steps:

  1. Download the Apache Tomcat Server
  2. Install Tomcat
  3. Set the JAVA_HOME variable
  4. Set the CATALINA_HOME variable
  5. Set the CLASSPATH variable
  6. Test the Server

Web Design & Development CS-

Environment Setup Using .zip File

Let’s take a detail look on each step and get some hands on experience of environment setup.

1. Download the Apache Tomcat Server

From the http://tomcat.apache.org, download the zip file for the current release (e.g. jakarta-tomcat-5.5.9.zip or any latest version) on your C:\ drive. There are different releases available on site. Select to download .zip file from the Binary Distributions Æ core section.

Note: J2SE 5.0 must be installed prior to use the 5.5.9 version of tomcat.

2. Installing Tomcat using .zip file

  • Unzip the file into a location (e.g. C:). (Rightclick on the zip file and select unziphere option )
  • When the zip file will unzipped a directory structure will be created on your computer such as:
  • The C:\jakarta-tomcat-5.5.9 folder is generally referred as root directory or CATALINA_HOME

Note: After extraction , make sure C:\jakarta-tomcat-5.5.9 contains a bin subdirectory. Sometimes students create their own directory and unzip the file there such as C:\jakarta-tomcat-5.5.9\jakarta-tomcat-5.5.9.This causes problems while giving path information

3. Set the JAVA_HOME variable

— JAVA_HOME indicates the root directory of your jdk. Set the JAVA_HOME environment variable to tell Tomcat, where to find java

Web Design & Development CS-

— Create a new User variable by clicking New button as shown above, the New User Variable window will appear — Set name of variable JAVA_HOME — The value is the installation directory of JDK (for example C:\Program Files\j2sdk_nb\j2sdk1.4.2). This is shown below in the picture. Please note that bin folder is not included in the path.

— Press Ok button to finish

Web Design & Development CS-

4. Set the CATALINA_HOME variable

CATALINA_HOME is used to tell the system about the root directory of the TOMCAT. There are various files (classes, exe etc) needed by the system to run. CATALINA_HOME is used to tell your system (in this case your web server Tomcat) where the required files are.

— To Set the CATALINA_HOME environment variable, create another 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 C drive. It should be C:\jakarta-tomcat-5.5.9. This is shown below:

— Press Ok button to finish

Note: To run Tomcat (web server) you need to set only the two environment

variables and these are JAVA_HOME & CATALINA_HOME

5. Set the CLASSPATH variable

Since servlets and JSP are not part of the Java 2 platform, standard edition, you have to identify the servlet classes to the compiler. The server already knows about the servlet classes, but the compiler (i.e., javac) you use for compiling source files of servlet does not. So if you don't set your CLASSPATH, any attempt to compile servlets, tag libraries, or other classes that use the servlet API will fail with error messages about unknown classes.

— To Set the CLASSPATH environment variable, create another User Variable.

— Type CLASSPATH as the name of the environment variable.

— Its value should be the path for servlet-api.jar and jsp-api.jar. These file can be found on following path:

Web Design & Development CS-

Web Design & Development CS-

There is another easier way to carry out the environment setup using .exe file. However, it is strongly recommended that you must complete the environment setup using .zip file to know the essential fundamentals.

Environment Setup Using .exe File

Let’s look at the steps involved to accomplish the environment setup using .exe file.

1. Download the Apache Tomcat Server

From the http://tomcat.apache.org, download the .exe file for the current release (e.g. jakarta-tomcat-5.5.9.zip) on your C:\ drive. There are different releases available on site. Select to download Windows executable (.exe) file from Binary Distributions Æ Core section.

Note: J2SE 5.0 must be installed to use the 5.5.9 version of tomcat.

2. Installing Tomcat using .exe file - Run the .exe file by double clicking on it. - Moving forward in setup, you will reach to the following window

Web Design & Development CS-

  • Double clicking on this button will open up Apache Tomcat Properties window. From here you can start or stop your web server. You can also configure many options if you want to. This properties window is shown below: 3. Set the JAVA_HOME variable

Choosing .exe mode does not require completing this step.

4. Set the CATALINA_HOME variable

Choosing .exe mode does not require completing this step.

5. Set the CLASSPATH variable

Same as step 5 of .zip installation mode

Web Design & Development CS-

6. Test the server

If tomcat installation is made using .exe file, follow these steps

  • Open the Apache Tomcat properties window by clicking on the Apache Tomcat button from Quick Launch.
  • Start the tomcat server if it is not running by clicking on Start button.
  • Open up a browser window and type http://localhost:8080. This should open the default page of Tomcat as shown in the next diagram:

Note: If default page doesn’t displayed, open up an internet explorer window, move on to Tools Æ Internet Options Æ Connections ÆLAN Settings. Make sure that option of “Bypass proxy server for local addresses” is unchecked.