Configuring And Running Servlet Program into tomcat 6

Configuring And Running Servlet Program into tomcat 6


Posted in : Servlet Posted on : October 30, 2010 at 5:52 PM Comments : [ 0 ]

In this tutorial you will learn how to configure and run servlet in tomcat 6

Configuring And Running Servlet Program into tomcat 6

Follow the steps to compile and run servlet application

  1. At first you need to install java ( jdk1.5 or above version ) .
  2. Set the JAVA_HOME as- go to the Start ->Control Panel ->Performance And Maintenance ->System ->Advance -> Environment Variable -> New -> Then write the variable name "JAVA_HOME" and in Variable Value value write the full path of java install directory.
  3. Then  download the tomcat 6 and then un-zip the zip the tomcat into the C:\ directory.
  4. Then go to the apache-tomcat-6.0.29\webapps\ROOT\WEB-INF directory make a new folder name it "classes". Remember that the name of classes directory must be written in small latter.
  5. Then write the following code in note pad or other editor you prefer and save it as TestServlet.java and save it to the apache-tomcat-6.0.29\webapps\ROOT\WEB-INF\classes directory-
    	import java.io.IOException;
    	import java.io.PrintWriter;
    
    	import javax.servlet.ServletException;
    	import javax.servlet.http.HttpServlet;
    	import javax.servlet.http.HttpServletRequest;
    	import 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("");
    	    }
    	}
  6. Then for compilation open your command prompt and by command prompt go to the apache-tomcat-6.0.29\webapps\ROOT\WEB-INF\classes and type javac TestServlet.java -classpath "C:\apache-tomcat-6.0.29\lib\servlet-api.jar" and then press enter.
  7. Now configure your servlet. To configure follow the steps go to the C:\apache-tomcat-6.0.29\webapps\ROOT\WEB-INF and open web.xml file in edit mode
  8. 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>	  
    Write your code here .......................
    </web-app> 

    Then write the fillowing mapping code

    <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> 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 browser
  9. 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
  10. To run your servlet write the following URL in your browser  http://localhost:8080/test and press enter then the output appears like this 
Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics