Thursday, August 18, 2011

How to install and configure Tomcat for Windows(tm)

How to install and configure Tomcat for Windows(tm)

September 17th, 2008 by uCertify Leave a reply » To install and configure Tomcat, you will have to take the following steps:
  1. Download JDK 1.5 from Sun(tm) site at http://java.sun.com/javase/downloads/index_jdk5.jsp
  2. Install JDK to C:Program FilesJavajdk1.5.0_08
  3. Download Tomcat 5 from http://tomcat.apache.org/download-55.cgi
  4. Install Tomcat on C:Program FilesApache Software FoundationTomcat 5.0
  5. Set the port number to 8080, which is the default port. Set the name as admin and password as desired. The password column can even be left blank.
  6. Set the environment variables as follows:
    • Right-click My Computer and select properties.
    • Click the Advanced tab button.
    • Click Environment Variables.
    • Select New to set the properties as follows:
      1. Classpath: In the variable name field write classpath, and in the variable value field write the location of servlet-api.jar file as C:Program FilesApache Software FoundationTomcat 5.0commonlibservlet-api.jar
      2. Path: In the variable name field write PATH, and in the variable value field write the location of JDK installation as C:Program FilesJavajdk1.5.0_08bin;.;
      3. CATALINA_HOME: In the variable name field write CATALINA_HOME, and in the variable value field write the location of Tomcat installation as C:Program FilesApache Software FoundationTomcat 5.0.
      4. JAVA_HOME: In the variable name field write JAVA_HOME, and in the variable value field write the location of JDK installation excluding bin as C:Program FilesJavajdk1.5.0_08.
  7. Write a small Servlet program to test the installation as follows: import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
    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>”);
          out.println(“<body>”);
          out.println(“This is a Servlet test!”);
          out.println(“</body>”);
          out.println(“</html>”);
       }
    }
  8. Compile the Servlet file and put the class file in webapps/servlets/WEB-INF/classes directory inside the Tomcat directory.
  9. Open a new textpad file and write the following code: <web-app>
       <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>
  10. Save the above textpad file as web.xml in the webapps/servlets/WEB-INF directory.
  11. Go to C:Program FilesTomcat 5.0bin directory and start tomcat by double clicking on the startup batch file.
  12. Open a new instance of Internet Explorer and type the following URL in the Address bar:
    http://localhost:8080/servlets/test
Note: Whenever any change is made to the servlet, it should be compiled again. In such a case, either restart the Tomcat server or redeploy the application by going to http://localhost:8080/manager/html.

Note by Rehan:
it was not being compiled. so I changed the name from servlet-api.jar to servlet.jar and made changes accordingly in classpath. then it started working fine.

No comments:

Post a Comment