user_login_jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
UserLoginServlet
package service;
import model.User;
import service.UserService;
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 java.io.IOException;
@WebServlet(name = "user_login",urlPatterns = "/user_login")
public class UserLoginServlet extends HttpServlet {
private UserService uService = new UserService();
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String ue = request.getParameter("ue");
String password = request.getParameter("password");
User user = uService.login(ue, password);
if(user==null) {
request.setAttribute("failMsg", "用户名、邮箱或者密码错误,请重新登录!");
request.getRequestDispatcher("/user_login.jsp").forward(request, response);
}else {
request.getSession().setAttribute("user", user);
request.getRequestDispatcher("/user_center.jsp").forward(request, response);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
User
package model;
public class User {
private int id;
private String username;
private String email;
private String password;
private String name;
private String phone;
private String address;
private boolean isadmin=false;
private boolean isvalidate=false;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", email=" + email + ", password=" + password + ", name="
+ name + ", phone=" + phone + ", address=" + address + ", isadmin=" + isadmin + ", isvalidate="
+ isvalidate + "]";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public boolean isIsadmin() {
return isadmin;
}
public void setIsadmin(boolean isadmin) {
this.isadmin = isadmin;
}
public boolean isIsvalidate() {
return isvalidate;
}
public void setIsvalidate(boolean isvalidate) {
this.isvalidate = isvalidate;
}
public User(int id, String username, String email, String password, String name, String phone, String address,
boolean isadmin, boolean isvalidate) {
super();
this.id = id;
this.username = username;
this.email = email;
this.password = password;
this.name = name;
this.phone = phone;
this.address = address;
this.isadmin = isadmin;
this.isvalidate = isvalidate;
}
public User( String username, String email, String password, String name, String phone, String address) {
this.username = username;
this.email = email;
this.password = password;
this.name = name;
this.phone = phone;
this.address = address;
this.isadmin = false;
this.isvalidate = false;
}
public User() {
super();
}
}
user_register
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
UsrService
package service;
import dao.UserDao;
import model.Page;
import model.User;
import java.sql.SQLException;
import java.util.List;
public class UserService {
private UserDao uDao = new UserDao();
public boolean register(User user) {
try {
if(uDao.isUsernameExist(user.getUsername())) {
return false;
}
if(uDao.isEmailExist(user.getEmail())) {
return false;
}
uDao.addUser(user);
return true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
public User login(String ue,String password) {
User user=null;
try {
user = uDao.selectByUsernamePassword(ue, password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(user!=null) {
return user;
}
try {
user=uDao.selectByEmailPassword(ue, password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(user!=null) {
return user;
}
return null;
}
public User selectById(int id) {
User u=null;
try {
u = uDao.selectById(id);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return u;
}
public void updateUserAddress(User user) {
try {
uDao.updateUserAddress(user);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void updatePwd(User user) {
try {
uDao.updatePwd(user);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Page getUserPage(int pageNumber) {
Page p = new Page();
p.setPageNumber(pageNumber);
int pageSize = 7;
int totalCount = 0;
try {
totalCount = uDao.selectUserCount();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
p.SetPageSizeAndTotalCount(pageSize, totalCount);
List list=null;
try {
list = uDao.selectUserList( pageNumber, pageSize);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
p.setList(list);
return p;
}
public boolean delete(int id ) {
try {
uDao.delete(id);
return true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
}
UserDao
package dao;
import model.User;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import utils.DataSourceUtils;
import java.sql.SQLException;
import java.util.List;
public class UserDao {
public void addUser(User user) throws SQLException {
QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "insert into user(username,email,password,name,phone,address,isadmin,isvalidate) values(?,?,?,?,?,?,?,?)";
r.update(sql,user.getUsername(),user.getEmail(),user.getPassword(),user.getName(),user.getPhone(),user.getAddress(),user.isIsadmin(),user.isIsvalidate());
}
public boolean isUsernameExist(String username) throws SQLException {
QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select * from user where username = ?";
User u = r.query(sql, new BeanHandler(User.class),username);
if(u==null) {
return false;
}else {
return true;
}
}
public boolean isEmailExist(String email) throws SQLException {
QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select * from user where email = ?";
User u = r.query(sql, new BeanHandler(User.class),email);
if(u==null) {
return false;
}else {
return true;
}
}
public User selectByUsernamePassword(String username,String password) throws SQLException {
QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select * from user where username=? and password=?";
return r.query(sql, new BeanHandler(User.class),username,password);
}
public User selectByEmailPassword(String email,String password) throws SQLException {
QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select * from user where email=? and password=?";
return r.query(sql, new BeanHandler(User.class),email,password);
}
public User selectById(int id) throws SQLException {
QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select * from user where id=?";
return r.query(sql, new BeanHandler(User.class),id);
}
public void updateUserAddress(User user) throws SQLException {
QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
String sql ="update user set name = ?,phone=?,address=? where id = ?";
r.update(sql,user.getName(),user.getPhone(),user.getAddress(),user.getId());
}
public void updatePwd(User user) throws SQLException {
QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
String sql ="update user set password = ? where id = ?";
r.update(sql,user.getPassword(),user.getId());
}
public int selectUserCount() throws SQLException {
QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select count(*) from user";
return r.query(sql, new ScalarHandler()).intValue();
}
public List selectUserList(int pageNo, int pageSize) throws SQLException {
QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select * from user limit ?,?";
return r.query(sql, new BeanListHandler(User.class), (pageNo-1)*pageSize,pageSize );
}
public void delete(int id) throws SQLException {
QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "delete from user where id = ?";
r.update(sql,id);
}
}