三层架构

三层架构:     

package org.entity; public class Login { private int id; private String uname; private String upwd; public Login() { } public Login( String uname, String upwd) { this.uname = uname; this.upwd = upwd; } public Login(int id, String uname, String upwd) { this.id = id; this.uname = uname; this.upwd = upwd; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getUpwd() { return upwd; } public void setUpwd(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 static int 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();
                  }
                 
              
          }
          return flag;
      }
}
三层架构

View

login.jsp

三层架构  
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body>
        <form action="LoginServlet" method="post">
        用户名:<input type="text" name="uname"><br/>
        密码:<input type="password" name="upwd"><br/>
        <input type="submit" value="登录"><br/>
        
        </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>
      登陆成功!!!
</body>
</html>
三层架构

Controller

 

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 {
     
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //处理登录请求    
        request.setCharacterEncoding("utf-8");
        String name= request.getParameter("uname");
        String pwd= request.getParameter("upwd");
        Login login=new Login(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);
        }
    }

    
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
三层架构

 

 

三层架构

 

上一篇:第二章--django--URL和视图函数


下一篇:SQL70 牛客每个人最近的登录日期(五)