Cette page nécessite de connaître les Servlet et les jsp.
package com.llgc.controleur; import java.io.IOException; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.llgc.modele.ModeleVoiture; import com.llgc.modele.pojo.POJOVoiture; /** * Servlet implementation class CtrlJSP */ @SuppressWarnings("serial") @WebServlet("/CtrlJSP") public class CtrlJSP extends HttpServlet { private ModeleVoiture modele = new ModeleVoiture(); /** * @see HttpServlet#HttpServlet() */ public CtrlJSP() { super(); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { POJOVoiture voiture = new POJOVoiture(); voiture.setMarque(request.getParameter("marque")); voiture.setPoids(Integer.parseInt(request.getParameter("poids"))); voiture.setNom(request.getParameter("nom")); try { modele.create(voiture); response.sendRedirect("index2.jsp"); } catch (ClassNotFoundException | SQLException e) { response.sendRedirect("error.jsp"); } } }
package com.llgc.modele; import java.sql.SQLException; import com.llgc.dao.DAOVoiture; import com.llgc.modele.pojo.POJOVoiture; public class ModeleVoiture { private DAOVoiture dao = new DAOVoiture(); public void create(POJOVoiture entite) throws ClassNotFoundException, SQLException { dao.create(entite); } public POJOVoiture read(int id) throws ClassNotFoundException, SQLException { return dao.read(id); } public void update(POJOVoiture entite) throws ClassNotFoundException, SQLException { dao.update(entite); } public void updateNom(int id, String nom) throws ClassNotFoundException, SQLException { dao.updateNom(id, nom); } public void updatePoids(int id, int poids) throws ClassNotFoundException, SQLException { dao.updatePoids(id, poids); } public void updateMarque(int id, String marque) throws ClassNotFoundException, SQLException { dao.updateMarque(id, marque); } public void delete(int id) throws ClassNotFoundException, SQLException { dao.delete(id); } }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>Insert title here</title> </head> <body> <form method="post" action="CtrlJSP"> <span class="label_nav">nom</span> <input type="text" id="nom" name="nom" required="required" /><br /> <span class="label_nav">poids</span> <input type="text" id="poids" name="poids" required="required" /><br /> <span class="label_nav">marque</span> <input type="text" id="marque" name="marque" required="required" /><br /> <input type="submit" value="Validation" id="button" /> </form> </body> </html>