Servlet-based MVC mode (using JSP, Servlet, JavaBean to implement MVC)

MVC pattern

1. The concept of
MVC MVC is an abbreviation for model, view, and controller, which represent three types of blame in Web applications.
Model : Business logic
view used to store data and process user requests : Submit data to the controller and display the data in the model.
Controller : According to the request made by the view, determine which model to process the request and data. The result is handed over to that view to update the display

2. Servlet-based MVC pattern
model : one or more JavaBean objects, used to store data (entity model, created by JavaBean classes) and processing business logic (business model, created by half of Java classes)
View : one or more JSP page, the controller submits data and provides data display for the model. The JSP page mainly uses HTML tags and JavaBean tags to display the data
controller : one or more Servlet objects are controlled according to the request submitted by the view, that is, the request is forwarded to the processing Business logic JavaBean, and store the processing result in the entity model JavaBean, and output to the view display

Use JSP, Servlet, JavaBean to implement MVC

Realize the MVC mode to realize a simple user login verification program.
1. Define to display data

package dto; 
public class User {
    
    
   private String name;
   private String pwd;	
   public String getName() {
    
    	
   	return name;
   		}	
   public void setName(String name) {
    
    
   	this.name=name;		
   	}	
   public String getPwd() {
    
    	
   	return pwd;	}	
  public void setPwd(String pwd) {
    
    	
  	this.pwd=pwd;	
  	}	
  	}

2. The UserCheck class is used to determine whether the user name and password are correct

package service;
import dto.User;
public class userCheck {
    
     
	public boolean validate(User user) {
    
    		
	   if(user!=null&&user.getName().equals("JSPMVC")) {
    
    
	   	if(user.getPwd().equals("MVC")) {
    
    		
	   			return true;			
	   			}			
	   return false;		
	   }		
	   return false;	
	   	}
	   	}

3. Write Servlet to complete request control

package servlet;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
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 service.userCheck;
import dto.User;
public class LoginCheckServlet extends HttpServlet {
    
    
	private static final long serialVersionUID = 1L;   
	protected void doGet(HttpServletRequest request,		
	HttpServletResponse response) throws ServletException, IOException {
    
    
			doPost(request,response);	} 	
        protected void doPost(HttpServletRequest request,			
        HttpServletResponse response) throws ServletException, IOException {
    
    		
        // TODO Auto-generated method stub		
        //doGet(request, response);	
        	request.setCharacterEncoding("utf-8");		 
        	 String name=request.getParameter("name");		 
        	 String pwd=request.getParameter("pwd");		  
        	 User user=new User();		  
        	 user.setName(name);		 
        	 user.setPwd(pwd);		  
        	 userCheck uc=new userCheck();		  
        	 if(uc.validate(user)) {
    
    		
        	 	  request.setAttribute("user", user);			  
        	 	  RequestDispatcher dis=request.getRequestDispatcher("loginSuccess.jsp");
        	 	  dis.forward(request,response);		
        	 	    }else {
    
    			 
        	 	   response.sendRedirect("loginCheck.jsp");		
        	 	     }	
        	 	     } 
        	 	     }

4. Login page

<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>loginCheck.jsp</title>
</head>
    <body>
        <form action="loginCheckServlet" method="post"><table>
        <tr>     
           <td>用户名:</td>    
           <td><input type="text" name="name"/></td></tr>
        <tr>     
           <td>密 码:</td>      
           <td><input type="password" name="pwd"/></td></tr>
        <tr>       
        <td><input type="submit" value="提交"/></td>       
        <td><input type="reset" value="重置"/></td>
        </tr>
        </table>
        </form>
        </body>
        </html>

Successful landing page

<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>loginSuccess.jsp</title>
</head>
    <body>
      <jsp:useBean id="user" type="dto.User" scope="requect"/>
      恭喜<jsp:getProperty property="name" name="user"/>登陆成功!
      </body>
      </html>

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/haha_7/article/details/109301303