如何使 ArrayList 在 Web 应用程序的所有 jsp 和 servlet 页面中可用



我有一个jsp+servlets web应用程序。我希望 ArrayList 在所有 jsp 和 servlet 页面中可用,每个登录用户都不同。当用户登录到Web应用程序时,我为用户分配了一些角色,根据角色,在Web应用程序中为他分配了一些权限。我的意思是说 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;
    }
}

我想您可以将角色列表存储到会话中并根据您的要求使用列表。您可以使用 jsp 轻松访问

<%= request.getAttribute("MyAttribute"); %>

同样,如果要从 jsp 设置会话属性,请使用 jsp 会话范围。

<c:set var="name" value="value" scope="session" />

参考链接 https://www.dineshonjava.com/jsp-scopes-example/

希望这对您有所帮助。

最新更新