2014-12-03 58 views
0

检索用户名时,我有一个技术问题,我不知道正确的方法来解决:战略从会议

在一个网页,我发展,我必须存储从会话中的当前用户名(登录的人)来“戳记”一个动作。 (例如,“用户创建此文件”)。我的算法从会话中检索用户名,但显然会为每个用户更改。因此,它始终是用户登录的用户之一,而不是创建者的名称。

任何提示任何人?

谢谢!

回答

1

因此,逻辑上这些是你想要的步骤?

  • User1登录时
  • User1的名称都存储在HTTP会话中
  • 用户1创建File42在用户1创建File42上Timestamp257数据库
  • 系统存储
  • 用户1注销
  • 用户2个日志在
  • 用户2的名字被存储在Http会话中
  • User2 view是关于File42信息
  • 系统从用户1创建File42上Timestamp257数据库读取
  • 系统显示信息,以用户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(); 
} 
+0

正是我的朋友,这正是我所期待的。问题是,在你的例子的最后一步,系统将向用户2显示“User2在Timestamp257上创建了File42”,因为该字符串中的User2只是会话中的用户,当系统应该显示给User2时“User1创建了File42” Timestamp257" 。你知道是否有任何一种软件模式来实现这一目标?队友的欢呼声。 – 2014-12-03 11:03:40

+1

您是否将文件,用户和时间戳存储在服务器上的数据库中? – 2014-12-03 11:04:20

+0

我不存储它,因为我试图找到一个更轻的解决方案。 – 2014-12-03 11:21:04