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 corrigés java orienté objet

abdelouafiSep 21, 2016

    1. abdelouafi

      abdelouafi Administrator Staff Member

      Messages:
      267
      Likes Received:
      8
      Trophy Points:
      18
      Joined
      Sep 13, 2016
      TP Servlet :
      1. Le code source de la classe Personne.java (dans le package ensa):

      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;
      }
      
      
      }
      
      2. Le code source de la classe PremiereServlet:
      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");
           
          }
      
      }
      
      3- Le code source de la classe SecondServlet.java :
      Code:
      package ma.ac.ensa;
      
      import java.io.IOException;
      import java.io.PrintWriter;
      
      import javax.servlet.ServletException;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
      import javax.servlet.http.HttpSession;
      
      /**
      * Servlet implementation class SecondServlet
      */
      public class SecondServlet extends HttpServlet {
          private static final long serialVersionUID = 1L;
           
          /**
           * @see HttpServlet#HttpServlet()
           */
          public SecondServlet() {
              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
          }
      
          /**
           * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
           */
          protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
              HttpSession session=request.getSession();
              Personne p=(Personne)session.getAttribute("personne");
              p.setAdresse(request.getParameter("adresse"));
              p.setTel(request.getParameter("tel"));
              p.setAge(Integer.parseInt(request.getParameter("age")));
              session.setAttribute("personne", p);
           
              PrintWriter out=response.getWriter();
              out.println("<html><head><title>Confirmation.html</title>" +
                      "</head><body>" +
                      "<form action=\"/SerieServlet/Servlet3\" method=\"post\" >"+
                      "<table>" +
                      "<tr><td> nom : " +p.getNom()+    "</td></tr>" +
                      "<tr><td> prenom : " +p.getPrenom()+    "</td></tr>" +
                      "<tr><td> adresse : " +p.getAdresse()+    "</td></tr>" +
                      "<tr><td> tel : " +p.getTel()+    "</td></tr>" +
                      "<tr><td> age : " +p.getAge()+    "</td></tr>" +
                      "<tr><td> <input type =\"submit\" value=\"Confirmer\" /></td></tr>" +
                              "</table>" +
                              "</form>");
           
          }
      
      }
      

      4- Le code source de la classe Servlet3.java :
      Code:
      package ma.ac.ensa;
      
      import java.io.IOException;
      import java.sql.Connection;
      import java.sql.DriverManager;
      import java.sql.SQLException;
      import java.sql.Statement;
      
      import javax.servlet.ServletException;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
      import javax.servlet.http.HttpSession;
      
      /**
      * Servlet implementation class Servlet3
      */
      public class Servlet3 extends HttpServlet {
          private static final long serialVersionUID = 1L;
            
          /**
           * @see HttpServlet#HttpServlet()
           */
          public Servlet3() {
              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
          }
      
          /**
           * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
           */
          protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
              Connection conn=null;
              HttpSession session=request.getSession();
              Personne p=(Personne)session.getAttribute("personne");
              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="INSERT INTO PERSONNE VALUES('"+p.getNom()+"','"+p.getPrenom()+
                          "','"+p.getAdresse()+"','"+p.getTel()+"',"+p.getAge()+");";
                  System.out.println(req);
                  st.executeUpdate(req);
                  conn.close();
              }catch(SQLException ex){ex.printStackTrace();}
            
            
            
          }
      
      }
      
       

      Attached Files:

      Last edited: Jan 16, 2017
      Loading...
Tags:

Share This Page

Share