tp1 Servlet
tp1 Servlet
tp1 Servlet
SERV1: : pour afficher un formulaire permettant de saisir infos d’un nouvel employé. Une fois le
formulaire rempli et soumis, les données sont envoyées à la servlet ajout.
out.println("Nom");
out.println("<input type='text' name='n'><br><br>");
out.println("Salaire");
out.println("<input type='text' name='s'><br><br>");
out.println("Prime");
out.println("<input type='text' name='p'><br><br>");
@WebServlet(urlPatterns = {"/liste"})
public class liste extends HttpServlet {
public static int mate;
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
//les etapes de connexion
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>liste employes</title>");
out.println("</head>");
out.println("<body>");
out.println("<center>");
out.println("<h1>Liste employés</h1>");
out.println("<form action='serv1' method='post'>");
out.println("<table border='2'>");
out.println("<tr>");
out.println("<td>");
out.println("Matricule");
out.println("</td>");
out.println("<td>");
out.println("Nom");
out.println("</td>");
out.println("<td>");
out.println("Salaire");
out.println("</td>");
out.println("<td>");
out.println("prime");
out.println("</td>");
out.println("<td>");
out.println("Action 1");
out.println("</td>");
out.println("<td>");
out.println("Action 2");
out.println("</td>");
out.println("</tr>");
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection co=null;
co=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","u","p");
Statement s1=co.createStatement();
String req="select * from personne";
ResultSet res=s1.executeQuery(req);
while(res.next())
{
mate=res.getInt("mat");
out.println("<tr>");
out.println("<td>");
out.println(""+res.getInt("mat"));
out.println("</td>");
out.println("<td>");
out.println(""+res.getString("nom"));
out.println("</td>");
out.println("<td>");
out.println(""+res.getFloat("sal"));
out.println("</td>");
out.println("<td>");
out.println(""+res.getFloat("pr"));
out.println("</td>");
out.println("<td><a href='modif?mat="+mate+"'>Modification</a></td>");
out.println("<td><a href='supp?mat="+mate+"'>Suppression</a></td>");
out.println("</tr>");
}
out.println("</table><br><br>");
@WebServlet(urlPatterns = {"/ajout"})
public class ajout extends HttpServlet {
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
int m=Integer.parseInt(request.getParameter("m"));
String n=request.getParameter("n");
float s = Float.valueOf(request.getParameter("s"));
float p = Float.valueOf(request.getParameter("p"));
//les etapes de connexion
try
{ //chargement pilote
Class.forName("oracle.jdbc.driver.OracleDriver");
//url
Connection con=null;
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","u","");
//creation espace memoire
Statement s1=con.createStatement();
//declaration requete
String req="insert into personne values("+m+",'"+n+"',"+s+","+p+")";
@WebServlet(urlPatterns = {"/Psupp"})
public class Psupp extends HttpServlet {
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
try
{ //chargement pilote
Class.forName("oracle.jdbc.driver.OracleDriver");
//url
Connection con=null;
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","u","");
//creation espace memoire
Statement s1=con.createStatement();
//declaration requete
String req="delete from personne where mat="+supp.mate;
@WebServlet(urlPatterns = {"/modif"})
public class modif extends HttpServlet {
public static int mate;
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
int mat=Integer.parseInt(request.getParameter("mat"));
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection co=null;
co=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","u","p");
Statement s1=co.createStatement();
String req="select * from personne where mat="+mat;
ResultSet res=s1.executeQuery(req);
while(res.next())
{
mate=mat;
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet modif</title>");
out.println("</head>");
out.println("<body>");
out.println("<form action ='Pmodif' method='post'>");
out.println("<center>");
out.println("Nom");
out.println("<input type='text' name='nom'
value='"+res.getString("nom")+"'><br><br>");
out.println("Salaire");
out.println("<input type='text' name='sal' value =
"+res.getFloat("sal")+"><br><br>");
out.println("Prime");
out.println("<input type='text' name='pr'
value="+res.getFloat("pr")+"><br><br>");
out.println("<input type='submit' value='Update'>");
out.println("</center>");
out.println("</form>");
out.println("</body>");
out.println("</html>");
}
}
catch(ClassNotFoundException ee)
{
out.println("err pilote");
}
catch(SQLException gg)
{
out.println("err de requete");
}
}
}//@overide *3
}
supp : affiche les informations d’un employé spécifique (sélectionné par le matricule passé en
paramètre) avant de le supprimer de la base de données. L’utilisateur est ensuite redirigé vers la
servlet Psupp.
@WebServlet(urlPatterns = {"/supp"})
public class supp extends HttpServlet {
public static int mate;
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
int mat=Integer.parseInt(request.getParameter("mat"));
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection co=null;
co=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","u","p");
Statement s1=co.createStatement();
String req="select * from personne where mat="+mat;
ResultSet res=s1.executeQuery(req);
while(res.next())
{
mate=mat;
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet supp</title>");
out.println("</head>");
out.println("<body>");
out.println("<form action ='Psupp' method='post'>");
out.println("<center>");
out.println("Nom");
out.println(""+res.getString("nom")+"");
out.println("<br><br>");
out.println("Salaire");
out.println(""+res.getFloat("sal")+"");
out.println("<br><br>");
out.println("Prime");
out.println(""+res.getFloat("pr")+"");
out.println("<br><br>");
@WebServlet(urlPatterns = {"/modif"})
public class modif extends HttpServlet {
public static int mate;
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
int mat=Integer.parseInt(request.getParameter("mat"));
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection co=null;
co=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","u","p");
Statement s1=co.createStatement();
String req="select * from personne where mat="+mat;
ResultSet res=s1.executeQuery(req);
while(res.next())
{
mate=mat;
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet modif</title>");
out.println("</head>");
out.println("<body>");
out.println("<form action ='Pmodif' method='post'>");
out.println("<center>");
out.println("Nom");
out.println("<input type='text' name='nom'
value='"+res.getString("nom")+"'><br><br>");
out.println("Salaire");
out.println("<input type='text' name='sal' value
="+res.getFloat("sal")+"><br><br>");
out.println("Prime");
out.println("<input type='text' name='pr'
value="+res.getFloat("pr")+"><br><br>");