在Servlet中引用文件 [英] Referencing a file within Servlet

查看:90
本文介绍了在Servlet中引用文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在代码中引用DTD文件时,我不知道如何在项目文件夹中引用它.例如:如果我的项目名称是Moo,我想引用DTD在/Moo/WEB-INF/courses.dtd.

When I reference to a DTD file in my code, I don't know how to reference to it within my project's folder. e.g : If my project's name is Moo, I'd want to refernce the DTD at /Moo/WEB-INF/courses.dtd.

TransformerFactory transfac = TransformerFactory.newInstance();
Transformer trans = null;  

try { trans = transfac.newTransformer();   }
catch (TransformerConfigurationException e) { }

trans.setOutputProperty(OutputKeys.INDENT, "yes");  
trans.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "/Moo/WEB-INF/courses.dtd");

但是找不到它.我该如何引用?

But it doesn't find it. How can I reference it?

为我提供了一个使用getRealPath问题的解决方案,因为我的项目将被读取为.war文件,所以效果不好.我该怎么办?

I was offered a solution using getRealPath problem is since my project will be read as a .war file this is not good. What should I do?

推荐答案

首先,不要包含"Moo".对于servlet,根是WEB-INF的上一级. 不确定OutputKeys.DOCTYPE_SYSTEM是否需要相对路径.绝对路径为:

First of all, do not include "Moo". For servlet root is one level up from WEB-INF. Not sure that OutputKeys.DOCTYPE_SYSTEM wants relative path. Absolute path is:

String path = getServletContext().getRealPath("/WEB-INF/courses.dtd");

这篇关于在Servlet中引用文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