Monday, March 12, 2012

Adding a simple servlet within the example-apps of Tomcat


Adding a simple servlet within the example-apps of Tomcat:

set CLASSPATH=%CLASSPATH%;C:\Extra\Tomcat 7.0\lib\servlet-api.jar;.

Code: ( File Name: HelloWorld.java)


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


public class HelloWorld extends HttpServlet {


  public void doGet(HttpServletRequest req, HttpServletResponse res)
                               throws ServletException, IOException {


    res.setContentType("text/html");
    PrintWriter out = res.getWriter();


    out.println("");
    out.println("Hello World");
    out.println("");
    out.println("Hello World");
    out.println("");
  }
}

Copy the class file @ C:\Extra\Tomcat 7.0\webapps\examples\WEB-INF\classes

Then


C:\Extra\Tomcat 7.0\webapps\examples\WEB-INF\web.xml

 Add the following entries




       Access newly deployed servled as:

http://localhost:8080/examples/servlets/servlet/HelloWorld

No comments:

Post a Comment