总共分为五个部分,
1.EL+JSTL前端JSP页面
2.servlet(传递数据)
3.dao(操作数据库)
4.javaBean(ORM)
5.数据库表设计
1.EL+JSTL前端JSP页面
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
用户权限管理if(session.getAttribute("login_user")!=null){
out.println("尊敬的"+session.getAttribute("login_user")+"
");
}
%>
if(request.getAttribute("tips")!=null){
out.println(request.getAttribute("tips")+"
");
}
%>
序号 | 用户名 | 权限 |
${current.index} | ${item.user_name} | ${item.authority} |
2.servlet
package com.user.manage.servlet;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.user.bean.VimUserInformationObject;
import com.user.dao.MultUserInformationDao;
public class ManagerUserAuthorityServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Constructor of the object.
*/
public ManagerUserAuthorityServlet() {
super();
}
/**
* Destruction of the servlet.
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* Initialization of the servlet.
*
* @throws ServletException
* if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
// 响应客户端请求的方法
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
String tips = "";
// Servlet本身并不输出响应到客户端,因此必须将请求转发到视图页面
RequestDispatcher rd;
try {
MultUserInformationDao userInformationDao = new MultUserInformationDao();
// 暂时将所有用户的数据放到登录Servlet处理,之后进行优化
List allUserAuthorityObjects = userInformationDao
.query();
request.setAttribute("allUserAuthority", allUserAuthorityObjects);
//
//
// 获取转发对象
rd = request
.getRequestDispatcher("/WEB-INF/jsp/user/manager_authority.jsp");
rd.forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
}
3.dao(操作数据库)
package com.user.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.base.dao.BaseDao;
import com.user.bean.VimUserInformationObject;
import com.util.DBconnect;
//前缀mult 表示多表联查
public class MultUserInformationDao extends BaseDao {
public MultUserInformationDao() {
super();
}
public List query() throws Exception {
List userInformationList = new ArrayList<>();
Connection conn = null;
PreparedStatement ps = null; // 创建PreparedStatement 对象
ResultSet rs = null;
String sql = new String(
"SELECT u.user_id, u.user_name, a.authority FROM user AS u, user_authority as A"
+ " WHERE a.user_id = u.user_id"
+ " ORDER BY u.user_id");
try {
conn = DBconnect.getConnection();
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
VimUserInformationObject userInformationObject = new VimUserInformationObject(
rs.getInt("user_id"), rs.getString("user_name"),
rs.getInt("authority"));
userInformationList.add(userInformationObject);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBconnect.closeAllConnection(rs, ps, conn);
}
return userInformationList;
}
public static void main(String[] args) {
MultUserInformationDao dao = new MultUserInformationDao();
try {
List userInformationList = (ArrayList) dao
.query();
if (!userInformationList.isEmpty()) {
for (VimUserInformationObject tmp : userInformationList) {
System.out.println("用户ID:" + tmp.getUser_id() + " 用户name:"
+ tmp.getUser_name() + " 用户权限:"
+ tmp.getAuthority());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}4.javaBean(ORM)
package com.user.bean;
//对应User表 和 User_authority 联查出来的数据, 前缀vim表示虚拟化
public class VimUserInformationObject {
private Integer user_id;
private String user_name;
private Integer authority;
{
user_id = 0;
user_name = "";
authority = 0;
}
public Integer getUser_id() {
return user_id;
}
public void setUser_id(Integer user_id) {
this.user_id = user_id;
}
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public Integer getAuthority() {
return authority;
}
public void setAuthority(Integer authority) {
this.authority = authority;
}
public VimUserInformationObject(Integer user_id, String user_name,
Integer authority) {
super();
this.user_id = user_id;
this.user_name = user_name;
this.authority = authority;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
5.数据库表设计
最终显示结果