jsp第七次作业

package org.mbw.dao;

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

import org.mbw.entity.Login;

public class LoginDao {
        public static int login(Login Login) {
            //boolean flag = false;
            int flag = -1 ;
            int result = -1;
            String URL = "jdbc:mysql://localhost:3306/move?useSSL=false&useUnicode=true&characterEncoding=UTF-8";
            String USERNAME = "root";
            String PWD = "123456";
            
            Connection connection = null;
            PreparedStatement ps = null;
            ResultSet rs = null;
            
            try {
                Class.forName("com.mysql.jdbc.Driver");
                connection = DriverManager.getConnection(URL, USERNAME, PWD);
                
                String sql = "select count(*) from user where uname = ? and pwsd = ?";
                ps = connection.prepareStatement(sql);
                ps.setString(1, Login.getUname());
                ps.setString(2, Login.getPwsd());
                rs = ps.executeQuery();
                if(rs.next()) {
                    result = rs.getInt(1);
                }
                if(result>0) {
                    return 1;
                }else {
                  return 0;
                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
                return -1;
            }catch(SQLException e) {
                e.printStackTrace();
                return -1;
            }catch(Exception e) {
                e.printStackTrace();
                return -1;
            }finally {
                try {
                if(rs!=null) rs.close();
                if(ps!=null) rs.close();
                if(connection!=null) rs.close();
                }catch(SQLException e) {
                    e.printStackTrace();
                }catch(Exception e) {
                    e.printStackTrace();
                }
            }
        }
}


package org.mbw.entity;

public class Login {
       private int id;
       private String uname;
       private String pwsd;
       
   public Login() {
}
   
   public Login(String uname, String pwsd) {
        this.uname = uname;
        this.pwsd = pwsd;
    }
    public Login(int id, String uname, String pwsd) {
        this.id = id;
        this.uname = uname;
        this.pwsd = pwsd;
    }
    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 getPwsd() {
        return pwsd;
    }
    public void setPwsd(String pwsd) {
        this.pwsd = pwsd;
    }
       
       
}



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


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("upwsd");
        Login login = new Login(name,pwd);//用户名,密码
        
        int result = LoginDao.login(login);
        if(result>0) {
            response.sendRedirect("Welcome.jsp");
        }else {
            response.sendRedirect("login.jsp");
        }
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

 

posted @ 2021-04-18 19:25  LiuZXin  阅读(9)  评论(0编辑  收藏  举报