简单JSP+mvc登录实现

这里写图片描述
User.java

public class User {
    private String name;
    private String userid;
    private String password;

    public String getName() {
        return name;
    }
    public String getPassword() {
        return password;
    }
    public String getUserid() {
        return userid;
    }
    public void setUserid(String userid) {
        this.userid = userid;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public void setName(String name) {
        this.name = name;
    }
}

DatabaseConnection.java

public class DatabaseConnection {
    private static final String DBDRIVER="com.mysql.jdbc.Driver";
    private static final String DBURL="jdbc:mysql://localhost:3306/lov";
    private static final String DBUSER="root";
    private static final String DBPASSWORD="997103";
    private Connection conn= null;
    public DatabaseConnection() throws Exception{
        try{
            Class.forName(DBDRIVER);
            this.conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);
        }catch (Exception e){
            throw e;
        }
    }

    public Connection getConnection() {
        return this.conn;
    }
    public void close()throws Exception{
        if(this.conn!=null){
            try{
                this.conn.close();
            }catch (Exception e){
                throw e;
            }
        }
    }
}

IUserDAO.java

public interface IUserDAO {
    public boolean findLogin(User user)throws Exception;
}

UserDAOImpl.java

public class UserDAOImpl implements IUserDAO {
    private Connection conn = null;
    private PreparedStatement pstmt = null;
    public UserDAOImpl(Connection conn){
        this.conn = conn;
    }

    @Override
    public boolean findLogin(User user) throws Exception {
        boolean flag = false;
        try{
            String sql = "SELECT name FROM user WHERE userid=? AND password=?";
            this.pstmt = this.conn.prepareStatement(sql);
            this.pstmt.setString(1,user.getUserid());
            this.pstmt.setString(2,user.getPassword());
            ResultSet rs = this.pstmt.executeQuery();
            if(rs.next()){
                user.setName(rs.getString(1));
                flag = true;
            }
        }catch (Exception e){
            throw  e;
        }finally {
            if(this.pstmt!=null){
                try {
                    this.pstmt.close();
                }catch (Exception e){
                    throw e;
                }
            }
        }
        return flag;
    }
}

UserDAOProxy.java

public class UserDAOProxy implements IUserDAO {
    private DatabaseConnection dbc = null;
    private IUserDAO dao = null;
    public  UserDAOProxy(){
        try{
            this.dbc = new DatabaseConnection();

        }catch (Exception e){
            e.printStackTrace();
        }
        this.dao = new UserDAOImpl(this.dbc.getConnection());
    }
    @Override
    public boolean findLogin(User user) throws Exception {
        boolean flag = false;
        try {
            flag = this.dao.findLogin(user);
        }catch (Exception e){
            throw e;
        }finally {
            this.dbc.close();
        }
        return flag;
    }
}

DAOFactory.java

public class DAOFactory {
    public static IUserDAO getIUserDAOInstance(){
        return new UserDAOProxy();
    }
}

LoginServlet.java

public class LoginServlet extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException,IOException{
        String path = "/";
        String userid = req.getParameter("userid");
        String userpass = req.getParameter("userpass");
        List<String> info = new ArrayList<String>();
        if(userid == null || "".equals(userid)){
            info.add("用户id不能为空");
        }
        if (userpass == null || "".equals(userpass)){
            info.add("密码不能为空");
        }
        if (info.size() == 0){
            User user = new User();
            user.setUserid(userid);
            user.setPassword(userpass);
            try{
                if(DAOFactory.getIUserDAOInstance().findLogin(user)){
                    info.add(user.getUserid());
                }else {
                    info.add("failure");
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        req.setAttribute("info",info);
        req.getRequestDispatcher(path).forward(req,resp);

    }
    public void doPost(HttpServletRequest req,HttpServletResponse resp)throws  ServletException,IOException{
        this.doGet(req,resp);
    }
}

login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Iterator" %>

<html>
<head>
    <title>Login Home</title>
</head>
<script language="JavaScript">
    function validate(f) {
        if(!(/^\w{5,15}$/.text(f.userid.value))){
            alert("id-5~15");
            f.userid.focus();
            return false;
        }
        if(!(/^\w{5,15}$/.text(f.userpass.value))){
            alert("pass-5~15");
            f.userpass.focus();
            return false;
        }
        return true;

    }
</script>
<body>

<h2>Login</h2>
<%
    request.setCharacterEncoding("GBK");
%>
<%
    List<String> info = (List<String >)request.getAttribute("info");
    if (info!= null){
        Iterator<String>iter = info.iterator();
        while (iter.hasNext()){
%>
<h4><%=iter.next()%></h4>
<%
    }}
%>

<form action="LoginServlet" method="post" onsubmit="return validate(this)">
    UserId:<input type="text" name="userid"><br>
    UserPass:<input type="password" name="userpass"><br>
    <input type="submit" value="login">
    <input type="reset" value="reset">
</form>

</body>
</html>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值