Vous êtes sur la page 1sur 3

Configuring And Running Servlet Program into tomcat 6

Follow the steps to compile and run servlet application


1. Go to the c:\Apache Software Foundation\tomcat 6\webapps\ROOT\WEBINF directory make a new folder name it "classes". Remember that the name of classes directory must be written in small latter. 2. Then write the following code in note pad or other editor you prefer and save it as TestServlet.java and save it to the copy/save into c:\Program files\java\jdk1.6.0\bin directory. If you go to compile in javand util package. So download and copy into into c:\Program files\java\jdk1.6.0\bin directory
import import import import import import java.io.IOException; java.io.PrintWriter; javax.servlet.ServletException; javax.servlet.http.HttpServlet; javax.servlet.http.HttpServletRequest; javax.servlet.http.HttpServletResponse;

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(""); out.println(""); out.println("<h1>This is my Fisrt Servlet</h1>"); out.println(""); out.println(""); }

3. Then for compilation open your command prompt and by command prompt goes to the c:\Program files\java\jdk1.6.0\bin\javac TestServlet.java and then press enter. It creates TestServlet.class file and copy it into c:\Apache Software Foundation\tomcat 6\webapps\ROOT\WEB-INF\classes. 4. Now configure your servlet. To configure follow the steps go to the c:\Apache Software Foundation\tomcat 6\webapps\ROOT\WEB-INF and open web.xml file in edit mode write the following block of code within
<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"> <display-name>Welcome to Tomcat</display-name> <description> Welcome to Tomcat </description>

<servlet> <servlet-name>Test</servlet-name> <servlet-class>TestServlet</servlet-class> </servlet> <!-- servlet mapping --> <servlet-mapping> <servlet-name>Test</servlet-name> <url-pattern>/test</url-pattern> </servlet-mapping> </web-app>

Here Test within <servlet-name>Test</servlet-name> is name of your servlet. TestServlet is your servlet class and /test is your url pattern or your servlet name by which you call this servlet on browse 5.Then Start the tomcat, go to the apache-tomcat-6.0.29\bin and double click on the startup. if a window appears like this the your tomcat have been started. To check whether you tomcat started or not open your web browser and type URL http://localhost:8080 and press enter then a windows appears like this

To run your servlet write the following URL in your browser http://localhost:8080/test and press enter then the output appears like this

BY P.sonaiyakarthick B.E.,DCSE,D.H/W.NET,

Vous aimerez peut-être aussi