package medical.web.servlet;
import medical.database.DbUtil;
import medical.domain.menu.Menu;
import medical.domain.role.Role;
import medical.domain.user.User;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* @author: tlgen
* @Discription:
*/
public class UserServlet extends BaseServlet
{
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);//使a标签发送post请求
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String servletPath = request.getServletPath();
String methodName = servletPath.substring(1);
methodName = methodName.substring(0,methodName.length()-3);
try {
//反射获取methName
Method method=getClass().getDeclaredMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
//利用反射调用方法
method.invoke(this,request, response);
}catch(Exception e) {
response.sendRedirect("error.jsp");
}
}
//显示所有用户信息
private void list(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
List<User> userList = new ArrayList<User>(); // 创建一个list集合,用于装user集合
Connection connection = DbUtil.getConn(); // 打开数据库连接
String sql = "select userid, username, pwd, fullname, status from t_user";
try {
PreparedStatement preparedStatement = connection.prepareStatement(sql);
ResultSet rs = preparedStatement.executeQuery();
while (rs.next()) {
Integer userid = rs.getInt("userid");
String username = rs.getString("username");
String pwd = rs.getString("pwd");
String fullname = rs.getString("fullname");
String status = rs.getString("status");
User user = new User(); /// 实例化user
user.setUserid(userid);
user.setUsername(username);
user.setPwd(pwd);
user.setFullname(fullname);
user.setStatus(status);
userList.add(user);
}
request.setAttribute("userList", userList);
request.getRequestDispatcher("/page/user/user_list.jsp").forward(request, response);
} catch (SQLException e) {
e.printStackTrace();
}
}
//添加用户
private void addUser(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
String pwd = request.getParameter("pwd");
String fullname = request.getParameter("fullname");
Integer status = Integer.parseInt(request.getParameter("status"));
Connection conn = DbUtil.getConn(); /// 获得数据库连接
String sql = "insert into t_user value(null,?,?,?,?);";
try {
PreparedStatement prtmt = conn.prepareStatement(sql); // 预编译
prtmt.setString(1, username); // 给第一个? 传值
prtmt.setString(2, pwd); // 给第二个? 传值
prtmt.setString(3, fullname);
prtmt.setInt(4, status);
int resInt = prtmt.executeUpdate(); // 执行sql返回影响行数;
request.setAttribute("resInt", resInt);
request.getRequestDispatcher("/list.do").forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
//修改用户信息
private void edit(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, SQLException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
Integer userid = Integer.parseInt(request.getParameter("userid"));
Connection conn = DbUtil.getConn();
String sql = "select * from t_user where userid = " + userid; // sql语句
PreparedStatement preparedStatement = conn.prepareStatement(sql);
ResultSet rs = preparedStatement.executeQuery();
try {
if (rs.next()) {
User user = new User();
user.setUserid(rs.getInt("userid"));
user.setUsername(rs.getString("username"));
user.setPwd(rs.getString("pwd"));
user.setFullname(rs.getString("fullname"));
user.setStatus(rs.getString("status"));
request.setAttribute("user", user);
request.getRequestDispatcher("/page/user/user_edit.jsp").forward(request, response);
}
}catch (SQLException e) {
e.printStackTrace();
}
}
//更新用户信息
private void update(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
Integer userid = Integer.parseInt(request.getParameter("userid"));
String username = request.getParameter("username");
String pwd = request.getParameter("pwd");
String fullname = request.getParameter("fullname");
Integer status = Integer.parseInt(request.getParameter("status"));
if (userid != null && username != null || pwd != null) {
// 获得数据库连接
Connection conn = DbUtil.getConn();
String sql = "update t_user set username=?,pwd=?, fullname=?, status=? where userid=" + userid; // sql语句
try {
PreparedStatement prtmt = conn.prepareStatement(sql); // 预编译语句
prtmt.setString(1, username); // 给第一个? 添加数据
prtmt.setString(2, pwd); // 给第二个? 添加数据
prtmt.setString(3, fullname); // 给第三个? 添加数据
prtmt.setInt(4, status);
int resInt = prtmt.executeUpdate(); // 执行sql返回影响行数;
request.setAttribute("resInt", resInt);
request.getRequestDispatcher("/list.do").forward(request, response);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//删除用户信息
private void delete(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String userid = request.getParameter("userid"); // 传入id;
if (userid != null) {
Connection conn = DbUtil.getConn();// 获得数据连接
String sql = "delete from t_user where userid = ?";
try {
PreparedStatement prtmt = conn.prepareStatement(sql); // 预编译
prtmt.setString(1, userid); // 给id赋值
int resInt = prtmt.executeUpdate(); // 执行sql返回影响行数;
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
用户权限管理系统:基于JSP、JavaBean和Servlet开发,包含436个文件,包括130个JavaScript文件、46个CSS样式文件、44个GIF图像文件、43个JAR包文件、34个PNG图像文件、34个JSP页面、24个Java字节码文件、24个Java类文件、16个XML配置文件和12个EOT字体文件。该项目是一个用户权限管理系统,采用原生JSP、JavaBean和Servlet技术,不包含DAO层的MVC设计模式,提供了精美的UI界面,旨在为用户提供高效、便捷的用户权限管理解决方案。
资源推荐
资源详情
资源评论
收起资源包目录
基于JSP、JavaBean和Servlet的用户权限设计源码 (436个子文件)
UserServlet.class 9KB
UserServlet.class 9KB
RoleServlet.class 7KB
RoleServlet.class 7KB
MenuServlet.class 7KB
MenuServlet.class 7KB
User.class 4KB
User.class 4KB
DbUtil.class 3KB
DbUtil.class 3KB
LoginServlet.class 3KB
LoginServlet.class 3KB
Menu.class 2KB
Menu.class 2KB
Role.class 2KB
Role.class 2KB
LogoutServlet.class 1KB
LogoutServlet.class 1KB
BaseServlet.class 1KB
BaseServlet.class 1KB
AuthorFilter.class 1KB
AuthorFilter.class 1KB
DbException.class 535B
DbException.class 535B
light-theme.css 262KB
light-theme.css 262KB
bootstrap.css 128KB
bootstrap.css 128KB
jquery-ui-1.10.0.custom.css 59KB
jquery-ui-1.10.0.custom.css 59KB
bootstrap-responsive.css 24KB
bootstrap-responsive.css 24KB
select2.css 16KB
select2.css 16KB
bootstrap-editable.css 14KB
bootstrap-editable.css 14KB
flags.css 12KB
flags.css 12KB
ui.dynatree.css 10KB
ui.dynatree.css 10KB
fullcalendar.css 10KB
fullcalendar.css 10KB
bootstrap-switch.css 8KB
bootstrap-switch.css 8KB
bootstrap-datetimepicker.min.css 6KB
bootstrap-datetimepicker.min.css 6KB
bootstrap-daterangepicker.css 5KB
bootstrap-daterangepicker.css 5KB
jquery.jgrowl.min.css 4KB
jquery.jgrowl.min.css 4KB
wizard.css 3KB
wizard.css 3KB
bootstrap-datatable.css 2KB
bootstrap-datatable.css 2KB
bootstrap-colorpicker.css 2KB
bootstrap-colorpicker.css 2KB
bootstrap-wysihtml5.css 2KB
bootstrap-wysihtml5.css 2KB
slidernav.css 2KB
slidernav.css 2KB
jquery.fileupload-ui.css 1KB
jquery.fileupload-ui.css 1KB
jquery.ui.1.10.0.ie.css 506B
jquery.ui.1.10.0.ie.css 506B
mention.css 316B
mention.css 316B
demo.css 288B
demo.css 288B
tabdrop.css 207B
tabdrop.css 207B
fontawesome-webfont.eot 29KB
fontawesome-webfont_2d2816fe.eot 29KB
fontawesome-webfont.eot 29KB
fontawesome-webfont_2d2816fe.eot 29KB
montserrat-regular-webfont.eot 18KB
montserrat-regular-webfont_2d2816fe.eot 18KB
montserrat-regular-webfont.eot 18KB
montserrat-regular-webfont_2d2816fe.eot 18KB
montserrat-bold-webfont_2d2816fe.eot 18KB
montserrat-bold-webfont.eot 18KB
montserrat-bold-webfont_2d2816fe.eot 18KB
montserrat-bold-webfont.eot 18KB
12.gif 9KB
12.gif 9KB
icons.gif 5KB
icons.gif 5KB
11.gif 4KB
11.gif 4KB
13.gif 3KB
13.gif 3KB
4.gif 3KB
4.gif 3KB
loading.gif 3KB
loading.gif 3KB
15.gif 2KB
15.gif 2KB
spinner.gif 2KB
loading.gif 2KB
1.gif 2KB
spinner.gif 2KB
共 436 条
- 1
- 2
- 3
- 4
- 5
资源评论
沐知全栈开发
- 粉丝: 5705
- 资源: 5216
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功