Cette page nécessite de connaître les [[helloworld:web:java:servlet|Servlet]] et les [[helloworld:web:java:jsp|jsp]].
=====Schéma MVC=====
{{:helloworld:design_pattern:mvc:java:jsp:simple-web-mvc-01.png|}}
[[http://bearprogrammer.com/2012/08/05/simple-web-mvc-with-servlets-and-jsp/|Simple web MVC with servlets and JSP]] {{ :helloworld:design_pattern:mvc:java:jsp:simple_web_mvc_with_servlets_and_jsp_bear_programmer_2020-04-27_11_13_22_am_.html |Archive du 08/05/2012 le 27/04/2020}}
=====Exemple=====
====Controleur====
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");
}
}
}
====Modèle====
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);
}
}
====Vue====
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here