1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

TP Servlet Java : exercices Servelet

abdelouafiSep 21, 2016

    1. abdelouafi

      abdelouafi Administrator Staff Member

      Messages:
      267
      Likes Received:
      8
      Trophy Points:
      18
      Joined
      Sep 13, 2016
      exercices Servelet:
      1- Le code source de la classe Employe.java :
      Code:
      package ma.ac.ensa;
      
      public class Employer {
      private String nom,prenom;
      
      public String getNom() {
          return nom;
      }
      
      public void setNom(String nom) {
          this.nom = nom;
      }
      
      public String getPrenom() {
          return prenom;
      }
      
      public void setPrenom(String prenom) {
          this.prenom = prenom;
      }
      
      }
      
      2- Le code source de la classe Login.java :
      Code:
      package ma.ac.ensa;
      
      public class Login {
      private String nom,prenom,date;
      
      public String getNom() {
          return nom;
      }
      
      public void setNom(String nom) {
          this.nom = nom;
      }
      
      public String getPrenom() {
          return prenom;
      }
      
      public void setPrenom(String prenom) {
          this.prenom = prenom;
      }
      
      public String getDate() {
          return date;
      }
      
      public void setDate(String date) {
          this.date = date;
      }
      
      }
      
      3- Le code source de la classe MaHttpJspBase.java:
      Code:
      package ma.ac.ensa;
      
      import java.io.IOException;
      import java.sql.Connection;
      import java.sql.DriverManager;
      import java.sql.ResultSet;
      import java.sql.SQLException;
      import java.sql.Statement;
      
      import javax.servlet.ServletException;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
      
      import org.apache.jasper.runtime.HttpJspBase;
      
      public class MaHttpJspBase extends HttpJspBase {
      public int nbr;
          @Override
          public void _jspService(HttpServletRequest arg0, HttpServletResponse arg1)
                  throws ServletException, IOException {
              // TODO Auto-generated method stub
      
          }
       
          public void _jspInit(){
              Connection conn=null;
                  System.out.println("initializing *************************");
               
                  try{Class.forName("com.mysql.jdbc.Driver");
                   
                  }catch(ClassNotFoundException ex){
                      ex.printStackTrace();
                  }
                  try{
                      conn=DriverManager.getConnection("jdbc:mysql://localhost/mabase", "root", "00001111");
                      Statement st=conn.createStatement();
                      String req="SELECT * FROM compteur ;";
                      System.out.println(req);
                      ResultSet rs=st.executeQuery(req);
                      rs.next();
                      nbr=rs.getInt("value");
                      conn.close();
                  }catch(SQLException ex){ex.printStackTrace();}
               
               
               
               
             
              }
      
              public void _jspDestroy() {
                  System.out.println("initializing *************************");
                  Connection conn=null;
               
               
                  try{Class.forName("com.mysql.jdbc.Driver");
                   
                  }catch(ClassNotFoundException ex){
                      ex.printStackTrace();
                  }
                  try{
                      conn=DriverManager.getConnection("jdbc:mysql://localhost/mabase", "root", "00001111");
                      Statement st=conn.createStatement();
                      String req="UPDATE compteur SET value="+nbr+" where id=1);";
                      System.out.println(req);
                      st.executeUpdate(req);
                      conn.close();
                  }catch(SQLException ex){ex.printStackTrace();}
               
               
               
               
              }
      
      
      }
      
      4- Le code source de la classe Personne.java
      Code:
      package ma.ac.ensa;
      
      public class Personne {
      private String nom,prenom,adresse,tel;
      private int age;
      public String getNom() {
          return nom;
      }
      public void setNom(String nom) {
          this.nom = nom;
      }
      public String getPrenom() {
          return prenom;
      }
      public void setPrenom(String prenom) {
          this.prenom = prenom;
      }
      public String getAdresse() {
          return adresse;
      }
      public void setAdresse(String adresse) {
          this.adresse = adresse;
      }
      public String getTel() {
          return tel;
      }
      public void setTel(String tel) {
          this.tel = tel;
      }
      public int getAge() {
          return age;
      }
      public void setAge(int age) {
          this.age = age;
      }
      public Personne(String nom, String prenom, String adresse, String tel, int age) {
          super();
          this.nom = nom;
          this.prenom = prenom;
          this.adresse = adresse;
          this.tel = tel;
          this.age = age;
      }
      
      
      }
      

      5- le code source de la classe PrmiereServlet.java
      Code:
      package ma.ac.ensa;
      
      import java.io.IOException;
      
      import javax.servlet.ServletException;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
      import javax.servlet.http.HttpSession;
      
      public class PremiereServlet extends HttpServlet{
         
          @Override
          protected void doPost(HttpServletRequest req, HttpServletResponse resp)
                  throws ServletException, IOException {
              HttpSession session=req.getSession();
              Personne p=new Personne();
              p.setNom(req.getParameter("nom"));
              p.setPrenom(req.getParameter("prenom"));
              session.setAttribute("personne", p);
              resp.sendRedirect("etape2.html");
             
          }
      
      }
      
       

      Attached Files:

      Last edited: Jan 16, 2017
      Loading...

Share This Page

Share