1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.util.*;
public class tp18 extends HttpServlet {
public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
PrintWriter out = res.getWriter();
try {
HttpSession session = req.getSession(true); /* creation d'une session pour le panier*/
Panier panier;
res.setContentType("text/html");
out.println(" <head><title>TP18</title></head><body><center> ");
out.println(" <h1>Contenu de la table PRODUITS : </h1> ");
/* ________ CONNECTION ________ */
String url = "jdbc:odbc:J2EE";
Connection con = null;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection(url, "admin", "ukvg");
Statement stmt = con.createStatement();
String s;
String query = "select * from PRODUITS";
ResultSet rs = stmt.executeQuery(query);
ResultSetMetaData rsmd = rs.getMetaData();
if (session.getAttribute( "panier" ) != null) {
//panier = (Panier)session.getAttribute( "panier" );
}else{
//panier = new Panier();
}
String b;
//session.setAttribute( "panier", (Panier)panier );
/* AFFICHAGE PAN IER */
int nbCols = rsmd.getColumnCount();
out.println("<table border= 4px> <thead> <tr> <th>NUM</th> <th>LIBELLE</th> <th>PRIX</th> </tr> </thead><tbody>");
while(rs.next()) {
out.println(" <tr>");
for (int i = 1; i <= nbCols; i++) {
out.println("<td>");
if (rsmd.getColumnName(i).equals("libellé")) {
s = rs.getString(""+rsmd.getColumnName(i));
out.println("<a href=tp18?id="+s+ ">"+ s +"</a>");
}else{
/* rs : recupere le contenu rsmd : recupere le nom de la colonne */
out.println(" " + rs.getString("" + rsmd.getColumnName(i)));
}
}
out.println("</tr>");
}
out.println(" </tbody></table>");
if (req.getParameter("id") != null) { // si on a un ID ==> article selectionné
panier.ajouter(req.getParameter("id"));
}
if (req.getParameter("b") != null) {
panier.vider();
}
/* AFFICHAGE PANIER */
String resultat = panier.afficher();
out.println(resultat + "<br/>");
/* _________________ */
/* bouton pour effacer panier*/
out.println("</center><p><form action=\"http://localhost:8080/vide/servlet/tp18?b=true\" method=\"post\">");
out.println("<INPUT TYPE=\"submit\" VALUE=\"vider Panier\">");
out.println("</form></p>");
/*_____________________*/
con.close();
} catch (Exception e) {
out.println("erreur : "+e);
}
out.println(" </body>");
}
} |
Partager