如何使ArrayList在web应用程序的所有jsp和servlet页面中可用
我有一个jsp+servlets web应用程序。我希望ArrayList在所有jsp和servlet页面中都可用,每个登录用户的页面都不同。当用户登录到web应用程序时,我为用户分配了一些角色,根据角色,在web应用程序中为用户分配了一些权限。我的意思是,web应用程序根据用户角色和权限进行转换。例如,某些操作可能仅适用于某些角色等。因此,我希望在登录时运行一个查询(仅一次),并在数组列表中保存所有权限,并在所有jsp和servlet页面中获取该数组列表。我该怎么做呢如何使ArrayList在web应用程序的所有jsp和servlet页面中可用,jsp,servlets,Jsp,Servlets,我有一个jsp+servlets web应用程序。我希望ArrayList在所有jsp和servlet页面中都可用,每个登录用户的页面都不同。当用户登录到web应用程序时,我为用户分配了一些角色,根据角色,在web应用程序中为用户分配了一些权限。我的意思是,web应用程序根据用户角色和权限进行转换。例如,某些操作可能仅适用于某些角色等。因此,我希望在登录时运行一个查询(仅一次),并在数组列表中保存所有权限,并在所有jsp和servlet页面中获取该数组列表。我该怎么做呢 public class
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;
}
}
公共类角色实现SingleThreadModel{
连接=空;
Statement=null;
主义者;
int用户标识;
公共角色(int user_id,连接){
试一试{
this.user\u id=user\u id;
这个连接=连接;
statement=connection.createStatement();
ist=新的ist();
}catch(SQLException-ex){
Logger.getLogger(Role.class.getName()).log(Level.SEVERE,null,ex);
}
}
公共列表getPermissionScreens(){
List myList=new ArrayList();
试一试{
字符串sql=“”;
ResultSet ResultSet=statement.executeQuery(sql);
while(resultSet.next()){
添加(resultSet.getString(“屏幕名称”);
}
}catch(SQLException-ex){
Logger.getLogger(Role.class.getName()).log(Level.SEVERE,null,ex);
}
返回myList;
}
公共列表获取权限(字符串屏幕){
List myList=new ArrayList();
试一试{
字符串sql=“”;
ResultSet ResultSet=statement.executeQuery(sql);
while(resultSet.next()){
添加(resultSet.getString(“权限\名称”);
}
}catch(SQLException-ex){
Logger.getLogger(Role.class.getName()).log(Level.SEVERE,null,ex);
}
返回myList;
}
}
我想您可以将角色列表存储到会话中,并根据需要使用列表。您可以使用
<%= request.getAttribute("MyAttribute"); %>
类似地,若要从jsp设置会话属性,请使用jsp会话作用域进行设置
<c:set var="name" value="value" scope="session" />
参考链接
希望这会对您有所帮助。我想您可以将角色列表存储到会话中,并根据需要使用列表。您可以使用
<%= request.getAttribute("MyAttribute"); %>
类似地,若要从jsp设置会话属性,请使用jsp会话作用域进行设置
<c:set var="name" value="value" scope="session" />
参考链接
希望这对您有所帮助。您可以在会话中保存该数组列表。会话特定于用户,并且在用户登录时有效/处于活动状态。用户注销后,其相应会话将/必须关闭。您可以在会话中保存该数组列表。会话特定于用户,并且在用户登录时有效/处于活动状态。一旦用户注销会话,其相应会话将/必须关闭。