一个简单的基于MVC模式的登录验证过程

MVC即Model-View-Controller(模型-视图-控制器)是一种软件设计模式,MVC最早出现在Smalltalk语言中,后来在Java中得到广泛应用。首先,简述一下,mvc模式各模块之间的调用过程:用户通过应用程序发出请求,该请求转发给控制器,控制器接受请求,选择相应的业务逻辑处理该请求并调用相应的模型进行处理。相应模型处理请求存取相关数据并返还所查询数据给控制器,控制器接收从模型中返回的数据并选择相应的视图来响应结果。

接下来,看看应用mvc模式的jsp模型,如图所示:

一个简单的基于MVC模式的登录验证过程_第1张图片

最后,让我们实践一下,看一个登录验证小程序:

首先,新建一个登录页面login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>





账户登录



	
用户名
密    码

一个JavaBean类:QueryJDBC.java

package com.yangliu.bean;

import java.sql.*;

public class QueryJDBC {
	private String Driver = "com.mysql.jdbc.Driver";
	private String url = "jdbc:mysql://localhost:3306/xmxblog";
	private String user = "root";
	private String password = "19911008";
	private Connection conn = null;
	private Statement st = null;
	private ResultSet rs = null;
	private String name, psw;

	public QueryJDBC() {
		try {
			Class.forName(Driver);
			conn = DriverManager.getConnection(url, user, password);
			st = conn.createStatement();
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e);
		}

	}
	public void setName(String name) {
		this.name = name;
	}
	public String getName() {
		return name;
	}
	public void setPassword(String psw) {
		this.psw = psw;
	}
	public String getPassword() {
		return psw;
	}
	public boolean validate() {
		String sqlSelect = "select * from user_info where userName='" + name
				+ "' and userPsw='" + psw + "'";
		System.out.println(sqlSelect);
		try {
			rs = st.executeQuery(sqlSelect);
			if (rs.next())
				return true;
		} catch (SQLException e) {
			e.printStackTrace();
			System.out.println(e);
		}
		return false;
	}
}
一个Servlet类:LoginServlet.java

package com.yangliu.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.yangliu.bean.*;
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		QueryJDBC db = new QueryJDBC();
		db.setName(username);
		db.setPassword(password);
		boolean status = db.validate();
		if(status){
			RequestDispatcher rd = request.getRequestDispatcher("login_success.jsp");
			rd.forward(request,response);
		}
		else{
			RequestDispatcher rd = request.getRequestDispatcher("login_failure.jsp");
			rd.forward(request,response);
		}
		
	}

}
配置web.xml,执行项目可操作观察整个登录验证过程。mvc较传统的模式易于维护,代码重用性更好。




	Login
	
		LoginServlet
		LoginServlet
		
		com.yangliu.servlet.LoginServlet
	
	
		LoginServlet
		/login
	
	
		login.jsp
	
	

 
  


你可能感兴趣的:(mvc模式应用实例)