1. What is MVC?
MVC Pattern stands for Model-View-Controller Pattern. This pattern is used to separate application’s concerns. Model: Handles data and business logic. View: Presents the data to the user whenever asked for. Controller: Entertains user requests and fetch necessary resources.
2. Example:
The whole program is as the picture show
First we create some jsp pages including index,jsp, login.jsp, signUp.jsp, about.jsp, notFound.jsp, codes are as following:
// index,jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Home Page</title> </head> <body> <!-- 完成简单的MVC控制 --> This is the home page where program start<br/> <!-- 使用Servlet作为controller来控制JSP页面的访问--> <a href="<%=request.getContextPath() %>/T?page=login">login</a><br/> <a href="<%=request.getContextPath() %>/T?page=about">about</a><br/> <a href="<%=request.getContextPath() %>/T?page=signUp">signUp</a><br/> <!-- 以下的方式也可以直接访问,但是没有实现MVC,没有实现前端后台的分离,不符合MVC模式 --> <%-- <a href="<%=request.getContextPath() %>/login.jsp">login</a><br/> --%> <%-- <a href="<%=request.getContextPath() %>/about.jsp">about</a><br/> --%> <%-- <a href="<%=request.getContextPath() %>/signUp.jsp">signUp</a><br/> --%> </body> </html>// login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>login</title> </head> <body> entered into login page </body> </html>// signUp.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>signUp</title> </head> <body> entered into signUp page </body> </html>// about.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>about</title> </head> <body> entered into about page </body> </html>// notFound.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>notFound</title> </head> <body> entered into notFound page </body> </html>Second, we need to create a controller here my class is T, code is as following:
package xxx.xxx; 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; /** * Servlet implementation class T */ @WebServlet("/T") public class T extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public T() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String paraString = request.getParameter("page"); if(paraString.equals("login")) { getServletContext().getRequestDispatcher("/login.jsp").forward(request, response); } else if(paraString.equals("signUp")) { getServletContext().getRequestDispatcher("/signup.jsp").forward(request, response); } else if(paraString.equals("about")) { getServletContext().getRequestDispatcher("/about.jsp").forward(request, response); } else { getServletContext().getRequestDispatcher("/notfound.jsp").forward(request, response); } } }Then, we start the index.jsp, and to show the result