67,513
社区成员
发帖
与我相关
我的任务
分享
public class User {
private int id;
private String name;
private String password;
private String email;
private int age;
private Date birthday;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public User(){
}
public User(String name, String password){
this.name = name;
this.password = password;
}
//1、用户登录使用,判断是否存在用户
public User login() throws SQLException{
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
User user = null;
try{
conn=DBConnection.getConnection();
String sql = "select * from [user] where name = ? and password = ?";
ps = conn.prepareStatement(sql);
ps.setString(1,name);
ps.setString(2,password);
rs=ps.executeQuery();
while(rs.next()){
user = new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setPassword(rs.getString("password"));
user.setEmail(rs.getString("email"));
user.setAge(rs.getInt("age"));
user.setBirthday(rs.getDate("birthday"));
}
}finally{
DBConnection.close(rs, ps, conn);
}
return user;
}
}
jsp登录处理:login_do.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>登录处理页面</title>
</head>
<body>
<jsp:useBean id = "user" class = "jspjavabean.User"/>
<jsp:setProperty name = "user" property = "*" />
<%
user = user.login();
%>
if(user!=null){
session.setAttribute("user", user);
%>
<jsp:forward page = "user.jsp"/>
<%
}
else{
%>
<jsp:forward page = "loginFail.jsp"/>
<%
}
%>
</body>
</html>
jsp登录:login.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>用户登录</title>
</head>
<body>
<center>
<form action = "login_do.jsp" methon = "post">
<h2>用户登录</h2><br>
<table width="500" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td>用户名 :</td>
<td>
<input type = "text" name = "name"><br>
</td>
</tr>
<tr>
<td>密码: </td>
<td>
<input type = "password" password = "password"><br></td>
</td>
</tr>
<tr>
<td></td>
<td> <input type = "submit" value = "登录"> </td>
</tr>
</h3>
<tr>
<td></td>
</tr>
<tr>
<td><h4>还没注册?</td><td><a href = "register.jsp">马上注册</a></h4></td>
</tr>
</table>
</form>
</center>
</body>
</html>