Servlets are platform independent and can work with almost all the web servers. They can be executed on any web server that supports the servlet API. Some of the web servers having built-in support for Java servlets are Tomcat server (Apache), Java web server (Sun Microsystems), Enterprise server (Netscape), Zeus web server (Zeus Technology), Tengah application server (Weblogic) and Sun web server (Sun Microsystems).
In this unit, we will be using the Tomcat server to explain the steps required to execute the servlet in Windows environment. Assume that the default location of Tomcat 6.0 is as follows.
C:\Program Files\Apache Software Foundation\Tomcat 6.0\
Following are the steps to create and execute the servlet.
1. Create a . java source file containing the code for creating servlet and save it with the name, say Sample. java.
2. Compile this source file, as a result Sample . class file will be created.
3. Copy the Sample. class file in the directory under the webapps directory of Tomcat. The
path for the directory is as follows.
C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\MyApp\WEB-INF\classes
Add the name and mapping of this servlet in the web . xml file. The path of this file is as follows.
C:\Program Files\Apache Software Foundation\Tomcat6.0\webapps\MyApp\WEB-INF
4. Add the following statements in the section which defines the servlets.
Also, add the following statements in the section which defines the servlets mappings.
5. Start the Tomcat. To start the Tomcat, click the Start menu, point to All Programs, point to Apache Tomcat 6.0 and then click Configure Tomcat. This displays the Apache Tomcat Properties. Click the Start button.
6. Start a web browser and request for the servlet. Note that the HTML code can be included in the servlet code or a separate HTML file can be used. If the HTML code is included in the servlet code, enter the following URL in the address bar.
If separate HTML file is used for the HTML code, enter the following URL in the address bar.
Here, 127 . 0 . 0 . 1 : 8O80 is an IP address of the local system. Note that the . html file must be saved in the MyAppdirectory.
The output of the servlet will be displayed in the display area of browser.