1. About MVC
①.m:model
②.v:view
③.c:controller
MVC is the abbreviation for Model-view-controller, the model-view-controller.
MVC is a design pattern that divides an application into three core modules: models, views, and controllers, each of which handles its own tasks.
2. Benefits: Do not use Mvc,servlet, JSP can be implemented for a request, but the disadvantage is that you need to spell the HTML code in the Servlet, write the code to process the request in the JSP
The process of MVC
The following code is insufficient: No database connection pool, dbutils, DAO, etc., one request a servlet, should be multiple requests (a module) a servlet, delete time did not join jquery. Just to present the example.
Used here
M (DAO)
V (JSP) Fill out the Java Code implementation display on the page
C (Servlet): Accept request, GET request parameter, call DAO method, put DAO return value into request, forward (redirect) page.
to render.
3. Code rendering
To create a student table:ID, name, hiredate
mvc_query.jsp
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 " pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
Mvc_query_servlet.java
/** * Servlet Implementation class Mvc_query_servlet */@WebServlet ("/getallstudent") public class Mvc_query_servlet Extends HttpServlet {private static final long serialversionuid = 1l;protected void doget (HttpServletRequest request, Http Servletresponse response) throws Servletexception, IOException {request.getrequestdispatcher ("/mvc_query_show.jsp") . Forward (request, response);}}
mvc_query_show.jsp
<% @page import= "Xixihaha. Studentdao "%><% @page import=" Xixihaha. Student "%><% @page import=" java.util.List "%><%@ page language=" java "contenttype=" text/html; Charset=utf-8 "pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >Studentdao.javapublic class Studentdao {private Connection getconnection () {Connection conn = null; String Driverclass = "Com.mysql.jdbc.Driver"; String url = "Jdbc:mysql://127.0.0.1:3306/mydatabase"; String user = "root"; String password = "Qiaolezi"; try {class.forname (driverclass); conn = drivermanager.getconnection (URL, user, password);} catch (Exception e) {e.printstacktrace ();} Return conn;} /** * Delete the student with the specified ID * @param id */public void Deleteid (Integer id) {PreparedStatement PS = null; String sql = "Delete from students where id =?"; try {PS = getconnection (). preparestatement (SQL);p S.setint (1, id);p s.executeupdate ();} catch (SQLException e) { E.printstacktrace ();}} /** * Get all student information * @return */public list<student> getAll () {PreparedStatement PS = null; ResultSet rs = null; list<student> students = new arraylist<student> (); try {String sql = "SELECT ID, stu_name,hire_date from Studen TS ";p s = getconnection (). preparestatement (sql); rs = Ps.executequery (); while (Rs.next ()) {int id = rs.getint (1); String NAme = rs.getstring (2);D ate Date = (date) rs.getobject (3); Students.add (new Student (Id,name,date));}} catch (Exception e) {e.printstacktrace ();} return students;}}
About when to redirect?
If the target response page does not need to read any value from request, then redirection can be used. You can also prevent duplicate submissions of forms.
MVC Programming Patterns