后台管理(用户管理)
1.主界面(userList.jsp)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="com.weihai.dao.*" %>'
<%@ page import="com.weihai.factory.*" %>
<%@ page import="java.util.*" %>
<%@ page import="com.weihai.bean.*" %>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta name="renderer" content="webkit">
<title></title>
<link rel="stylesheet" href="css/pintuer.css">
<link rel="stylesheet" href="css/admin.css">
<script src="js/jquery.js"></script>
<script src="js/pintuer.js"></script>
</head>
<body>
<%
int pageCount = 0;//总的页数
int pageNo=1;
pageNo = ((Integer)request.getAttribute("pageNo")).intValue();
//获取总的页数
pageCount = ((Integer)request.getAttribute("pageCount")).intValue();
//获取当前页的记录
ArrayList<User> users=(ArrayList<User>)request.getAttribute("users");
//System.out.println(users.size());
%>
<form method="post" action="" id="listform">
<div class="panel admin-panel">
<div class="panel-head"><strong class="icon-reorder"> 内容列表</strong> <a href="" style="float:right; display:none;">添加字段</a></div>
<div class="padding border-bottom">
<ul class="search" style="padding-left:10px;">
<li> <a class="button border-main icon-plus-square-o" href="add.jsp"> 添加用户</a> </li>
<li>
<input type="text" placeholder="请输入搜索关键字" name="keywords" class="input" style="width:250px; line-height:17px;display:inline-block" />
<a href="UserServlet?operate=queryByPage" class="button border-main icon-search" > 搜索</a></li>
</ul>
</div>
<table class="table table-hover text-center">
<tr>
<th width="100" style="text-align:left; padding-left:20px;">ID</th>
<th>用户名</th>
<th>密码</th>
<th width="310">操作</th>
</tr>
<volist name="list" id="vo">
<%
for(int i=0;i<users.size();i++){
User user = users.get(i);
%>
<tr>
<td style="text-align:left; padding-left:20px;"><input type="checkbox" name="id[]" value="" />
<%=user.getId() %></td>
<td><%=user.getUsername() %></td>
<td><%=user.getPassword() %></td>
<td><div class="button-group">
<a class="button border-main" href="UserServlet?operate=queryById&id=<%=user.getId()%>"><span class="icon-edit"></span> 修改</a>
<a class="button border-red" href="UserServlet?operate=delete&id=<%=user.getId() %>" onclick="return del(1,1,1)"><span class="icon-trash-o"></span> 删除</a> </div></td>
</tr>
<tr>
<%} %>
<td style="text-align:left; padding:19px 0;padding-left:20px;"><input type="checkbox" id="checkall"/>
全选 </td>
<td colspan="7" style="text-align:left;padding-left:20px;"><a href="javascript:void(0)" class="button border-red icon-trash-o" style="padding:5px 15px;" onclick="DelSelect()"> 删除</a> <a href="javascript:void(0)" style="padding:5px 15px; margin:0 10px;" class="button border-blue icon-edit" onclick="sorts()"> 排序</a> 操作:
<select name="ishome" style="padding:5px 15px; border:1px solid #ddd;" onchange="changeishome(this)">
<option value="">首页</option>
<option value="1">是</option>
<option value="0">否</option>
</select>
<select name="isvouch" style="padding:5px 15px; border:1px solid #ddd;" onchange="changeisvouch(this)">
<option value="">推荐</option>
<option value="1">是</option>
<option value="0">否</option>
</select>
<select name="istop" style="padding:5px 15px; border:1px solid #ddd;" onchange="changeistop(this)">
<option value="">置顶</option>
<option value="1">是</option>
<option value="0">否</option>
</select>
移动到:
<select name="movecid" style="padding:5px 15px; border:1px solid #ddd;" onchange="changecate(this)">
<option value="">请选择分类</option>
<option value="">产品分类</option>
<option value="">产品分类</option>
<option value="">产品分类</option>
<option value="">产品分类</option>
</select>
<select name="copynum" style="padding:5px 15px; border:1px solid #ddd;" onchange="changecopy(this)">
<option value="">请选择复制</option>
<option value="5">复制5条</option>
<option value="10">复制10条</option>
<option value="15">复制15条</option>
<option value="20">复制20条</option>
</select></td>
</tr>
<tr>
<td colspan="8"><div class="pagelist">
<%if(pageNo>1){ %>
<a href="UserServlet?operate=queryByPage&pageNo=<%= pageNo-1%>">上一页</a>
<%} %>
<%
for(int i=1;i<=pageCount;i++){
%>
<a href="UserServlet?operate=queryByPage&pageNo=<%=i%>"><%=i %></a>
<%
}
%>
<%if(pageNo<pageCount){ %>
<a href="UserServlet?operate=queryByPage&pageNo=<%= pageNo+1%>">下一页</a>
<a href="UserServlet?operate=queryByPage&pageNo=<%=pageCount%>">尾页</a>
<%} %>
</div></td>
</tr>
</table>
</div>
</form>
<script type="text/javascript">
//搜索
function changesearch(){
}
//单个删除
function del(id,mid,iscid){
if(confirm("您确定要删除吗?")){
return true;
}
else{
return false;
}
}
//全选
$("#checkall").click(function(){
$("input[name='id[]']").each(function(){
if (this.checked) {
this.checked = false;
}
else {
this.checked = true;
}
});
})
//批量删除
function DelSelect(){
var Checkbox=false;
$("input[name='id[]']").each(function(){
if (this.checked==true) {
Checkbox=true;
}
});
if (Checkbox){
var t=confirm("您确认要删除选中的内容吗?");
if (t==false) return false;
$("#listform").submit();
}
else{
alert("请选择您要删除的内容!");
return false;
}
}
//批量排序
function sorts(){
var Checkbox=false;
$("input[name='id[]']").each(function(){
if (this.checked==true) {
Checkbox=true;
}
});
if (Checkbox){
$("#listform").submit();
}
else{
alert("请选择要操作的内容!");
return false;
}
}
//批量首页显示
function changeishome(o){
var Checkbox=false;
$("input[name='id[]']").each(function(){
if (this.checked==true) {
Checkbox=true;
}
});
if (Checkbox){
$("#listform").submit();
}
else{
alert("请选择要操作的内容!");
return false;
}
}
//批量推荐
function changeisvouch(o){
var Checkbox=false;
$("input[name='id[]']").each(function(){
if (this.checked==true) {
Checkbox=true;
}
});
if (Checkbox){
$("#listform").submit();
}
else{
alert("请选择要操作的内容!");
return false;
}
}
//批量置顶
function changeistop(o){
var Checkbox=false;
$("input[name='id[]']").each(function(){
if (this.checked==true) {
Checkbox=true;
}
});
if (Checkbox){
$("#listform").submit();
}
else{
alert("请选择要操作的内容!");
return false;
}
}
//批量移动
function changecate(o){
var Checkbox=false;
$("input[name='id[]']").each(function(){
if (this.checked==true) {
Checkbox=true;
}
});
if (Checkbox){
$("#listform").submit();
}
else{
alert("请选择要操作的内容!");
return false;
}
}
//批量复制
function changecopy(o){
var Checkbox=false;
$("input[name='id[]']").each(function(){
if (this.checked==true) {
Checkbox=true;
}
});
if (Checkbox){
var i = 0;
$("input[name='id[]']").each(function(){
if (this.checked==true) {
i++;
}
});
if(i>1){
alert("只能选择一条信息!");
$(o).find("option:first").prop("selected","selected");
}else{
$("#listform").submit();
}
}
else{
alert("请选择要复制的内容!");
$(o).find("option:first").prop("selected","selected");
return false;
}
}
</script>
</body>
</html>
2.添加界面(addUser.jsp)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="com.weihai.bean.User" %>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta name="renderer" content="webkit">
<title></title>
<link rel="stylesheet" href="css/pintuer.css">
<link rel="stylesheet" href="css/admin.css">
<script src="js/jquery.js"></script>
<script src="js/pintuer.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function(){
$("#username").focus();
});
var hadoop = function(){
// 异步验证用户是否重复
if($.trim($("#username").val())==""){
$("#message").html("用户名不能为空");
$("#username").focus();
}else{
$.ajax({
type: "post",
url: "UserServlet?operate=queryByName",
data: "username=" + $("#username").val(),
success: function(message){
$("#message").html(message);
}
});
}
};
</script>
</head>
<body>
<div class="panel admin-panel">
<div class="panel-head" id="add"><strong><span class="icon-pencil-square-o"></span>增加用户</strong></div>
<div class="body-content">
<form method="post" class="form-x" action="UserServlet?operate=add">
<div class="form-group">
<div class="label">
<label>用户名:</label>
</div>
<div class="field">
<input type="text" class="input w50" value="" id="username" name="username" onblur="hadoop()" data-validate="required:请输入用户名" />
<div class="tips" id="message"></div>
</div>
</div>
<div class="form-group">
<div class="label">
<label>密码:</label>
</div>
<div class="field">
<input type="password" class="input w50" value="" name="password" data-validate="required:请输入密码" />
<div class="tips"></div>
</div>
</div>
<div class="form-group">
<div class="label">
<label></label>
</div>
<div class="field">
<button class="button bg-main icon-check-square-o" type="submit"> 提交</button>
</div>
</div>
</form>
</div>
</div>
</body></html>
3.(check.jsp)
<%@page import="com.weihai.factory.DaoFactory"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %>
<%@page import="com.weihai.dao.*" %>
<!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>
<%
//1.获取用户输入的用户名和密码
String username=request.getParameter("username");
String password=request.getParameter("password");
System.out.print("aaa"+username);
//2.访问数据库,查询用户名密码
//使用JavaBean访问数据库,程序的可读性好,逻辑清晰
UserDao userDao=DaoFactory.getUserDao();
boolean t=userDao.login(username, password);
if(t){
//用户名密码正确,通过向客户端写cookie记住登陆的用户名和密码
//1.创建COOKie对象
Cookie cookie1 = new Cookie("username",username);
Cookie cookie2 = new Cookie("password",password);
//2.设置cookie的有效期
cookie1.setMaxAge(24*60*60);
cookie2.setMaxAge(24*60*60);
//3.向客户端写cookie
response.addCookie(cookie1);
response.addCookie(cookie2);
%>
<jsp:forward page="index.jsp"/>
<%}else{
response.sendRedirect("fail.jsp");
}
%>
</body>
</html>
4.修改密码(updatePass.jsp)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="com.weihai.bean.User" %>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta name="renderer" content="webkit">
<title></title>
<link rel="stylesheet" href="css/pintuer.css">
<link rel="stylesheet" href="css/admin.css">
<script src="js/jquery.js"></script>
<script src="js/pintuer.js"></script>
</head>
<body>
<% User user = (User)request.getAttribute("user");
%>
<div class="panel admin-panel">
<div class="panel-head" id="add"><strong><span class="icon-pencil-square-o"></span>增加内容</strong></div>
<div class="body-content">
<form method="post" class="form-x" action="LoginServlet?operate=updateuser&id=<%=user.getUsername()%>">
<div class="form-group">
<div class="label">
<label>密码:</label>
</div>
<div class="field">
<input type="password" class="input w50" value="<%=user.getPassword() %>" name="password" data-validate="required:请输入密码" />
<div class="tips"></div>
</div>
</div>
<div class="form-group">
<div class="label">
<label></label>
</div>
<div class="field">
<button class="button bg-main icon-check-square-o" type="submit"> 提交</button>
</div>
</div>
</form>
</div>
</div>
</body></html>
创建Java文件
1.创建User.java(com.weihai.bean包)
package com.weihai.bean;
import java.io.Serializable;
public class User implements Serializable{
private static final long serialVersionUID = 1L;
private int id;
private String username;
private String password;
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 getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
2.DBConn.java(com.weihai.conn包)
package com.weihai.conn;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBConn {
public static final String dbDriver = "com.mysql.jdbc.Driver";
public static final String dbURL = "jdbc:mysql://localhost:3306/db_shop?useSSL=false";
public static final String dbUsername = "root";
public static final String dbPwd = "root";
//连接数据库
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName(dbDriver);
}catch (ClassNotFoundException e) {
e.printStackTrace();
}
//创建数据库的连接
try {
conn = (Connection) DriverManager.getConnection(dbURL,dbUsername,dbPwd);
}catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
//关闭与数据库连接
public static void close(Connection conn,PreparedStatement pt,ResultSet rs){
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (pt != null) {
try {
pt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void close(Connection conn,PreparedStatement pt){
if (pt != null) {
try {
pt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
3.UserDao.java(com.weihai.dao包)
package com.weihai.dao;
import java.util.ArrayList;
import com.weihai.bean.User;
public interface UserDao {
boolean login(String username, String password);
boolean insert(User user);
boolean delete(int id);
boolean queryByUsername(String username);
ArrayList<User> queryAll();
User queryById(int id);
boolean update(User user);
boolean updateuser(User user);
ArrayList<User> queryByPage(int pageNo);
int getRecordCount();
int getPageCount();
}
4.UserDaoImpl(com.weihai.dao.impl包)
package com.weihai.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import com.weihai.bean.User;
import com.weihai.conn.DBConn;
import com.weihai.dao.UserDao;
import com.weihai.factory.DaoFactory;
public class UserDaoImpl implements UserDao {
Connection conn = null;
PreparedStatement pt = null;
ResultSet rs = null;
public final int pageSize = 5;
/* (non-Javadoc)
* @see com.soft1.dao.UserDao#login(java.lang.String, java.lang.String)
*/
@Override
public boolean login(String username,String password) {
boolean t = false;
conn = DBConn.getConnection();
//3.创建语句容器
//Statement stmt = conn.createStatement();
//4.执行SQL语句
String sql = "select * from admin_info where name=? and pwd =?";
try {
pt =conn.prepareStatement(sql);
pt.setString(1,username);
pt.setString(2,password);
rs = pt.executeQuery();
if(rs.next()) {
t =true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DBConn.close(conn, pt, rs);
return t;
}
/* (non-Javadoc)
* @see com.soft1.dao.UserDao#insert(com.soft1.bean.User)
*/
@Override
public boolean insert(User user) {
boolean t = false;
int n=0;
conn = DBConn.getConnection();
//3.创建语句容器
//Statement stmt = conn.createStatement();
//4.执行SQL语句
String sql = "insert into admin_info(name,pwd)values(?,?)";
try {
pt =conn.prepareStatement(sql);
pt.setString(1,user.getUsername());
pt.setString(2,user.getPassword());
n = pt.executeUpdate();
if(n > 0) {
t =true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DBConn.close(conn, pt, rs);
return t;
}
@Override
public boolean delete(int id) {
// TODO Auto-generated method stub
boolean t = false;
int n=0;
conn = DBConn.getConnection();
//3.创建语句容器
//Statement stmt = conn.createStatement();
//4.执行SQL语句
String sql = "delete from admin_info where id=?";
try {
pt =conn.prepareStatement(sql);
pt.setInt(1,id);
n = pt.executeUpdate();
if(n > 0) {
t =true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DBConn.close(conn, pt, rs);
return t;
}
@Override
public boolean queryByUsername(String username) {
// TODO Auto-generated method stub
boolean t = false;
conn = DBConn.getConnection();
//3.创建语句容器
//Statement stmt = conn.createStatement();
//4.执行SQL语句
String sql = "select * from admin_info where name=? ";
try {
pt =conn.prepareStatement(sql);
pt.setString(1,username);
rs = pt.executeQuery();
if(rs.next()) {
t =true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DBConn.close(conn, pt, rs);
return t;
}
@Override
public ArrayList<User> queryAll() {
// TODO Auto-generated method stub
ArrayList<User> users =new ArrayList<User>();
conn = DBConn.getConnection();
//3.创建语句容器
//Statement stmt = conn.createStatement();
//4.执行SQL语句
String sql = "select * from admin_info ";
try {
pt =conn.prepareStatement(sql);
rs = pt.executeQuery();
while(rs.next()) {
User user = new User();
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("name"));
user.setPassword(rs.getString("pwd"));
users.add(user);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DBConn.close(conn, pt, rs);
return users;
}
@Override
public User queryById(int id) {
// TODO Auto-generated method stub
User user = new User();
conn = DBConn.getConnection();
//3.创建语句容器
//Statement stmt = conn.createStatement();
//4.执行SQL语句
String sql = "select * from admin_info where id=?";
try {
pt =conn.prepareStatement(sql);
pt.setInt(1,id);
rs = pt.executeQuery();
if(rs.next()) {
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("name"));
user.setPassword(rs.getString("pwd"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DBConn.close(conn, pt, rs);
return user;
}
@Override
public boolean update(User user) {
// TODO Auto-generated method stub
boolean t = false;
int n=0;
conn = DBConn.getConnection();
//3.创建语句容器
//Statement stmt = conn.createStatement();
//4.执行SQL语句
String sql = "update admin_info set name=?,pwd=? where id=?";
try {
pt =conn.prepareStatement(sql);
pt.setString(1,user.getUsername());
pt.setString(2,user.getPassword());
pt.setInt(3,user.getId());
n = pt.executeUpdate();
if(n > 0) {
t =true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DBConn.close(conn, pt, rs);
return t;
}
@Override
public boolean updateuser(User user) {
// TODO Auto-generated method stub
boolean t = false;
int n=0;
conn = DBConn.getConnection();
//3.创建语句容器
//Statement stmt = conn.createStatement();
//4.执行SQL语句
String sql = "update admin_info set pwd=? where username=?";
try {
pt =conn.prepareStatement(sql);
pt.setString(1,user.getPassword());
pt.setString(2,user.getUsername());
n = pt.executeUpdate();
if(n > 0) {
t =true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DBConn.close(conn, pt, rs);
return t;
}
@Override
/**
* 分页查询
*/
public ArrayList<User> queryByPage(int pageNo) {
// TODO Auto-generated method stub
ArrayList<User> users =new ArrayList<User>();
conn = DBConn.getConnection();
//3.创建语句容器
//Statement stmt = conn.createStatement();
//4.执行SQL语句
String sql = "select * from admin_info limit ?,?";
try {
pt =conn.prepareStatement(sql);
pt.setInt(1, (pageNo-1)*pageSize);
pt.setInt(2, pageSize);
rs = pt.executeQuery();
while(rs.next()) {
User user = new User();
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("name"));
user.setPassword(rs.getString("pwd"));
users.add(user);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DBConn.close(conn, pt, rs);
return users;
}
//单元测试
public static void main(String[] args) {
UserDao userDao=DaoFactory.getUserDao();
ArrayList<User> users=userDao.queryByPage(2);
for(int i=0;i<users.size();i++){
User user=new User();
user=users.get(i);
System.out.println(user.getUsername());
}
}
@Override
/**
* 查询总的记录数
*/
public int getRecordCount() {
// TODO Auto-generated method stub
int recordCount=0;
conn = DBConn.getConnection();
//3.创建语句容器
//Statement stmt = conn.createStatement();
//4.执行SQL语句
String sql = "select count(*) as aa from admin_info ";
try {
pt =conn.prepareStatement(sql);
rs = pt.executeQuery();
if(rs.next()){
recordCount = rs.getInt(1);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DBConn.close(conn, pt, rs);
return recordCount;
}
@Override
/**
* 返回总的页数
*/
public int getPageCount() {
// TODO Auto-generated method stub
int recordCount = getRecordCount();
return (recordCount+pageSize-1)/pageSize;
}
}
5.工厂DaoFactory.java(com.weihai.factory)
package com.weihai.factory;
import com.weihai.dao.GoodDao;
import com.weihai.dao.UserDao;
import com.weihai.dao.impl.GoodDaoImpl;
import com.weihai.dao.impl.UserDaoImpl;
/**
* 采用工厂模式创建dao的对象
* @author x501
*
*/
public class DaoFactory {
public static UserDao getUserDao() {
return new UserDaoImpl();
}}
6.UserServlet.java(com.weihai.servlet)
package com.weihai.servlet;
import java.io.IOException;
import java.util.ArrayList;
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.weihai.bean.User;
import com.weihai.dao.UserDao;
import com.weihai.factory.DaoFactory;
/**
* Servlet implementation class UserServlet
*/
@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public UserServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//response.getWriter().append("Served at: ").append(request.getContextPath());
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String operate = request.getParameter("operate");
UserDao userDao=DaoFactory.getUserDao();
//查询所有记录
if("queryAll".equals(operate)) {
ArrayList<User> users=userDao.queryAll();
//把数据users封装到request对象
request.setAttribute("users", users);
request.getRequestDispatcher("userList.jsp").forward(request, response);
}
//删除记录
if("delete".equals(operate)) {
String tId = request.getParameter("id");
int id = 0;
if(tId!=null&&!tId.isEmpty()) {
id = Integer.parseInt(tId);
}
boolean t = userDao.delete(id);
if(t) {
response.sendRedirect("UserServlet?operate=queryAll");
}else {
response.sendRedirect("error.jsp");
}
}
//添加
if("add".equals(operate)) {
//获取用户输入的用户名和密码
String username = request.getParameter("username");
String password = request.getParameter("password");
User user = new User();
user.setUsername(username);
user.setPassword(password);
boolean t = userDao.insert(user);
if(t) {
response.sendRedirect("UserServlet?operate=queryAll");
}else {
response.sendRedirect("error_add.jsp");
}
}
//按照Id查找用户
if("queryById".equals(operate)) {
String tId = request.getParameter("id");
int id = 0;
if(tId!=null&&!tId.isEmpty()) {
id = Integer.parseInt(tId);
}
User user=userDao.queryById(id);
//把数据users封装到request对象
request.setAttribute("user", user);
request.getRequestDispatcher("update.jsp").forward(request, response);
}
//修改用户信息
if("update".equals(operate)) {
String tId = request.getParameter("id");
String username=request.getParameter("username");
String password=request.getParameter("password");
int id = 0;
if(tId!=null&&!tId.isEmpty()) {
id = Integer.parseInt(tId);
}
User user=new User();
user.setId(id);
user.setUsername(username);
user.setPassword(password);
boolean t = userDao.update(user);
if(t) {
response.sendRedirect("UserServlet?operate=queryAll");
}else {
response.sendRedirect("error_update.jsp");
}
//修改当前用户信息
if("updateuser".equals(operate)) {
String passworduser=request.getParameter("password");
}
User user1=new User();
user1.setPassword(password);
boolean d = userDao.update(user1);
if(d) {
response.sendRedirect("UserServlet?operate=queryAll");
}else {
response.sendRedirect("error_update.jsp");
}
}
//分页查询
if("queryByPage".equals(operate)) {
int pageNo = 1;
int pageCount;
String tPageNo = request.getParameter("pageNo");
if(tPageNo!=null&&!tPageNo.isEmpty()) {
pageNo = Integer.parseInt(tPageNo);
}
//ArrayList<User> users=userDao.queryByPage(pageNo,);
ArrayList<User> users=userDao.queryByPage(pageNo);
pageCount = userDao.getPageCount();
//把当前页的记录users封装到request对象
request.setAttribute("users", users);
//把总的页数封装到request对象,传递给userList.jsp
request.setAttribute("pageCount", pageCount);
//把上一次选中的页码传给userList.jsp
request.setAttribute("pageNo", pageNo);
request.getRequestDispatcher("userList.jsp").forward(request, response);
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}