Thursday, June 7, 2012

Servlet 3.0 Program on Tomcat 7.0

Here, I have developed sample project with version 3 servlet on Tomcat server.

Folder Structure:
Tomcat -- > webapps -- > sample -- > WEB-INF -- > classes
Now just write a servlet class using servlet 3.0 standards like


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyServlet extends HttpServlet {
      private static final long serialVersionUID = 1L;

      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            PrintWriter out = response.getWriter();
            out.print("Welcome to Servlet World!!!");

Optional :Copy the following web.xml file into WEB-INF folder. It is optional as we are not using application level settings.

<xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" id="WebApp_ID" version="3.0">

Compile the and copy the class files in classes folder.
Now start the tomcat server with startup.bat located in tomcat/bin folder and wait for startup.
Now enter the following address to see the result of our servlet.

No comments:

Post a Comment