Servlet Wizard

Lire cette page en français Go back

This wizard will generate a basic servlet class.

Warning: do not forget to add in your classpath the servlet.jar. In order to use this plugin you should install the Tomcat plugin (or a similar plugin).
During the generation of the class you will have to provide these information:
  • The class name of your servlet
  • Methods that should be implemented: doGet, doPost, init and destroy, doPut, doDelete, getServletInfo

  • On the second window you can select your mapping options
  • Servlet key name used in the web.xml file.
  • URL where your servlet can be found
  • Place to find/create the web.xml file


  • This is an example of a generated servlet:
    import java.io.*;
    
    import javax.servlet.*;
    import javax.servlet.http.*;
    import javax.servlet.http.HttpServlet;
    
    public class MyServlet extends HttpServlet {
    
      /**
       * Constructor for MyServlet.
       */
      public MyServlet() {
        super();
      }
    
      /**
       * The doGet method of the servlet. <br>
       *
       * This method is called when a form has its tag value method equals to get.
       * 
       * @param request the request send by the client to the server
       * @param response the response send by the server to the client
       * @throws ServletException if an error occurred
       * @throws IOException if an error occurred
       */
      public void doGet(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
        out.println("<HTML>");
        out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
        out.println("  <BODY>");
        out.print("    This is ");
        out.print(this.getClass());
        out.println(", using the GET method");
        out.println("  </BODY>");
        out.println("</HTML>");
        out.flush();
        out.close();
      }
    
      /**
       * The doPost method of the servlet. <br>
       *
       * This method is called when a form has its tag value method equals to post.
       * 
       * @param request the request send by the client to the server
       * @param response the response send by the server to the client
       * @throws ServletException if an error occurred
       * @throws IOException if an error occurred
       */
      public void doPost(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
        out.println("<HTML>");
        out.println("  <HEAD<TITLE>A Servlet</TITLE></HEAD>");
        out.println("  <BODY>");
        out.print("    This is ");
        out.print(this.getClass());
        out.println(", using the POST method");
        out.println("  </BODY>");
        out.println("</HTML>");
        out.flush();
        out.close();
      }
    
      /**
       * The doPut method of the servlet. <br>
       *
       * This method is called when a HTTP put request is received.
       * 
       * @param request the request send by the client to the server
       * @param response the response send by the server to the client
       * @throws ServletException if an error occurred
       * @throws IOException if an error occurred
       */
      public void doPut(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
        // Put your code here
      }
    
      /**
       * The doDelete method of the servlet. <br>
       *
       * This method is called when a HTTP delete request is received.
       * 
       * @param request the request send by the client to the server
       * @param response the response send by the server to the client
       * @throws ServletException if an error occurred
       * @throws IOException if an error occurred
       */
      public void doDelete(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
        // Put your code here
      }
    
      /**
       * Initilaisation of the servlet. <br>
       *
       * @throws ServletException if an error occure
       */
      public void init() throws ServletException {
        // Put your code here
      }
    
      /**
       * Destruction of the servlet. <br>
       */
      public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
      }
    
      /**
       * Returns information about the servlet, such as 
       * author, version, and copyright. 
       *
       * @return String information about this servlet
       */
      public String getServletInfo() {
        return "This is my default servlet created by Eclipse";
      }
    }
      

    This is an example of web.xml generated :
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
    <web-appgt;
    
      <display-name>PutHereTheNameOfYourWebApp</display-name>
    
      <description>This a description of my web app made by Eclipse</description>
    
      <servlet>
        <servlet-name>MyServlet</servlet-name>
        <display-name>This is the display name of my J2EE component</display-name>
        <description>This is the description of my J2EE component</description>
        <servlet-class>com.bg.MyServlet</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>MyServlet</servlet-name>
        <url-pattern>/servlet/MyServlet</url-pattern>
      </servlet-mapping>
      <welcome-file-list>
        <welcome-file>/servlet/MyServlet</welcome-file>
      </welcome-file-list>
    
    </web-app>
      


    Comments, ideas and bugs mail me at : Contact