在WebRoot文件下得user文件夹创建showuser.jsp
<%@ page language="java" import="java.util.*,com.lzy.pojo.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".click").click(function(){
$(".tip").fadeIn(200);
});
$(".tiptop a").click(function(){
$(".tip").fadeOut(200);
});
$(".sure").click(function(){
$(".tip").fadeOut(100);
});
$(".cancel").click(function(){
$(".tip").fadeOut(100);
});
});
</script>
</head>
<body>
<div class="place">
<span>位置:</span>
<ul class="placeul">
<li><a href="main/main.jsp">首页</a></li>
<li><a href="main/main.jsp">管理信息</a></li>
<li><a href="user/userInfo.jsp">查看用户信息</a></li>
</ul>
</div>
<div class="rightinfo">
<table class="tablelist">
<thead>
<tr>
<th>用户ID<i class="sort"><img src="images/px.gif" /></i></th>
<th>用户名</th>
<th>密码</th>
<th>性别</th>
<th>年龄</th>
<th>出生日期</th>
</tr>
</thead>
<tbody>
<%
List<User> lu=(ArrayList<User>)request.getAttribute("lu");
for(User u:lu){
%>
<tr>
<td><%=u.getUid() %></td>
<td><%=u.getUname() %></td>
<td><%=u.getPwd()%></td>
<%
if("1".equals(u.getSex())){
%>
<td>男</td>
<%}else{ %>
<td>女</td>
<%} %>
<td><%=u.getAge() %></td>
<td><%=u.getBirth() %></td>
</tr>
<%} %>
</tbody>
</table>
<div class="tip">
<div class="tiptop"><span>提示信息</span><a></a></div>
<div class="tipinfo">
<span><img src="images/ticon.png" /></span>
<div class="tipright">
<p>是否确认对信息的修改 ?</p>
<cite>如果是请点击确定按钮 ,否则请点取消。</cite>
</div>
</div>
<div class="tipbtn">
<input name="" type="button" class="sure" value="确定" />
<input name="" type="button" class="cancel" value="取消" />
</div>
</div>
</div>
<script type="text/javascript">
$('.tablelist tbody tr:odd').addClass('odd');
</script>
</body>
</html>
在main文件夹下添加代码:
<div class="title">
<span><img src="images/leftico01.png" /></span>管理信息
</div>
<ul class="menuson">
<li><cite></cite><a href="user?oper=show" target="rightFrame">查看用户信息</a></li>
</ul>
</dd>
</dl>
第六行得href=“user?oper=show"(user为UserServlet的匿名)
当点击页面查看所有用户信息时返回UserServlet,此时opershow,在UserServlet中opershow,实现查看所有用户的操作
首先在UserServlet中添加
String oper=req.getParameter("oper");
if("login".equals(oper)){
//响应登陆操作
checkUserLogin(req,resp);
}else if("pwd".equals(oper)){
//调用修改密码功能
userChangePwd(req,resp);
}else if("show".equals(oper)){
//调用显示所有用户功能
userShow(req,resp);
}else if("reg".equals(oper)){
//响应注册操作
}else if("out".equals(oper)){
userOut(req,resp);
}else{
logger.debug("响应错误"+oper);
}
添加8 9 10行代码,然后添加userShow(req,resp)方法
//显示用户所有信息
private void userShow(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//处理请求
//调用service
List<User> lu=us.userShowService();
//判断
if(lu!=null){
//将查询得用户数据存储到request对象
req.setAttribute("lu", lu);
//请求转发
req.getRequestDispatcher("/user/showUSer.jsp").forward(req, resp);
return;
}
}
同时在userservice接口中添加方法userShowService();
package com.lzy.service;
import java.util.List;
import com.lzy.pojo.User;
public interface UserService {
/**
* 校验用户登录
* @param uname 用户名
* @param pwd 密码
* @return 返回查询到的用户信息
*/
User checkUserLoginService(String uname,String pwd);
/**
* 用户修改密码
* @param newPwd
* @param uid
* @return
*/
int userChangePwdService(String newPwd, int uid);
/**
* 获取所有得用户信息
* @return
*/
List<User> userShowService();
}
再在UserServiceImpl类中实现该方法
package com.lzy.service.imp;
import java.util.List;
import org.apache.log4j.Logger;
import com.lzy.dao.UserDao;
import com.lzy.dao.impl.UserDaoImpl;
import com.lzy.pojo.User;
import com.lzy.service.UserService;
public class UserServiceImpl implements UserService {
//声明日志对象
Logger logger=Logger.getLogger(UserServiceImpl.class);
//声明Dao层对象
UserDao ud=new UserDaoImpl();
//用户登录
@Override
public User checkUserLoginService(String uname, String pwd) {
//打印日志
logger.debug(uname+"发起登录请求");
User u=ud.checkUserLoginDao(uname, pwd);
//判断
if(u!=null){
logger.debug(uname+"登陆成功");
}else{
logger.debug(uname+"登录失败");
}
return u;
}
//修改用户密码
@Override
public int userChangePwdService(String newPwd, int uid) {
logger.debug(uid+":发起了请求.");
int index=ud.userChangePwdDao(newPwd,uid);
if(index>0){
logger.debug(uid+":修改密码成功");
}else{
logger.debug(uid+":修改密码失败");
}
return index;
}
//获取所有用户信息
@Override
public List<User> userShowService() {
List<User> lu=ud.userShowDao();
logger.debug("显示所有用户信息:"+lu);
return lu;
}
}
再在UserDao中创建方法userShowDao()方法
在UserDaoImpl类中实现该方法
package com.lzy.dao.impl;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.lzy.dao.UserDao;
import com.lzy.pojo.User;
public class UserDaoImpl implements UserDao{
@Override
public User checkUserLoginDao(String uname, String pwd) {
//声明jdbc对象
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
//声明变量
User u=null;
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//获取链接
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
//创建sql命令
String sql="select * from t_user where uname=? and pwd=?";
//创建sql命令对象
ps=conn.prepareStatement(sql);
//给占位符赋值
ps.setString(1, uname);
ps.setString(2, pwd);
//执行sql语句
rs=ps.executeQuery();
//遍历结果
while(rs.next()){
//给变量赋值
u=new User();
u.setUid(rs.getInt("uid"));
u.setPwd(rs.getString("pwd"));
u.setUname(rs.getString("uname"));
u.setSex(rs.getString("sex"));
u.setAge(rs.getInt("age"));
u.setBirth(rs.getString("birth"));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return u;
}
//根据用户ID修改用户密码
@Override
public int userChangePwdDao(String newPwd, int uid) {
//声明jdbc对象
Connection conn=null;
PreparedStatement ps=null;
//创建变量
int index=-1;
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//获取连接
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "123456");
//创建SQL命令
String sql="update t_user set pwd=? where uid=?";
//创建SQL命令对象
ps=conn.prepareStatement(sql);
//给占位符赋值
ps.setString(1, newPwd);
ps.setInt(2, uid);
//执行
index=ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally{//关闭资源
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//返回结果
return index;
}
//查看所有用户
@Override
public List<User> userShowDao() {
//声明jdbc对象
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
//声明变量
List<User>lu=null;
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//获取链接
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
//创建sql命令
String sql="select * from t_user";
//创建sql命令对象
ps=conn.prepareStatement(sql);
//执行sql语句
rs=ps.executeQuery();
//给集合赋值
lu=new ArrayList<User>();
//遍历结果
while(rs.next()){
//给变量赋值
User u=new User();
u.setUid(rs.getInt("uid"));
u.setPwd(rs.getString("pwd"));
u.setUname(rs.getString("uname"));
u.setSex(rs.getString("sex"));
u.setAge(rs.getInt("age"));
u.setBirth(rs.getString("birth"));
//将对象存储到集合中
lu.add(u);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return lu;
}
}
在UserDaoImpl中将查询到的所有用户集合到List中,并返回