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.
<servlet>
<servlet-name>Sample</servlet-name>
<servlet-class>Sample</servlet-class>
</servlet>
Also, add the following statements in the section which defines the servlets mappings.
<servlet-mapping>
<servlet-name>Sample</servlet-name>
<url-pattem>/servlet/Sample</url-pattem>
</servlet-mapping>
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.
https://ecomputernotes.com:8080/MyApp/servlet/Sample
or
http:/1127.0.0.1:8080/MyApp/servlet/Sample
If separate HTML file is used for the HTML code, enter the following URL in the address bar.
https://ecomputernotes.com:8080/MyApp/Sample.html
or
http:/1127.0.0.1:8080/MyApp/Sample.html
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.