Correction exercice 05 serie 01 servlet

Correction exercice 05 serie 01 servlet

  1. Exercice 05

    1. Énoncé
    2. Solution
      • import java.io.IOException;
        import java.io.PrintWriter;
        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 milleNombresPremiers
         */
        @WebServlet("/milleNombresPremiers")
        public class milleNombresPremiers extends HttpServlet {
        	private static final long serialVersionUID = 1L;
        	boolean isPrimes = true;
        	int cellule=1;
            /**
             * @see HttpServlet#HttpServlet()
             */
            public milleNombresPremiers() {
                super();
                // 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.setContentType("text/html") ;	
        		PrintWriter out = response.getWriter() ;
        		out.println("<html>") ;
        		out.println("<head>") ;
        		out.println("<title>milleNombresPremiers</title>") ;
        		out.println("</head>") ;
        		out.println("<body><center>") ;
        		out.println("
        <h1>Bienvenue au cours de programmation web avec java</h1>") ;
        		out.println("<table border='1' width='60%'>");
        		for(int i =2; i <= 1000; i++){
        			isPrimes = true;
        			for(int j = 2; j <= i; j++){
        				if((i%j == 0)&& (j!=i)){
        					isPrimes = false;
        					break;
        				}
        			}
        			 if(isPrimes){			    	
        				if(cellule==1)
        				{
        					out.println("<tr>");
        					out.println("<td align='center'>"
        +i+"</td>");
        				}
        				if(cellule==10)
        				{									
        					out.println("<td align='center'>"
        +i+"</td>");
        					out.println("</tr>");
        					cellule=0;					
        				}
        				if(cellule!=10 && cellule!=1 && cellule!=0) {
        					out.println("<td align='center'>"
        +i+"</td>");
        				}						
        				cellule++;
        			 }
        
        			}
        		out.println("</table></center>");
        
        		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