【发布时间】:2019-10-26 13:27:18
【问题描述】:
我有一个 jsp+servlets Web 应用程序。我希望 ArrayList 在所有 jsp 和 servlet 页面中都可用,这对于每个登录的用户都不同。当用户登录到网络应用程序时,我为用户分配了一些角色,根据角色在网络应用程序中为他分配了一些权限。我的意思是说 Web 应用程序会根据用户角色和权限进行转换。例如,某些操作可能仅适用于某些角色等。所以我想在登录时(仅一次)运行查询并将所有权限保存在数组列表中,并在所有 jsp 和 servlet 页面中获取该数组列表。我该怎么做。
public class Role implements SingleThreadModel{
Connection connection = null;
Statement statement = null;
IST ist;
int user_id;
public Role(int user_id, Connection connection) {
try {
this.user_id = user_id;
this.connection = connection;
statement = connection.createStatement();
ist = new IST();
} catch (SQLException ex) {
Logger.getLogger(Role.class.getName()).log(Level.SEVERE, null, ex);
}
}
public List<String> getPermissionScreens() {
List<String> myList = new ArrayList<>();
try {
String sql = "";
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
myList.add(resultSet.getString("screen_name"));
}
} catch (SQLException ex) {
Logger.getLogger(Role.class.getName()).log(Level.SEVERE, null, ex);
}
return myList;
}
public List<String> getPermissions(String screen) {
List<String> myList = new ArrayList<>();
try {
String sql = "";
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
myList.add(resultSet.getString("permission_name"));
}
} catch (SQLException ex) {
Logger.getLogger(Role.class.getName()).log(Level.SEVERE, null, ex);
}
return myList;
}
}
【问题讨论】:
-
您可以在会话中保存该数组列表。会话特定于用户,并且在用户登录时有效/有效。一旦用户注销,会话将/必须关闭其对应的会话。