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
| public class ClientCtrl extends HttpServlet implements Constantes {
private static final long serialVersionUID = 1L;
public void doIt(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
resp.setContentType(GetInHtml);
try {
String action = req.getParameter("_action");
if (action.equals("0")) {
listAll(req, resp);
return;
}
if (action.equals("Edit")) {
editClient(req, resp);
return;
}
if (action.equals("Maj")) {
doMaj(req, resp);
return;
}
} catch (Exception e) {
e.printStackTrace();
resp.getWriter().write("Erreur:" + e.getMessage());
}
}
private void listAll(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
resp.setContentType(GetInHtml);
try {
ArrayList<Client> listeClt = new ArrayList<Client>();
listeClt=ServiceFactory.getInstance().clientService.listAll();
req.getSession().setAttribute("listeClt", listeClt);
resp.sendRedirect(pageAPP + "Client.jsp");
return;
} catch (Exception e) {
resp.getWriter().write("1//" + e.getMessage());
}
}
private void editClient(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
resp.setContentType(GetInHtml);
try {
Client clt=new Client();
req.getSession().setAttribute("op", req.getParameter("op"));
req.getSession().setAttribute("id", req.getParameter("id"));
if(req.getParameter("op").equals("2")){
clt=ServiceFactory.getInstance().clientService.getById(new Integer(req.getParameter("id")));
}
req.getSession().setAttribute("clt", clt);
resp.sendRedirect(pageAPP + "ClientEdit.jsp");
return;
} catch (Exception e) {
resp.getWriter().write("1//" + e.getMessage());
}
}
private void doMaj(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
resp.setContentType(GetInHtml);
Client client = new Client();
try {
if ((req.getParameter("txtOp").equals("1"))
|| (req.getParameter("txtOp").equals("2"))) {
client.setLibelle(req.getParameter("txtLibelle"));
client.setAdresse(req.getParameter("txtAdresse"));
if (req.getParameter("txtOp").equals("1")) {
ServiceFactory.getInstance().clientService.create(client);
}
if (req.getParameter("txtOp").equals("2")) {
client.setId(new Integer(req.getParameter("txtId")));
ServiceFactory.getInstance().clientService.update(client);
}
}
// delete
// -------------------------------------------------------------------------
if (req.getParameter("txtOp").equals("3")) {
ServiceFactory.getInstance().clientService.delete(req
.getParameter("id"));
}
resp.sendRedirect(pageAPP + "Client.jsp");
} catch (Exception e) {
e.printStackTrace();
resp.getWriter().write("1//" + e.getMessage());
}
}
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException
{
doIt(req, resp);
}
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException
{
doIt(req, resp);
}
}
// FIN |
Partager