Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从会话检索用户名时的策略_Java_Html_Jsp_Jakarta Ee_Jsp Fragments - Fatal编程技术网

Java 从会话检索用户名时的策略

Java 从会话检索用户名时的策略,java,html,jsp,jakarta-ee,jsp-fragments,Java,Html,Jsp,Jakarta Ee,Jsp Fragments,我有一个技术问题,我不确定正确的解决方法: 在我正在开发的网页中,我必须存储会话(登录者)中的当前用户名以“标记”某个操作。(例如,“用户在上创建了此文件”)。我的算法从会话中检索用户名,但显然每个用户的用户名都会发生变化。因此,始终是登录用户的名称,而不是创建者名称 有任何提示吗 谢谢 从逻辑上讲,这些是您想要的步骤 用户1登录 User1的名称存储在Http会话中 User1创建文件42 系统存储在User1在Timestamp257上创建文件42的数据库中 用户1注销 用户2登录 Use

我有一个技术问题,我不确定正确的解决方法:

在我正在开发的网页中,我必须存储会话(登录者)中的当前用户名以“标记”某个操作。(例如,“用户在上创建了此文件”)。我的算法从会话中检索用户名,但显然每个用户的用户名都会发生变化。因此,始终是登录用户的名称,而不是创建者名称

有任何提示吗


谢谢

从逻辑上讲,这些是您想要的步骤

  • 用户1登录
  • User1的名称存储在Http会话中
  • User1创建文件42
  • 系统存储在User1在Timestamp257上创建文件42的数据库中
  • 用户1注销
  • 用户2登录
  • User2的名称存储在Http会话中
  • User2查看有关File42的信息
  • 系统从数据库读取User1在时间戳257上创建的文件42
  • 系统向用户2显示信息
我认为您可能缺少系统存储内容的部分(例如,在数据库中)

编辑:如果不需要持久性,可以将共享数据存储在ServletContext中。注意这不是一个严肃的解决方案,但可以用于快速原型或演示。不要想在生产中这样做,这是有问题的

在servlet中,执行以下操作:

private static Map<String, FileData> fileAccess;

private class FileData {
    String userName;
    Date timeStamp = new Date();;
    String fileName;
    FileData(String userName, String fileName) {
        this.userName = userName;
        this.fileName= fileName;
    }
}

public void init(ServletConfig config) {
    String attributeKey = "fileAccess";
    fileAccess = config.getServletContext().getAttribute(attributeKey);
    if (fileAccess == null) {
        fileAccess = new HashMap<String, FileData>();
        config.getServletContext().setAttribute(attributeKey, fileAccess);
    }
}

// in this example a POST means a user accesses a file
public void doPost(HttpServletRequest req, HttpServletResponse resp) {

    // get the user name from the current session
    String userName = req.getSession().getAttribute("userName");

    // get the file name from the request (posted from the file access form)
    String fileName = req.getParameter("fileName");

    // check if we have the necessary data
    if (userName == null || fileName == null) {
        resp.getWriter().write("Invalid file access request");
        resp.getWriter().flush();    
        return;
    }

    // create and fill file data wrapper
    FileData fileData = new FileData(userName, fileName);

    // store the file data in the shared fileAccess map.
    // synchronized to block simultaneous acccess from different threads
    synchronized (fileAccess) {
        // note: any previously stored FileData object gets replaced
        fileAccess.put(fileName, fileData);
    }

    // display the result to the user
    display(fileData, resp);
}

// in this example a GET means a user views a file
public void doGet(HttpServletRequest req, HttpServletResponse resp) {

    // get the file name parameter from the request (sent as part of the view-file request)
    String fileName = req.getParameter("fileName");

    // check if we have the necessary data
    if (fileName == null) {
        resp.getWriter().write("Invalid view file request.");
        resp.getWriter().flush();    
        return;
    }

    // get the file data from the shared fileAccess map.
    // synchronized to block simultaneous acccess from different threads
    synchronized (fileAccess) {
        FileData  fileData = fileAccess.get(fileName);   

        // display the result to the user
        display(fileData, resp);
    }
}

private void display(FileData fileData, HttpServletResponse resp) {
    resp.getWriter().write("File accessed:");
    resp.getWriter().write("User: " + fileData.userName);
    resp.getWriter().write("File: " + fileData.fileName);
    resp.getWriter().write("Timestamp: " + fileData.timeStamp);
    resp.getWriter().flush();  
}
私有静态映射文件访问;
私有类文件数据{
字符串用户名;
日期时间戳=新日期();;
字符串文件名;
文件数据(字符串用户名、字符串文件名){
this.userName=用户名;
this.fileName=文件名;
}
}
public void init(ServletConfig配置){
字符串attributeKey=“fileAccess”;
fileAccess=config.getServletContext().getAttribute(attributeKey);
if(fileAccess==null){
fileAccess=newhashmap();
config.getServletContext().setAttribute(attributeKey,fileAccess);
}
}
//在本例中,POST表示用户访问文件
public void doPost(HttpServletRequest-req、HttpServletResponse-resp){
//从当前会话获取用户名
字符串userName=req.getSession().getAttribute(“用户名”);
//从请求中获取文件名(从文件访问表单发布)
字符串文件名=req.getParameter(“文件名”);
//检查我们是否有必要的数据
如果(用户名==null | |文件名==null){
resp.getWriter().write(“无效的文件访问请求”);
分别是getWriter().flush();
返回;
}
//创建并填充文件数据包装器
FileData FileData=新文件数据(用户名、文件名);
//将文件数据存储在共享文件访问映射中。
//已同步以阻止来自不同线程的同时访问
已同步(文件访问){
//注意:将替换以前存储的任何FileData对象
fileAccess.put(文件名、文件数据);
}
//向用户显示结果
显示(文件数据,resp);
}
//在本例中,GET表示用户查看文件
公共无效数据集(HttpServletRequest请求、HttpServletResponse响应){
//从请求中获取文件名参数(作为查看文件请求的一部分发送)
字符串文件名=req.getParameter(“文件名”);
//检查我们是否有必要的数据
如果(文件名==null){
resp.getWriter().write(“无效的视图文件请求”);
分别是getWriter().flush();
返回;
}
//从共享文件访问映射获取文件数据。
//已同步以阻止来自不同线程的同时访问
已同步(文件访问){
FileData FileData=fileAccess.get(文件名);
//向用户显示结果
显示(文件数据,resp);
}
}
私有无效显示(FileData FileData,HttpServletResponse resp){
resp.getWriter().write(“文件已访问:”);
resp.getWriter().write(“用户:+fileData.userName”);
resp.getWriter().write(“文件:+fileData.fileName”);
resp.getWriter().write(“时间戳:+fileData.Timestamp”);
分别是getWriter().flush();
}

正是我的朋友,这正是我要找的。问题是,在示例的最后一步中,系统将向用户2显示“User2在时间戳257上创建了文件42”,因为此字符串中的User2只是会话中的用户,此时系统应向用户2显示“User1在时间戳257上创建了文件42”。您知道是否有任何软件模式可以实现这一点吗?干杯,伙计。你是否将文件、用户和时间戳存储在服务器上的数据库中?我之所以不存储它,是因为我试图找到一个更轻松的解决方案。请参阅我在回答中添加的解决方案。只用于原型或演示,而不是生产代码。谢谢,但我几天前解决了这个问题,所以我不会在原型、演示或生产代码中使用它,但无论如何,谢谢。