Вы находитесь на странице: 1из 5

ED n6 - corrig

Exercices dirigs n6 - corrig


Gnrer le formulaire HTML qui produit cette page (balise form)

Solution
<form action='Boutique' method='get'>
<input type='radio' name='radio' value='crayon'><h7>crayon</h7><br>
<input type='radio' name='radio' value='gomme'><h7>gomme</h7><br>
<input type='radio' name='radio' value='cahier'><h7>cahier</h7><br>
<input type='radio' name='radio' value='rgle'><h7>rgle</h7><br>
<input name='choix' type='submit' value='choisir un article'>
</form>
Coder la mthode doGet de la servlet Boutique qui rcupre la valeur du paramtre slectionn
par la question prcdente et l'affiche en rsultat
Solution
protected void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/plain");
PrintWriter out = res.getWriter();
String article = req.getParameter("radio");
out.println( article );
}
Coder la servlet Blog qui rcupre un nom et un mot de passe partir d'un formulaire puis les
compare aux valeurs initiales de la servlet (dans le web.xml). Au cas o elles sont identiques, on
considre que l'utilisateur est authentifi, une page HTML est cre par la servlet qui propose une
zone de texte et un bouton de validation.
En rsum, il faut crer un premier formulaire puis la partie utile du du web.xml, puis la servlet Blog
qui affiche la nouvelle page.

Solution
<form action='Blog' method='get'>
<input type='text' name='nom' value='Votre nom'><br>
<input type='passwd' name='passwd' value='Mot de passe'> <br>
<input name='choix' type='submit' value='valider'>
</form>
<servlet>
<servlet-name>Blog</servlet-name>
<servlet-class>servlets.Blog</servlet-class>
1

