Home » Java Basics » 09 - Java Servlet Basics
9

Servlets and JSPs

Integral parts of most web applications

Servlets and JSPs are an integral part of most web applications. They handle HTTP requests, pass the requests on to other components or process them and construct HTTP responses that the container compiles into HTML. This HTML output is sent to the client computer through TCP/IP. We may easily construct a few JSP scripts and servlets that work with the fruit class. First, let us set up tomcat to deploy a new 'ShowFruits' application.

  1. Ensure that the tomcat server is running. Click on Start -> Apache Tomcat 5.5 -> Tomcat Manager. An Internet Browser window will open but you will be asked to enter your user name and password before viewing contents. The following web page will be displayed once you login.
Screen Shot 9a: The Tom Cat server Manager
Screen Shot 9a: The Tom Cat server Manager
  1. Note the message box that displays the status of the server. You may start, stop, reload and configure all applications on the server from the manager.
  2. Let us create a new application. First, click on Start -> Apache Tomcat 5.5 -> Tomcat 5.5 Program Directory. Navigate into the subfolder 'webapps'. Create a new folder called ShowFruits in the 'webapps' folder.
  3. Create the following directory structure under ShowFruits (Case Sensitive!)
 
ShowFruits
________|_________
|                 |
|                 |
WEB-INF           images
_____|______
|            |
|            |
classes       lib
 
  1. Every application deployed through tomcat should contain the above directory structure (the images directory is optional). Tomcat recovers the configuration for an application from the 'WEB-INF' directory and searches for Java classes in the classes directory. HTML pages and JSP go into the main ShowFruits directory. The lib folder may contain jar files and other items the application uses.
  2. Go one level up and into the ROOT subfolder. Go into the WEB-INF folder. Copy the web.xml file.
  3. Go back to ShowFruits\WEB-INF. Paste the web.xml file. This file holds important configuration-related parameters. Change the sentence 'Welcome to Tomcat' within the display-name and description tags to 'Show Fruits'. Save the file and close it.
  4. Now, go back to the IE Browser window running the Tomcat manager and refresh the window's contents. You will see the new application with the 'Show Fruits' description next to it. You can also use the 'Deploy' section on this page to deploy new applications. An application may be removed from the server, started, stopped, or reloaded through the manager. Stopping an application makes it unavailable on the Internet. Changes in class files and other modifications warrant application reload; reloading an application clears the cache memory area and loads programs afresh. Click on the 'Start' link to start up the ShowFruits application.
  5. Several sample applications that will help you learn a lot more about tomcat are also available; just start tomcat and browse to the appropriate example through http://localhost:8080//<application_name%>
Screen Shot 9b: Applications deployed through Tomcat
Screen Shot 9b: Applications deployed through Tomcat