Wizard pour la création d'une Servlet

Read this page in english Go back

Ce Wizard génère une Servlet standard de base.

Attention : Ne pas oublier de placer dans le classpath de l'application, les fichiers jar adaptés, c.a.d ceux contenant les packages javax.servlet. Ses fichiers Jars se trouvent dans le répertoire de votre serveur d'application. Si vous utilisez Tomcat, je vous conseil fortement d'utiliser le plugin Tomcat.
Durant la phase de lancement du plugin, les informations suivantes vous sont demandées :
  • Quel est le nom de la nouvelle Servlet
  • Quelles sont les méthodes que vous désirez générer : doGet, doPost, init et destroy, doPut, doDelete, getServletInfo

  • Sur une seconde fenêtre vous devez choisir les options de mapping de votre Servlet :
  • Le nom de la servlet qui est utilisé dans le fichier web.xml. Ce nom n'a aucun rapport avec le nom de la classe, c'est un nom de référence.
  • L'URL qui donne accès à votre servlet. C'est via cette URL que vous pouvez accéder à votre servlet.
  • Vous devez préciser où se trouve le fichier web.xml de votre application web. Si ce dernier n'existe pas, il sera créé à l'endroit indiqué.


  • Voici un exemple d'une classe Java générée par le plugin :
    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";
      }
    }
      

    Voici un exemple de fichier web.xml généré :
                <?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>
      
    Remarque :
    Evolution future :

    Pour toutes remarques, commentaires idées, bugs : Contact