Example: 1. Greeting Application (index.html, web.xml, GreetingServlet.java)

Servlet Example
Servlet Example1

Directory Structure for web Application Greeting

Servlet Example2

Steps from Development to deployment:
Step: 1 Create the following structure in your application folder (say D:application   )

Servlet Example3

Step: 2 Write code for Greeting servlet and save copy the source in src folder (GreetingServlet.java).
Step: 3 Write code for welcome file (index.html) and save / copy it into folder web.
Step: 4 compile the java files so that the package hierarchy is created in the WEB-INF/classes folder.
            javac –d D:applicationservletswebWEB-INFclassesGreetingServlet.java
            Note: - while compiling, make sure that either jsdk.jar or j2ee.jar or servlet-api.jar is in the
Classpath. (C:j2sdklibj2ee.jar on PU server).
Step: 5 Create folders servlets in the webapps folder (C:Tomcat 5.0webapps)
Step: 6 Copy the contents of the web folder (d:applicationservletsweb) into the folder servlets created in
Step: 7 Stop and Restart the tomcat server to refresh the web application.

Servlet Example4

Accessing Greeting Application
Step: 1 http://localhost:8080/servlets or http://localhost:8080/servlets/index.html
            The following will be displayed in the browser:
Step: 2 Fill the Name and E-Mail fields.
Step: 3 Click on the submit button.
Step: 4 GreetingServlet will be invoked and will give the following response.

Directly Accessing The GreetingServlet
            Type the following URL in the address bar: http://localhost:8080/servlet/servlets/org.test.GreetingServlet
            And observe the response.
            Note – uncomment InvokerServlet and it’s mapping from tomcat/conf/web.xml
            (Response will indicate that GET method is not support.)

Accessing Using GET method for index.html
Step: 1 Modify the index.html file:
            Change method = “POST” to method = “GET”
Step: 2 Access index.html file.
Step: 3 Click on Submit button after filling name and email.
Step: 4 observe the response (Response will indicate that GET method is not supported).

Accessing GreetingServlet after adding doGet( ) method
Step: 1 Add doGet( ) method in the GreetingServlet.Simply copy the code of doPost( ) method without
            any change.
Step: 2 Recompile the GreetingServlet.java
Step: 3 Copy the class file into folder C:Tomcat 5.0webappsservletsWEB-INFclasses
Step: 4 Stop and Restart the greeting Application in tomcat web server.
Step: 5 Access the GreetingServlet directly and through index.html file and observe the response. (The
            Servlet response will be displayed in both the cases.)

Deploying Using .war file
Step: 1 to Step: 5 will remain same.
Step: 6 move to web (D:applicationservletsweb) folder and give the following command:
            jar -cf servlets.war *
            See that the file greeting.war is created in the web folder.
Step: 7 Copy servlets.war file into webapps folder of tomcat. Tomcat supports not deployment (which
            Is on by default) so the war will be extracted and web application “servlets” will be deployed.
            Confirm this by checking that the folder “servlets” is created and by verifying the content of
            This folder. Or Access tomcat manager through the URL: http://localhost:8080/manager/html
            and then deploy war file.