Java web ----- MVC design mode, web ----- mvc design mode
I. MVC divides the code into three parts: View (jsp), model (javaBean), and control part (servlet );
The view is basically a jsp file and the main content is the html code of the interface, which is responsible for displaying the interface;
The model is a javaBean, responsible for interacting with the database;
The Control Department is divided into servlet roles, responsible for processing business logic and page switching.
2. MVC includes Model1 and Model2;
1. The Model1 model procedure is as follows:
In Model1, the interface is displayed and the interface jumps. the business logic is handled by jsp, which leads to a large amount of java script code in jsp,
Code repetition rate is high, availability is low, and minor modifications to program functions often cause a large number of modifications. The advantage is that it is easy to control.
2. The Model2 model procedure is as follows:
The layer in the Model2 model is more obvious. The jsp is responsible for the page display, and the javaBean is responsible for database operations and business logic, servlet
It is the core scheduling part. It is responsible for calling javaBean to process the business logic and scheduling and switching on the interface. Advantage: Program
The execution is clearer to facilitate the addition and modification of later functions.
Iii. User Login instance based on MVC model:
1. Interface section (login. jsp logon interface, login_success.jsp logon success interface, login_failure.jsp logon Failure Interface)
Login. 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"> :
Login_success.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"> :
Login_failure.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"> :
2. servlet (controller)
LoginConf. java
Package com. javaweb. mvc; import java. io. IOException; 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 javax. servlet. http. httpSession;/*** Servlet implementation class loginConf */@ WebServlet ("/loginConf") public class loginConf extends HttpServlet {private static final long serialVersionUID = 1L; /*** @ see HttpServlet # HttpServlet () */public loginConf () {super (); // TODO Auto-generated constructor stub}/*** @ see HttpServlet # doGet (HttpServletRequest request, response) */protected void doGet (HttpServletRequest request, response) throws ServletException, IOException {// TODO Auto-generated method stub // set the encoding format response. setContentType ("text/html; charset = UTF-8"); request. setCharacterEncoding ("UTF-8"); // obtain the String uname = request. getParameter ("uname"); String password = request. getParameter ("password"); // get session HttpSession session = request. getSession (); // service logic judgment loginCheck lc = new loginCheck (); if (lc. isLogin (uname, password) {session. setAttribute ("login", "true"); request. getRequestDispatcher ("MVC/login_success.jsp "). forward (request, response);} else {request. getRequestDispatcher ("MVC/login_failure.jsp "). forward (request, response) ;}}/*** @ see HttpServlet # doPost (HttpServletRequest request, response) */protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stub doGet (request, response );}}View Code
3. javaBean (business logic Model)
Package com. javaweb. mvc; public class loginCheck {public boolean isLogin (String uname, String password) {if (uname. equals ("wei") & password. equals ("123") {return true;} else {return false ;}}}View Code