Running Servlets using Tomcat

For: Beginners (those who are new to Java Programming)

This tutorial will tell you how to Install tomcat and how to run servlet using Tomcat. However, you can use easier tools like Eclipse or Netbeans or IBM RAD to deploy the Servlets.

Installing Tomcat in Windows 7

    1. Download Tomcat 7.0 from http://tomcat.apache.org/download-70.cgi
    2. Just double Click and install (The default location will be in C:/program Files/Tomcat 7.0/). I have selected C:/tomcat (This path will be very easy to remember and no PATH settings issues will be there
    3. Once installed, to run jsp or servlets, there are some set of jar files are given in the library. Set PATH for those jar files.
    4. To set the path, go to Control Panel –> System –> Advanced System Settings –> Environmental Variables –> Edit PATH in the System Variables (Just update the C:\tomcat\lib\servlet-api.jar) for running servlets. Similarly for jsp also.
    5. Once done open the browser, http://localhost:8080/ will take you to the Tomcat Home Page (if successful, then your installation is succeeded)

Simple Servlet

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

public class HelloServlet extends HttpServlet
{
    public void init() throws ServletException
    {}
    public void service(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException
    {
        res.setContentType("text/plain");
        PrintWriter pw = res.getWriter();
        pw.println("<html><body>");
        pw.println("<h1> Hello World, I am a Servlet </h1>");
        pw.println("</body></html>");
    }
    public void destroy()
    {}

}

  1. Name this program as HelloServlet.java (as the class is public)
  2. Save this file in the folder C:/Tomcat/webapps/examples/WEB-INF/classes (In my  case)
  3. Compile this program through terminal or using any IDE like Eclipse or NetBeans
  4. Once Compiled, tell about this Servlet in web.xml File which is located in the C:/tomcat/webapps/examples/WEB-INF (the servlets information has to be provided to this file and there is a format for us to specify the name of the servlets)
  5. Here is the syntax to add the entry in the web.xml file

   <servlet>
      <servlet-name>HelloServlet</servlet-name>
      <servlet-class>HelloServlet</servlet-class>
    </servlet>

<servlet-mapping>
        <servlet-name>HelloServlet</servlet-name>
        <url-pattern>/HelloServlet</url-pattern>
    </servlet-mapping>

In the servlet-mapping tag, url-pattern see there is a /HelloServlet (all servlet url mapping should start with a /)

Once done, Save this file and run the servlet in the browser using this link http://localhost:8080/examples/HelloServlet (if the desired output is not there, try restart your apache server)

Comments

  1. [...] Running Servlets using Tomcat » HTML Form Processing using Servlets For : Beginners (Those with basic knowledge on Java [...]

    ReplyDelete
  2. [...] the file and Make an entry in the web.xml file and run the HTML file using the link [...]

    ReplyDelete

Post a Comment

Popular Posts