工厂模式改进 实现登录

Posted mytangyh

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了工厂模式改进 实现登录相关的知识,希望对你有一定的参考价值。

User.java:

public class User {

private String id;
private String username;
private String password;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}

DBConn.java:

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

public class DBConn {
String driver="org.gjt.mm.mysql.Driver";
String url = "jdbc:mysql://localhost:3306/javaee";
String user="root";
String pass="";
Connection conn=null;
ResultSet rs=null;
public Connection GetConn() {
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user,pass);

} catch (Exception e) {
// TODO: handle exception
}

return conn;
}
public ResultSet Query(String sql) {

try {
DBConn db=new DBConn();
Connection conn=db.GetConn();
Statement s = conn.createStatement();
rs=s.executeQuery(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();

}
return rs;

}
}

ICheckLoginDomain.java:

public interface ICheckLoginDomain {
public boolean checkLogin(User u);
}

CheckLoginDomainImpl.java:

import java.sql.ResultSet;
import java.sql.SQLException;

import cn.edu.ujn.mvc.jdbc.DBConn;
import cn.edu.ujn.mvc.pojo.User;

public class CheckLoginDomainImpl implements ICheckLoginDomain {

@Override
public boolean checkLogin(User u) {
// TODO Auto-generated method stub
try {
DBConn conn = new DBConn();
String username = u.getUsername();
String pwd = u.getPassword();
String sql = "select * from user where username=‘" + username + "‘ and password=‘" + pwd + "‘";

ResultSet rs = conn.Query(sql);

if (rs.next()) {
return true;
} else {
return false;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}

}

LoginDomainFactory.java:

public class LoginDomainFactory {
public static ICheckLoginDomain getInstance() {
ICheckLoginDomain loginDomain=null;
loginDomain= new CheckLoginDomainImpl();
return loginDomain;
}

 

LoginServlet.java:

import java.io.IOException;
import java.sql.*;

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 com.sun.net.httpserver.Authenticator.Success;

import cn.edu.ujn.mvc.pojo.User;
@WebServlet("/login")
public class LoginServlet extends HttpServlet{
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
String username = req.getParameter("username");
String pwd = req.getParameter("pwd");
User u=new User();
u.setUsername(username);
u.setPassword(pwd);
ICheckLoginDomain dlDomain=LoginDomainFactory.getInstance();
if (dlDomain.checkLogin(u)) {
resp.sendRedirect("success.jsp");
} else {
resp.sendRedirect("login.jsp");
}

}
}


}

login.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head>
<title>MVC</title>
</head>
<body bgcolor="#E3E3E3">
<form action="login" method="post">
<table>
<caption>
用户登录
</caption>
<tr>
<td>
登录名:
</td>
<td>
<input type="text" name="username" size=”20” />
</td>
</tr>
<tr>
<td>
密码:
</td>
<td>
<input type="password" name="pwd" size=”21” />
</td>
</tr>
</table>
<input type="submit" value="登录" />
<input type="reset" value="重置" />
</form>
</body>
</html>

 

以上是关于工厂模式改进 实现登录的主要内容,如果未能解决你的问题,请参考以下文章

使用简单工厂来改进策略模式

工厂设计模式

JAVA设计模式——工厂方法模式案例实现

设计模式之工厂方法和抽象工厂

设计模式之抽象工厂模式

设计模式——抽象工厂模式