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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
| import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class projetServlet13 extends HttpServlet {
private final String title="Génération de formulaire";
private final String DSName= "onlineReportdataSource";
private final String dbURL= "jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb)};DBQ=u:\\projet\\Projet.mdb";
Connection connexion=null;
Statement st=null;
ResultSet rs=null;
private String msgErreur0=null;
public void init(ServletConfig config){
try{
//il s'agit maintenant de se connecter à la base de données
// Connexion à la Bd
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
connexion=DriverManager.getConnection(dbURL,"Admin","");
System.out.println(connexion);
DatabaseMetaData dmd = connexion.getMetaData();
int type = dmd.getSQLStateType();
String sType = "";
if(type == DatabaseMetaData.sqlStateSQL99){
sType = "SQL99";
}
if(type == DatabaseMetaData.sqlStateXOpen ){
sType = " XOPEN";
}
System.out.println("Les SQLState suivent la norme "+sType);
}catch (Exception ex){
//probleme de connectio
System.out.println(ex);
System.out.println("pas de connexion a la bd");
} finally{
try {connexion.close();} catch (Exception ex) {}
}
}//init
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws IOException, ServletException{
//on récupère les paramètres envoyés Login et MDP de l'utilisateur
String login=request.getParameter("login");
String pwd=request.getParameter("pass");
try{
//Objet statement
System.out.println("avant");
st=connexion.createStatement();
System.out.println("apres");
//exécution requete pour récupérer valeurs dansla Bd puis les comparer
rs=st.executeQuery("SELECT password From Personne where name="+login;);
System.out.println("le mot de passse de l'utilisateur est"+rs.getString("password"));
}catch (SQLException ex){
//problème
msgErreur0=ex.getMessage();
System.out.println(ex);
System.out.println(ex.getSQLState());
System.out.println(ex.getErrorCode());
System.out.println(ex.getMessage());
ex.printStackTrace ();
while(ex!=null) {
System.out.println("passe");
ex = ex.getNextException();
if (ex!=null) System.out.println(ex);
}
System.out.println("probleme avec la requete");
}
catch (Exception e) { System.out.println(e); }
finally{
try{rs.close();}catch(Exception ex){}
try{st.close();}catch(Exception ex){}
}
// on indique le contenu du document
response.setContentType("text/html");
// flux de sortie
PrintWriter out=response.getWriter();
// on compare les password
try{
if (rs.getString("password") != pwd) {
out.println("<html><head><title>" + title +
"</title></head>");
out.println("<body>");
out.println(
"<h3>+ Incorrect password: Would you please reenter it !+</h3>");
out.println("</body></html>");
return;
} //if
}catch(Exception ex){}
// on gere les messages d'erreur
if (msgErreur0!=null){
//on genere la page d'erreur
out.println("<html><head><title>"+title+"</title></head>");
out.println("<body><h3>Application indisponible : vous avez entre un mauvais login ou password !</h3></body>");
out.println("</html>");
return;
}//if
// A ce stade, on envoit la reponse, a savoir, la 2e page HTML, le sommaire
out.println("<html>");
out.println("<title>+onlineReport.com+</title>");
out.println("<body bgcolor=\"white\">");
out.println("<br><br><br>");
out.println("<center>");
out.println("<h2>+WELCOME TO ONLINEREPORT.COM+</h2>");
out.println("</center>");
out.println("<center>");
out.println("<h3>+Here are the services we offer you:+</h3>");
out.println("</center>");
out.println("<br><br>");
out.println("<li>");
out.println("<A HREF=claimReport.html>+ FILL CLAIM REPORT_to fill within the 5 days following the accident.+</A>");
out.println("</body>");
out.println("</html>");
}
//post
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws IOException, ServletException
{
//renvoie sur GET
doGet(request,response);
}
} |
Partager