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
| import com.hibernate.dao.*;
import com.hibernate.dao.base.BaseUtilisateurDAO;
import com.hibernate.service.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
/**
* Servlet implementation class Application
* @param <ServletConfig>
*/
public class Application extends HttpServlet {
private static final long serialVersionUID = 1L;
private ServletConfig config;
Service service ;
BaseUtilisateurDAO dao = null;
public Application() {
super();
}
/* public void init()
throws ServletException{
ServletConfig config = getServletConfig();
try {
dao.init();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
service.setDao(dao);
}
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
try {
Session ses=HibernateUtil.currentSession();
Transaction tx =ses.beginTransaction();
Utilisateur u=new Utilisateur();
u.setAge(25);
u.setNom("Maroua");
u.setId(23);
ses.save(u);
tx.commit();
HibernateUtil.closeSession();
//doListPersonne(request, response,ses);
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void doListPersonne(HttpServletRequest request,
HttpServletResponse response , Session s) throws ServletException, IOException, HibernateException {
try{Iterator<Utilisateur> it=service.getAll(s);
PrintWriter out =response.getWriter();
out.print("<table border=\"1\">");
while(it.hasNext()){
Utilisateur e2 =(Utilisateur)it.next();
out.print("<tr><td>"+ e2.getNom()+"</td>"+"<td>"+ e2.getAge()+"</td></tr>" );
}
out.print("</table>");
}catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
// on passe la main au GET
}
} |
Partager