简单JSP+mvc登录实现



简单JSP+mvc登录实现

版权声明: https://blog.csdn.net/qq_34866380/article/details/79367934

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;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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;
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
IUserDAO.java

public interface IUserDAO {
public boolean findLogin(User user)throws Exception;
}
1
2
3
4
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;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
DAOFactory.java

public class DAOFactory {
public static IUserDAO getIUserDAOInstance(){
return new UserDAOProxy();
}
}
1
2
3
4
5
6
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);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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>
———————
作者:Lov_sb
来源:CSDN
原文:https://blog.csdn.net/qq_34866380/article/details/79367934
版权声明:本文为博主原创文章,转载请附上博文链接!