一、MVC设计模式实例
M层
Login.java
package org.entity;
public class Login {
private intid;
private String uname;
private String upwd;
public Login() {
}
public Login( String uname, String upwd) {
this.uname =uname;
this.upwd =upwd;
}
public Login(intid, String uname, String upwd) {
this.id =id;
this.uname =uname;
this.upwd =upwd;
}
public intgetId() {
returnid;
}
public void setId(intid) {
this.id =id;
}
public String getUname() {
returnuname;
}
public voidsetUname(String uname) {
this.uname =uname;
}
public String getUpwd() {
returnupwd;
}
public voidsetUpwd(String upwd) {
this.upwd =upwd;
}
}
LoginDao.jsp
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 {
public staticint login(Login login) {//登录
int flag=-1;//登录成功与否的标识 -1:系统异常,0:用户名或密码有误,1:登录成功
int result =-1;
Connection connection=null;
PreparedStatement pstmt=null;
ResultSet rs=null;try{
Class.forName("com.mysql.cj.jdbc.Driver");//Ctrl+1自动返回
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/login?serverTimezone=UTC&characterEncoding=utf-8","root","vayne");
String sql="Select count(*) from login where uname=? and upwd =?";
pstmt=connection.prepareStatement(sql);
pstmt.setString(1, login.getUname());
pstmt.setString(2, login.getUpwd());
rs=pstmt.executeQuery();if(rs.next()) {
result=rs.getInt(1);
}if(result>0) {//登录成功
flag= 1;
}else{
flag=0;//用户名或密码错误
}
}catch(ClassNotFoundException e) {
e.printStackTrace();
flag=-1;//系统异常
}catch(SQLException e) {
e.printStackTrace();
flag=-1;//系统异常
}catch(Exception e) {
e.printStackTrace();
flag=-1;//系统异常
}finally{try{if(rs!=null) rs.close();if(pstmt!=null) pstmt.close();if(connection!=null) connection.close();
}catch(SQLException e) {
e.printStackTrace();
}catch(Exception e) {
e.printStackTrace();
}
}returnflag;
}
}
View
login.jsp
登录用户名:密码:
welcome.jsp
Insert title hereController
LoginServlet.java
package org.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 org.dao.LoginDao;
import org.entity.Login;//控制器层:接受view层的请求,并分发给Model处理
public class LoginServlet extends HttpServlet {
protectedvoiddoGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//处理登录请求
request.setCharacterEncoding("utf-8");
String name= request.getParameter("uname");
String pwd= request.getParameter("upwd");
Login login=newLogin(name,pwd);//调用模型层的登录功能
int result=LoginDao.login(login);if(result>0) {
request.getRequestDispatcher("welcome.jsp").forward(request, response);
}else {//返回登录页,重新登录
request.getRequestDispatcher("login.jsp").forward(request, response);
}
}
protectedvoiddoPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//TODO Auto-generated method stub
doGet(request, response);
}
}
登录失败,返回登录页面