如何在 SessionListener 中获取 JSF 中的真实路径

发布于 2024-12-18 07:08:12 字数 678 浏览 3 评论 0原文

我的要求如下:

信息:icefaces上传组件,将文件上传到相关文件夹,并为每个用户在该文件夹中使用sessionId创建一个子目录。

要求:在每个用户的sessionDestroyed处,我想获取真实路径,以删除当前用户文件夹。

我知道如何使用 JSF 获取真实路径,如下所示:

ServletContext ctx = (ServletContext) FacesContext.getCurrentInstance()
                .getExternalContext().getContext();
        String deploymentDirectoryPath = ctx.getRealPath("/");

问题: 如果您尝试在 sessionDestroyed 中获取真实路径,您将得到空指针异常,所以我想知道如果有一种方法可以在侦听器中初始化变量 deploymentDirectoryPath ,以便我可以在 sessionDestroyed 方法中使用它,或者可以在应用程序启动时初始化真实路径变量并使用它 这里 ?

请告知如何解决这个问题。

my requirement is as follows:

INFO: icefaces upload component, uploads the files to relative folder and creates for each user a sub-directory in that folder with the sessionId.

Requirement: at the sessionDestroyed for each user, i want to get the real path, to delete current user folder.

i know how to get the real path with JSF as follows:

ServletContext ctx = (ServletContext) FacesContext.getCurrentInstance()
                .getExternalContext().getContext();
        String deploymentDirectoryPath = ctx.getRealPath("/");

Problem: if you try to get the real path in sessionDestroyed you will get null pointer exception, so i was wondering if there's a way to initialize the variable deploymentDirectoryPath in the listener so i can use it in sessionDestroyed method, or maybe initialize the real path variable on application startup and use it here ?

please advise how to solve this issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

今天小雨转甜 2024-12-25 07:08:12

即使您还没有发布与问题相关的实际代码,以下内容也给了我真正的路径:

public class MySessListener implements HttpSessionListener {

    @Override
    public void sessionCreated(final HttpSessionEvent se) {
        System.out.println(Thread.currentThread().getStackTrace()[1]);
        new Timer().schedule(new TimerTask() {

            @Override
            public void run() {
                HttpSession sess = se.getSession();
                sess.invalidate();
            }
        }, 10000);
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        System.out.println(Thread.currentThread().getStackTrace()[1]);
        String realPath = se.getSession().getServletContext().getRealPath("/");
        System.out.println("realPath: " + realPath);
    }
}

输出

INFO: com.bhesh.demo.web.listener.MySessListener.sessionCreated(MySessListener.java:13)
INFO: com.bhesh.demo.web.listener.MySessListener.sessionDestroyed(MySessListener.java:26)
INFO: realPath: C:\Documents and Settings\Bhesh\My Documents\NetBeansProjects\JsfMessageList\build\web\

Even though you haven't posted the actual code that relates to the problem, the following gives me the real path:

public class MySessListener implements HttpSessionListener {

    @Override
    public void sessionCreated(final HttpSessionEvent se) {
        System.out.println(Thread.currentThread().getStackTrace()[1]);
        new Timer().schedule(new TimerTask() {

            @Override
            public void run() {
                HttpSession sess = se.getSession();
                sess.invalidate();
            }
        }, 10000);
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        System.out.println(Thread.currentThread().getStackTrace()[1]);
        String realPath = se.getSession().getServletContext().getRealPath("/");
        System.out.println("realPath: " + realPath);
    }
}

Output

INFO: com.bhesh.demo.web.listener.MySessListener.sessionCreated(MySessListener.java:13)
INFO: com.bhesh.demo.web.listener.MySessListener.sessionDestroyed(MySessListener.java:26)
INFO: realPath: C:\Documents and Settings\Bhesh\My Documents\NetBeansProjects\JsfMessageList\build\web\
给妤﹃绝世温柔 2024-12-25 07:08:12

根据这里和其他地方的 BalusC 合理建议,我们可以编写一个如下所示的通用函数:

  String getPath(){

ExternalContext tmpEC;      

tmpEC = FacesContext.getCurrentInstance().getExternalContext();   
String realPath=tmpEC.getRealPath("/");
return realPath;    
}

Based on BalusC sound advice here and elsewhere one could write a general purpose function like this:

  String getPath(){

ExternalContext tmpEC;      

tmpEC = FacesContext.getCurrentInstance().getExternalContext();   
String realPath=tmpEC.getRealPath("/");
return realPath;    
}
旧夏天 2024-12-25 07:08:12

您可以按如下方式获取真实路径:-

FacesContext.getCurrentInstance().getExternalContext().getRealPath("/");

<​​strong>编辑:-

正如 BalusC 在他的一个答案中提到的,在使用 getRealPath("/") 之前您应该三思而后行,因为如果没有选择扩展 war 文件,则 getRealPath("/") 可能会返回 null

请改用 getExternalContext.getResourceAsStream 。根据,docs,有效在应用程序启动或关闭期间调用此方法。

You can get the real path as follows:-

FacesContext.getCurrentInstance().getExternalContext().getRealPath("/");

EDIT:-

As mentioned by BalusC in one of his answers, you should think twice before using getRealPath("/") because if one has not chose to expand the war file then, getRealPath("/") might return null.

Use getExternalContext.getResourceAsStream instead. as per, docs, It is valid to call this method during application startup or shutdown.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文