码迷,mamicode.com
首页 > 编程语言 > 详细

JavaWeb学习(13):MVC案例

时间:2020-03-25 19:22:40      阅读:73      评论:0      收藏:0      [点我收藏+]

标签:rom   uname   根据   res   分发   character   stat   user   submit   

前置知识:

MVC模式,不懂的戳我哦

View:

login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

		<!-- View 部分 -->

		<form action="LoginServlet" method = "post">
			
			<input type = "text" name = "uname">
			<input type = "password" name = "upwd">
			<input type = "submit" value = "登录">
		
		</form>
			
		
	
</body>
</html>

Welcome.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
		<%
			out.print("登录成功");
		%>
</body>
</html>

Controller:

LoginServlet.java

package org.servlet;

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 org.dao.LoginDao;
import org.entity.Login;

// 控制器实行分发

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
   
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		// 根据内置对象得到用户的信息
		String name = request.getParameter("uname");
		String password = request.getParameter("upwd");
		
		// 将用户信息封装成一个实体类,传递给模型层
		Login login = new Login(name,password);
		
		LoginDao loginDao = new LoginDao();
		
		// 传输一个JavaBean 封装的实体类
		int cnt = loginDao.login(login);
		
		// 根据从模型层返回的信息反馈给 View,执行具体的操作
		
		if(cnt > 0) {
			// 校验成功 跳转到欢迎界面
			response.sendRedirect("welcome.jsp");
		} else {
			// 校验失败 跳转到 登录界面 进行重新登录
			response.sendRedirect("login.jsp");
		}

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {		
		doGet(request, response);
	}

}


Model:

LoginDao.java -- 封装业务逻辑的 JavaBean

JDBC相关知识的 Click Me

package org.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.entity.Login;

// 模型层(封装逻辑或者封装实体类 -- 数据 , 实现某种功能)

public class LoginDao {
	
	private static  String URL = "jdbc:mysql://localhost:3306/sqltest";
	private static  String User = "root";
	private static  String Password = "root";
	
	//封装业务逻辑,实现校验用户信息的功能
	
	public int login(Login login) {
		
		Connection connection = null;
		PreparedStatement pstam = null;
		ResultSet rs = null;
		
		try {
			
			Class.forName("com.mysql.jdbc.Driver");
			
			connection = DriverManager.getConnection(URL, User, Password);
			
			String sql = "select count(*) from login where uname = ? && upwd = ?";
			
			pstam = connection.prepareStatement(sql);
			
			pstam.setString(1,login.getName());
			pstam.setString(2,login.getPassword());
			
			rs = pstam.executeQuery();
			
			if(rs.next()) {
				return 1;
			} else {
				return 0;
			}
			
		} catch(ClassNotFoundException e) {
			e.printStackTrace();
		} catch(SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if(rs != null) rs.close();
				if(pstam != null) pstam.close();
				if(connection != null) connection.close();
			} catch(SQLException e) {
				e.printStackTrace();
			}
		}
		return -1;
	}
}

Login.java -- 封装数据的 JavaBean (对应数据库中的一张表)

package org.entity;

// 封装数据的 JavaBean,对应数据库中的一张表;

public class Login {
	private String name;
	private String password;
	
	// 无参的构造方法
	public Login() {

	}
	// 带参的构造方法
	public Login(String name, String password) {
		
		this.name = name;
		this.password = password;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
}

后记:

一定要有自信,不要让怯懦成为你前进路上的畔脚石。

JavaWeb学习(13):MVC案例

标签:rom   uname   根据   res   分发   character   stat   user   submit   

原文地址:https://www.cnblogs.com/prjruckyone/p/12568459.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!