Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
Installation, Configuration and running Servlets 
 

In this section, we will see as how to install a WebServer, configure it and finally run servlets using this server.

 

Installation, Configuration and running Servlets

                         

In this section, we will see as how to install a WebServer, configure it and finally run servlets using this server. Throughout this tutorial, we will be using Apache’s Tomcat server as the WebServer. Tomcat is not only an open and free server, but also the most preferred WebServer across the world. A few reasons we can attribute for its popularity is – Easy to install and configure, very less memory footprint, fast, powerful and portable. It is the ideal server for learning purpose. 

  1. Installation of Tomcat Server and JDK

    As mentioned earlier, Apache’s Tomcat Server is free software available for download @ www.apache.org. The current version of Tomcat Server is 6.0 (as of November 2007). This Server supports Java Servlets 2.5 and Java Server Pages (JSPs) 2.1 specifications. In case of doubt or confusion, you can refer to the abundant documentation repository available on this site.

    Important software required for running this server is Sun’s JDK (Java Development Kit) and JRE (Java Runtime Environment). The current version of JDK is 6.0. Like Tomcat, JDK is also free and is available for download at www.java.sun.com.
  2. Configuring Tomcat Server

    • Set JAVA_HOME variable - You have to set this variable which points to the base installation directory of JDK installation. (e.g. c:\program file\java\jdk1.6.0). You can either set this from the command prompt or from My Computer -> Properties -> Advanced -> Environment Variables.
    • Specify the Server Port – You can change the server port from 8080 to 80 (if you wish to) by editing the server.xml file in the conf folder. The path would be something like this – c:\program files\apache software foundation\tomcat6\conf\server.xml
  3. Run Tomcat Server

    Once the above pre-requisites are taken care, you can test as whether the server is successfully installed as follows:

    Step 1

    • Go to C:\Program Files\Apache Software Foundation\Tomcat 6.0\bin and double click on tomcat6

    OR

    • Go to Start->Programs->Apache Tomcat 6.0 -> Monitor Tomcat. You will notice an icon appear on the right side of your Status Bar.   Right click on this icon and click on Start service.

    Step 2

    • Open your Browser (e.g. MS Internet Explorer) and type the following URL :

    http://localhost/ (If you have changed to port # to 80)

    OR

    • Open your Browser (e.g. MS Internet Explorer) and type the following URL :

    http://localhost:8080/ (If you have NOT changed the default port #)

    In either case, you should get a page similar to the one in Figure-8 which signifies that the Tomcat Server is successfully running on your machine.
  4. Compile and Execute your Servlet

    This section through a step by step (and illustration) approach explains as how to compile and then run a servlet using Tomcat Server. Though this explanation is specific to Tomcat, the procedure explained holds true for other Web servers too (e.g. JRun, Caucho’s Resin).

    Step 1 – Compile your servlet program

    The first step is to compile your servlet program. The procedure is no different from that of writing and compiling a java program. But, the point to be noted is that neither the javax.servlet.* nor the javax.servlet.http.* is part of the standard JDK. It has to be exclusively added in the CLASSPATH. The set of classes required for writing servlets is available in a jar file called servlet-api.jar. This jar file can be downloaded from several sources. However, the easiest one is to use this jar file available with the Tomcat server (C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\servlet-api.jar). You need to include this path in CLASSPATH. Once you have done this, you will be able to successfully compile your servlet program. Ensure that the class file is created successfully.

    Step 2 – Create your Web application folder

    The next step is to create your web application folder. The name of the folder can be any valid and logical name that represents your application (e.g. bank_apps, airline_tickets_booking, shopping_cart,etc). But the most important criterion is that this folder should be created under webapps folder. The path would be similar or close to this - C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps. For demo purpose, let us create a folder called demo-examples under the webapps folder. 


    Figure- depicts the same.

    Step 3 – Create the WEB-INF folder

    The third step is to create the WEB-INF folder. This folder should be created under your web application folder that you created in the previous step. Figure-10 shows the WEB-INF folder being placed under the demo-examples folder.


    Figure – WEB-INF folder inside web application folder

    Step 4 – Create the web.xml file and the classes folder

    The fourth step is to create the web.xml file and the classes folder. Ensure that the web.xml and classes folder are created under the WEB-INF folder. Figure-11 shows this file and folder being placed under the WEB-INF folder.


    Figure – web.xml file and the classes folder

    Note – Instead of creating the web.xml file an easy way would be to copy an existing web.xml file (e.g. C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\examples\WEB-INF) and paste it into this folder. You can later edit this file and add relevant information to your web application.

    Step 5 – Copy the servlet class to the classes folder

    We need to copy the servlet class file to the classes folder in order to run the servlet that we created. All you need to do is copy the servlet class file (the file we obtained from Step 1) to this folder. Figure-12 shows the servlet_lifecycle (refer section 1.2.3.) class being placed in the classes folder.


    Figure – servlet class file placed under classes folder

    Step 6 – Edit web.xml to include servlet’s name and url pattern

    This step involves two actions viz. including the servlet’s name and then mentioning the url pattern. Let us first see as how to include the servlet’s name in the web.xml file. Open the web.xml file and include the servlet’s name as shown in Figure-13.


    Figure– Include servlet’s name using the <servlet> </servlet> tag

    Note – The servlet-name need not be the same as that of the class name. You can give a different name (or alias) to the actual servlet. This is one of the main reasons as why this tag is used for.

    Next, include the url pattern using the <servlet-mapping> </servlet-mapping> tag. The url pattern defines as how a user can access the servlet from the browser. Figure-14 shows the url pattern entry for our current servlet.


    Figure – Include url-pattern using the <servlet-mapping> </servlet-mapping> tag

    Note – Please remember that the path given in the url-pattern is a relative path. This means that this path is w.r.t. your web applications folder (demo-examples in this case).

    Step 7 – Run Tomcat server and then execute your Servlet

    This step again involves two actions viz. running the Web Server and then executing the servlet. To run the server, follow the steps explained in Section 1.3.3.

    After ensuring that the web server is running successfully, you can run your servlet. To do this, open your web browser and enter the url as specified in the web.xml file. The complete url that needs to be entered in the browser is:

    http://localhost/demo-examples/servlet_lifecycle


    Figure – Our servlet’s output!

    Eureka! Here’s the output of our first servlet. After a long and pain staking effort, we finally got an output! As mentioned in Section 1.2.3. you can keep refreshing the browser window and see for yourself as how i value is incremented (a proof that the doGet is called every time you re-invoke a servlet).

                         

» View all related tutorials
Related Tags: java c database web jsp file ide browser data redirect servlet view user link page vi new this id create

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

3 comments so far (
post your own) View All Comments Latest 10 Comments:

i have a lots of doubt abt tomcat and how to run a program of servlet in tomcat.after go through this i get a basic idea about this,so thanks alot for helping me.

Posted by Monalisha Parida on Monday, 09.8.08 @ 17:41pm | #78025

actually i had blank idea about servlets now after gone through this site i gathered a wonderfull knowledge.thank you very much
bye

suganthi arumugam.

Posted by suganthi arumugam on Tuesday, 03.18.08 @ 16:35pm | #53178

I tried out with this procedure, but still it gives a couple of errors, one of them is :

package javax.servlet does not exist
import javax.servlet.*;
^

Posted by Raghava on Friday, 01.25.08 @ 14:44pm | #46116

Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.