ED n6 - corrig
<init-param>
<param-name>nomInitial</param-name>
<param-value>moi</param-value>
</init-param>
<init-param>
<param-name>passwdInitial</param-name>
<param-value>d2XW5*2Q</param-value>
</init-param>
</servlet>
public class Blog extends HttpServlet {
private String nomInitial = null;
private String passwdInitial = null;
public void init() throws ServletException{
ServletConfig config = getServletConfig();
nomInitial = config.getInitParameter("nomInitial");
passwdInitial = config.getInitParameter("passwdInitial");
}

public void doGet(HttpServletRequest req,HttpServletResponse res)


throws ServletException, IOException{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
String nom = req.getParameter("nom");
String passwd = req.getParameter("passwd");
if (nom == nomInitial && passwd == passwdInitial){
nom = nomParDefaut; }
if (passwd == null) { passwd = passParDefaut; }
out.println("<HTML>");
out.println("<HEAD><TITLE>Accueil</TITLE></HEAD>");
out.println("<BODY>");
out.println("<form action='' method='get'>");
out.println("<textarea name='zone' rows='5' cols='40'><br>");
out.println("<input name='texte' type='submit' value='valider'>");
out.println("</form>");
out.println("<br></BODY></HTML>");
}

Etant donne la base de donnes bd-cocktails constitue de la table :


cocktail (nom,alcool,proportionAlcool,soda,jus,proportionJus)
Supposons connu le bean Cocktail constitu de ses "getters" et "setters" et d'un constructeur avec
initialisation :
public Cocktail(String nom,String alcool,int proportionAlcool,
String soda,String jus,int proportionJus) {
this.alcool = alcool;
this.proportionAlcool = proportionAlcool;
this.soda = soda;
this.jus = jus;
this.proportionJus = proportionJus;
}
En utilisant les PreparedStatement pour constituer les requtes SQL, complter la classe
DAOCocktail dont le rle est de transmettre des requtes SQL la base de donnes :
2

ED n6 - corrig

import
import
import
import
import
import
import
import

beans.Cocktail;
java.sql.Connection;
java.sql.ResultSet;
java.sql.PreparedStatement;
java.sql.SQLException;
java.util.logging.Level;
java.util.logging.Logger;
javax.servlet.UnavailableException;

public class DAOCocktail {


private PreparedStatement pSmt = null;
private ResultSet rs = null;
private Connection dbcon;
public DAOCocktail() throws UnavailableException{
try {
dbcon = new DAOConnexion().getConnexion();
} catch (SQLException ex) {
Logger.getLogger(DAOCocktail.class.getName()).log(Level.SEVERE,null,ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(DAOCocktail.class.getName()).log(Level.SEVERE,null,ex);
}
}

public Cocktail select( String nom )throws SQLException{



}
public void insert( Cocktail cocktail ) throws SQLException{

}
public void update( Cocktail cocktail ) throws SQLException{

}
public void delete( String nom ) throws SQLException{

}

Solution
import
import
import
import
import
import
import
import

beans.Cocktail;
java.sql.Connection;
java.sql.ResultSet;
java.sql.PreparedStatement;
java.sql.SQLException;
java.util.logging.Level;
java.util.logging.Logger;
javax.servlet.UnavailableException;

public class DAOCocktail {


private PreparedStatement pSmt = null;
private ResultSet rs = null;
private Connection dbcon;
public DAOCocktail() throws UnavailableException{
try {
dbcon = new DAOConnexion().getConnexion();
} catch (SQLException ex) {
3

ED n6 - corrig

Logger.getLogger(DAOCocktail.class.getName()).log(Level.SEVERE,null,ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(DAOCocktail.class.getName()).log(Level.SEVERE,null,ex);
}

public Cocktail select( String nom )throws SQLException{


Cocktail cocktail = null;
pSmt = (PreparedStatement)dbcon.prepareStatement
("SELECT * FROM cocktail WHERE nom=?" );
pSmt.setString(1,nom);
rs = pSmt.executeQuery();
if(rs.next()){
String alcool = rs.getString("alcool");
int proportionAlcool = rs.getInt("proportionAlcool");
String soda = rs.getString("soda");
String jus = rs.getString("jus");
int proportionJus = rs.getInt("proportionJus");
cocktail = new
Cocktail(nom,alcool,proportionAlcool,soda,jus,proportionJus);
}
rs.close();
pSmt.close();
return cocktail;
}
public void insert( Cocktail cocktail ) throws SQLException{
String alcool = cocktail.getAlcool();
int proportionAlcool = cocktail.getProportionAlcool();
String soda = cocktail.getSoda();
String jus = cocktail.getJus();
int proportionJus = cocktail.getProportionJus();
pSmt = (PreparedStatement)dbcon.prepareStatement
("insert into cocktail values(?,?,?,?,?)");
pSmt.setString(1,alcool);
pSmt.setInt(2,proportionAlcool);
pSmt.setString(3,soda);
pSmt.setString(1,jus);
pSmt.setInt(2,proportionJus);
pSmt.executeUpdate();
pSmt.close();
}
public void update( Cocktail cocktail ) throws SQLException{
String alcool = cocktail.getAlcool();
int proportionAlcool = cocktail.getProportionAlcool();
String soda = cocktail.getSoda();
String jus = cocktail.getJus();
int proportionJus = cocktail.getProportionJus();
pSmt = (PreparedStatement)dbcon.prepareStatement
("update cocktail set
alcool=?,proportionAlcool =?,soda=?,jus=?,proportionJus=? " );
pSmt.setString(1,alcool);
pSmt.setInt(2,proportionAlcool);
pSmt.setString(3,soda);
pSmt.setString(4,jus);
pSmt.setInt(5,proportionJus);
pSmt.executeUpdate();
pSmt.close();
}
4

ED n6 - corrig
public void delete( String nom ) throws SQLException{
pSmt = (PreparedStatement)dbcon.prepareStatement
("delete from personne where nom=?" );
pSmt.setString(1,nom);
pSmt.executeUpdate();
pSmt.close();
}
}
Supposons connu le bean Cocktails :
public class Cocktails {
Hashtable<String,Cocktail> liste;
public Cocktails(){
liste = new Hashtable<String,Cocktail>();
}
public Hashtable<String, Cocktail> getListe() {
return liste;
}
public void setListe(Hashtable<String, Cocktail> liste) {
this.liste = liste;
}
public void ajouterCocktail( Cocktail cocktail ){
liste.put(cocktail.getNom(),cocktail);
}
}
En utilisant les PreparedStatement pour constituer la requte SQL, complter la mthode
select de la classe DAOCocktails dont le rle est de transmettre des requtes SQL la base de
donnes :
public Cocktails select( String alcool )throws SQLException{...

Solution
public Cocktails select( String alcool )throws SQLException{
Cocktails cocktails = new Cocktails();
Cocktail cocktail = null;
pSmt = (PreparedStatement)dbcon.prepareStatement
("SELECT * FROM cocktail WHERE alcool=?" );
pSmt.setString(1,alcool);
rs = pSmt.executeQuery();
if(rs.next()){
String nom = rs.getString("nom");
int proportionAlcool = rs.getInt("proportionAlcool");
String soda = rs.getString("soda");
String jus = rs.getString("jus");
int proportionJus = rs.getInt("proportionJus");
cocktail = new
Cocktail(nom,alcool,proportionAlcool,soda,jus,proportionJus);
cocktails.ajouterCocktail(cocktail);
}
rs.close();
pSmt.close();
return cocktails;
}
5