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
| package project2;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.*;
import javax.servlet.http.*;
public class Servlet1 extends HttpServlet {
private static final String CONTENT_TYPE =
"text/html; charset=windows-1252";
private Connection connexion = null;
private String URL = "jdbc:odbc:inventaire";
public void init(ServletConfig config) throws ServletException {
super.init(config);
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connexion = DriverManager.getConnection(URL, "", "");
} catch (Exception e) {
e.printStackTrace();
connexion = null;
}
if (connexion != null) {
ServletContext application = this.getServletContext();
application.setAttribute("connexion", connexion);
}
}
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>Servlet #1</title></head>");
out.println("<body>");
out.println("<form id=\"form\" name=\"categorie\" method=\"get\" action=\"servlet2\">\n" +
" <label>\n" +
" <select name=\"categorie\" id=\"categorie\">\n" +
" <option value=\"Processeurs\" selected=\"selected\">Processeurs</option>\n" +
" <option value=\"Carte Maitresse\">Carte Maitresse</option>\n" +
" <option value=\"Modem\">Modem</option>\n" +
" <option value=\"Imprimantes\">Imprimantes</option>\n" +
" <option value=\"Mémoire Virtuelle\">Mémoire Virtuelle</option>\n" +
" <option value=\"Numériseur\">Numériseur</option>\n" +
" <option value=\"Moniteur\">Moniteur</option>\n" +
" <option value=\"Carte Vidéo\">Carte Vidéo</option>\n" +
" </select>\n" +
" </label>\n" +
" <label>\n" +
" <input type=\"submit\" name=\"soumettre\" id=\"soumettre\" value=\"Soumettre\" />\n" +
" </label>\n" +
" <p> </p>\n" +
"</form>");
out.println("</body></html>");
out.close();
}
public void destroy() {
try {
if (connexion != null) {
connexion.close();
}
} catch (Exception e) {
System.err.println("Problème à la fermeture de" +
" la base de données");
}
}
} |