GlassFish 下 Web 应用程序中文件的路径

发布于 2024-09-04 13:54:00 字数 187 浏览 10 评论 0 原文

如何指定 Web 应用程序中文件的路径?我在 WEB-INF 下有一个名为“templates”的文件夹,有人告诉我在 GlassFish v3 下路径应如下所示:

./WebContent/WEB-INF/templates

但这样我会收到文件未找到异常。为了让它发挥作用,我必须改变什么?

How do I specify the path to a file in a web application? I have a folder named 'templates' under WEB-INF, I've been told that under GlassFish v3 the path should look like this:

./WebContent/WEB-INF/templates

but this way I'm getting a file not found exception. What do I have to change in order to make it work?

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

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

发布评论

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

评论(4

像极了他 2024-09-11 13:54:00

如果我理解正确的话,您不能依赖当前工作目录来查找已部署的资源。如果您的资源在物理上相对于类路径资源放置(例如在 jar 内),您可以询问该资源在哪里,然后从那里导航。

来自 servlet 如何获取 servlet 外部文件的绝对路径? 又来自 http://www.exampledepot.com/egs/java.lang/ClassOrigin.html

Class cls = this.getClass();
ProtectionDomain pDomain = cls.getProtectionDomain();
CodeSource cSource = pDomain.getCodeSource();
URL loc = cSource.getLocation();  // file:/c:/almanac14/examples/

If I understand you correctly, you cannot rely on the current working directory to locate a resource you have deployed. If your resource is physically placed relative to a classpath resource (like inside a jar), you can ask about where that resource is, and then navigate from there.

From How a servlet can get the absolute path to a file outside of the servlet? which in turn is from http://www.exampledepot.com/egs/java.lang/ClassOrigin.html:

Class cls = this.getClass();
ProtectionDomain pDomain = cls.getProtectionDomain();
CodeSource cSource = pDomain.getCodeSource();
URL loc = cSource.getLocation();  // file:/c:/almanac14/examples/
生生不灭 2024-09-11 13:54:00

当您在 Eclipse 中创建动态 Web 应用程序项目时,将进入 war 文件根目录的内容将从 WebContent 文件夹打包。

听起来您想在 Web 应用程序运行时访问 WEB-INF/templates 目录中的文件。

我假设您目前正在使用绝对路径从那里访问文件。您已经发现,一旦部署了您的应用程序,这可能不适用于您的应用程序。

您需要使用 ServletContext.getResourceAsStream(String)

以下代码片段从 servlet 中查找名为 WEB-INF/templatez/myfile.txt 的文件,该 servlet 是包含 myfile.txt 文件的 Web 应用程序的一部分。其他 Web 应用程序和用户将无法通过 http GET 请求访问该文件。

package a;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name="FileFinder", urlPatterns={"/FileFinder"})
public class FileFinder extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            //* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet FileFinder</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet FileFinder at " + request.getContextPath () + "</h1>");
            InputStream is = null;
            try {
                is = request.getServletContext().getResourceAsStream("/WEB-INF/templatez/myfile.txt");
                out.println((null == is ? "did not " : "did ") + "find the file myfile.txt");
            } finally {
                if (null != is) is.close();
            }

            out.println("</body>");
            out.println("</html>");
            //*/
        } finally { 
            out.close();
        }
    } 

    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }

}

When you create a dynamic web app project in Eclipse, the content that will go into the root of the war file gets packaged from the WebContent folder.

It sounds like you want to access a file from the directory WEB-INF/templates at runtime for your web app.

I assume that you are using the absolute path to access a file from there presently. You have already figured out that this probably will not work for your app, once it is deployed.

You will need to access the content of the file using ServletContext.getResourceAsStream(String).

The following snippet finds a file named WEB-INF/templatez/myfile.txt from a servlet that is part if the web app that contains the myfile.txt file. Other web apps and users will will not be able to access the file via http GET requests.

package a;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name="FileFinder", urlPatterns={"/FileFinder"})
public class FileFinder extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            //* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet FileFinder</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet FileFinder at " + request.getContextPath () + "</h1>");
            InputStream is = null;
            try {
                is = request.getServletContext().getResourceAsStream("/WEB-INF/templatez/myfile.txt");
                out.println((null == is ? "did not " : "did ") + "find the file myfile.txt");
            } finally {
                if (null != is) is.close();
            }

            out.println("</body>");
            out.println("</html>");
            //*/
        } finally { 
            out.close();
        }
    } 

    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }

}
圈圈圆圆圈圈 2024-09-11 13:54:00

好吧,我明白了。不敢相信解决方案如此简单。我刚刚将 templates 文件夹移至 WebContent 文件夹,JSP 和 HTML 页面所在的位置相同,并将 DD 中的路径更改为 /templates。现在我非常确定它可以在任何服务器上的任何 Web 容器下工作。

Ok, I figured this out. Can't believe the solution was this simple. I just moved the templates folder to WebContent folder, same place JSP and HTML pages are at and changed the path in the DD to /templates. Now I'm pretty sure it'll work under any web container on any server.

猫弦 2024-09-11 13:54:00

servlet 3.0 的资源 JAR 功能有用吗: http://blogs.oracle.com/alexismp /entry/web_inf_lib_jar_meta

Would servlet 3.0's resource JAR feature be useful: http://blogs.oracle.com/alexismp/entry/web_inf_lib_jar_meta ?

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