Sunday, January 13, 2013

Steps to create a simple servlet

The article assumes, you have JDK 5.0/6.0 and Tomcat 6.0.

The following are the steps to be taken by you to create a simple servlet.

Download Tomcat 6.0 from http://tomcat.apache.org
Install Tomcat 6.0 into c:\ApacheTomcat6.0
Go to webapps directory of Tomcat and create the following directory structure for demo application. Any directory placed with required structure of Java EE web application is treated as a web application by Tomcat.
demo
WEB-INF
classes
TestServlet.java
web.xml

Creating Servlet Source Code

The following is the code for TestServlet.java. It simply sends a message to browser.

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class TestServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><body><h1>Test Servlet </h1></body></html>");
}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
doGet(request, response);
}
}



Create web.xml as follows. It is better you copy web.xml from some other application in Tomcat webapps directory instead of typing it from scratch. Make sure it is placed in WEB-INF (in uppercase) folder of demo application with the following content.

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>

</web-app>

TestServlet is associated with url pattern /test in web.xml. From the client, we have to make request for url pattern /test.
Compiling Servlet

Go to classes directory, where .java file is placed as enter the following commands at the command prompt to compile the servlet.
path=d:\jdk1.6.0\bin
set classpath=.;c:\apachetomcat6.0\lib\servlet-api.jar
javac TestServlet.java

Make sure you change JDK and Tomcat directories according to your installation.

Set path to the directory where JDK is installed. Servlet-api.jar contains API related to servlet like HttpServlet, HttpServletRequest etc., so it must be placed in the classpath. Compile the servlet (.java) to create .class file.

Starting Tomcat and Running Servlet

Start Tomcat by taking the follwing steps from BIN directory of Tomcat.
set java_home=d:\jdk1.6.0
startup

Once, tomcat is successfully started, go to browser and enterer the following URL.
http://localhost:8080/demo/test
You must see the output as follows.

[IMG]

No comments:

Post a Comment

Thank you share the knowledge U have with in U.