directory

  • What is MVC?
  • MVC pattern composition
    • Model
    • View
    • Controller
  • The sample
    • M layer
      • DAO data access interface
    • V layer
      • login.jsp
      • login_success.jsp
    • C layer
      • com.servlet.LoginServlet
  • summary

What is MVC?


MVC (Model View Controller) Model – View – Controller. Typical MVC is javabean(M)+JSP(V) + servlet(C) mode, is the further decoupling of DAO mode, we can simply think of DAO based on a servlet(acting as a controller), its purpose is to achieve the separation of M and V, convenient before and after parallel development.

MVC pattern composition


The sample


To realize the login

M layer

DAO data access interface

See the code here

V layer

login.jsp

<%@ page language="java"  pageEncoding="gbk"% > <! DOCTYPE HTML><html>
  <head>
    <meta content="text/html; charset=gbk">
    <title>Landing page</title>
  </head>
  
  <body>Null if(request.getAttribute("err")!) if(request.getAttribute("err")! =null) { %><h2><%=request.getAttribute("err")%></h2>The < %} % ><form action="Login" method="post">User ID:<input type="text" name="id"/><br/>Password:<input type="password" name="password"/><br/>
	<input type="submit" value="Login"/>
	<input type="reset" value="Reset"/>
	</form>
  </body>
</html>
Copy the code

login_success.jsp

<%@ page language="java" contentType="text/html; charset=gbk"
    pageEncoding="gbk"% > <! DOCTYPE html><html>
<head>
    <meta content="text/html; charset=gbk">
	<title>Login successful</title>
</head>
<body><% if(session.getAttribute("uname")! =null) {// user logged in %><h2>Log in successfully</h2>
			<h2>welcome<font color="red" size="12">
				<%=session.getAttribute("uname")%>
			</font>visit</h2><%} else {response.setHeader("refresh","3; URL=login.jsp") ; %> You have not logged in, please log in first!!<br/>Jump to login page after 3 seconds!!<br/>If no jump, please click<a href="login.jsp">here</a>!<br/>The < %} % ></body>
</html>
Copy the code

C layer

com.servlet.LoginServlet

package com.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.factory.DAOFactory;
import com.vo.Person;
public class LoginServlet extends HttpServlet {
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		String path = "login.jsp";
		// 1
		String id = request.getParameter("id");
		String password = request.getParameter("password");
		// 2. Assign the requested content to the VO class
		Person person = new Person();
		person.setId(id);
		person.setPassword(password);
		try {
			// Perform database validation
			if (DAOFactory.getPersonDAOInstance().login(person)) {
				// If true, the user ID and password are valid
				// Set the user name to the session scope
				request.getSession().setAttribute("uname", person.getName());
				// Modify the jump path
				path = "login_success.jsp";
			} else {
				// Login failed
				// Set the error message
				request.setAttribute("err"."Wrong user ID and password!!"); }}catch (Exception e) {
		}
		// Jump
		request.getRequestDispatcher(path).forward(request, response);
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		this.doPost(request, response); }}Copy the code

summary


Advantages: MVC code reuse is high, low coupling, completely separate the view layer and business layer, can be developed in parallel at the front and back end.

Original is not easy, please do not reprint (this is not rich visits add insult to injury) blogger home page: blog.csdn.net/qq_45034708 If the article is helpful to you, remember to focus on the likes collection ❤