Correction Exercices 01 les servlets Série 01

Correction Exercices 01 les servlets Série 01







  1. Exercice 01

    1. Énoncé
    2. Solution





        • 
          
          import java.io.IOException;
          import java.io.PrintWriter;
          import java.text.DateFormat;
          import java.text.SimpleDateFormat;
          import java.util.Date;
          import javax.servlet.ServletException;
          import javax.servlet.annotation.WebServlet;
          import javax.servlet.http.HttpServlet;
          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;
          
          /**
           * Servlet implementation class BienvenuePremierServlet
           */
          @WebServlet("/BienvenuePremierServlet")
          public class BienvenuePremierServlet extends HttpServlet {
          	private static final long serialVersionUID = 1L;
          	private int compteur = 0;
              /**
               * Default constructor. 
               */
              public BienvenuePremierServlet() {
                  // TODO Auto-generated constructor stub
              }
          
          	/**
          	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
          	 */
          	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          		
          		// TODO Auto-generated method stub
          		//response.getWriter().append("Served at: ").append(request.getContextPath());
          		response.setContentType("text/html") ;	
          		PrintWriter out = response.getWriter() ;
          		out.println("<html>") ;
          		out.println("<head>") ;
          		out.println("<title>BienvenueServlet</title>") ;
          		out.println("</head>") ;
          		out.println("<body>") ;
          		out.println("<h1>Bienvenue au cours de programmation web avec java</h1>") ;
          		DateFormat formatDate = new SimpleDateFormat("dd/MM/yyyy - HH'h'mm");
          		out.println(formatDate.format(new Date()));
          		compteur++;
                  out.println("</br><b>Cette Servlet a été visitée " + compteur + " fois.</p>");
          
          		out.println("</body>") ;
          		out.println("</html>") ;  
          	}
          
          	/**
          	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
          	 */
          	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          		// TODO Auto-generated method stub
          		doGet(request, response);
          	}
          
          }
          



Abonnez vous à notre chaîne YouTube gratuitement