简体   繁体   English

如何从打开位置的其他JSP页面关闭文件

[英]How to close a file from a different JSP page from where it was open

I've a JSP page where a kind of users users_group_1 need to work with a file. 我有一个JSP页面,其中一种用户users_group_1需要使用文件。 All of them work with the same file , so since I'm in JSP, I made a class ( Class1 ) with all the methods as synchronized to avoid problems using it. 它们都使用同一个文件 ,因此,由于我在JSP中,因此我将所有方法同步化为一个类( Class1 ),以避免使用它时出现问题。

In the JSP for these group of users, I've only one instance for all them, declared like this: 在针对这些用户组的JSP中,我为所有这些用户只有一个实例,声明如下:

<%!
  Class1 my_object = null;
%>

Then, when the first user uses the web, it does: 然后,当第一个用户使用网络时,它会:

if (my_object == null)
{
  my_object = new Class1(file_to_open);
}

Then, all the users from the group will be using the same instance. 然后,该组中的所有用户将使用同一实例。

So now, I need another JSP page , that will be open by user_group_2 , to close this file and save the work done . 所以现在,我需要另一个JSP页面 ,该页面将由user_group_2打开, 以关闭该文件并保存已完成的工作

So I think I need to get the instance of Class1 used in the JSP and give it to the second one. 因此,我认为我需要获取JSP中使用的Class1实例并将其交给第二个实例。

How can I do it? 我该怎么做?

Extra data: user_group_2 never uses the same JSP page than user_group_1 , so I can't use request/session/... objects (I think). 额外数据: user_group_2从不使用比同JSP页面user_group_1 ,所以我不能使用请求/会话/ ...对象(我认为)。

Your requirement can be achieved through something like given below by using ServletContext object(application scope). 通过使用ServletContext对象(应用程序范围),可以通过以下类似的方式满足您的要求。

On First jsp page where you want to initialize your object and set it to application scope. 在第一个jsp页面上,您要在其中初始化对象并将其设置为应用程序范围。

<% 
     ProcessFile processFile=(ProcessFile)application.getAttribute("processFile");
     if(null==processFile){
     // make sure that all method of this class is synchronized beacause of multiple users
         processFile=new  ProcessFile("pathToOpenFile");
     }

     application.setAttribute("processFile", processFile); 
     //now processFile available globally. 
    %>

And after that on second jsp page you can use that processFile object which has been set into application scope like : 然后在第二个jsp页面上,您可以使用已设置到应用程序范围内的那个processFile对象,例如:

 <% 
        ProcessFile processFile2=(ProcessFile)application.getAttribute("processFile");
          // now start processing of file 
           if(null!=processFile2){

           processFile2.readFile();
          //open file if not. And read it.  

          //and after that. 
          processFile2.closeFileIfOpen();  
          // do neccesorry checks inside above method while closing file.  
          //all method are synchronized
          }
        %>

Create a singleton class and have everyone getting an instance of that class. 创建一个单例类,让每个人都获得该类的实例。

 public class SingletonFile {
      private static SingletonFile instance.
      static {
            instance = new SingletonFile();
      }

      public static SingletonFile getInstance() {
           return instance;
      }

      private SingletonFile () {}

      // public methods for manipulation of files goes here.
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM