Demo_01
package study;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
//模型层,用于处理登陆
public class Demo {
//从数据库获取数据,验证是否够为数据库拥有的账户
public static int login(UserMessage um){//返回值告诉view部分登陆是否成功
PreparedStatement ps = null;
Connection c = null;
int count = 0;
int flage = -1;//-1系统异常,0账号密码错误,1正常登陆
try {
Class.forName("com.mysql.jdbc.Driver");
c = DriverManager.getConnection("jdbc:mysql://localhost:3306/login","root","root");
String sql = "select count(*) from login where name=? and password=?";
ps = c.prepareStatement(sql);
ps.setString(1, um.getName());
ps.setString(2, um.getPassword());
ResultSet rs = ps.executeQuery();
if(rs.next()) {
count = rs.getInt(1);
}
if(count>0) {
flage = 1;
}else {
flage = 0;
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
flage = -1;
} catch (SQLException e) {
e.printStackTrace();
flage = -1;
}finally {
//关闭
try {
if(ps != null) {
ps.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(c != null) {
c.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return flage;
}
}
Loginservlet.java
package study;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//控制器层:接受view请求,并分发给model层处理
public class Loginservlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//接收view中用户输入的数据
request.setCharacterEncoding("utf-8");
String name = request.getParameter("username");
String password = request.getParameter("userpassword");
UserMessage um = new UserMessage(name, password);
//调用模型层的登录功能
int result = Demo.login(um);
if(result == 1) {//登陆成功到welcome页面
response.sendRedirect("welcome.jsp");
}else {//失败,返回登陆页面
response.sendRedirect("login.jsp");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
UserMessage.java
package study;
//获取用户在登陆页面输入的数据
public class UserMessage {
private String name;
private String password;
public UserMessage() {
}
public UserMessage(String name,String password) {
this.setName(name);
this.setPassword(password);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登陆</title>
</head>
<body>
<form action="Loginservlet" method="post"><!-- 有密码一般用post -->
用户名<input type="text" name="username"><br>
密码<input type="password" name="userpassword"><br>
<input type="submit" value="登陆">
</form>
</body>
</html>
welcome.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
welcome
</body>
</html>
2495

被折叠的 条评论
为什么被折叠?



