使用进行返回附加的 servlet 调用,但它们没有被呈现
所以这个问题建议在执行包含之前使用 servlet 进行文件检查: 如何检查在 JSP 中包含/导入文件之前是否存在该文件?
所以我编写了一个 servlet 来执行此操作。我使用类似的方式来调用它,
<jsp:include page='<%= "/servlet/fileChecker?section=THX&file=1138" &>'></jsp:include>
但是该 servlet 调用的输出包含一个 jsp:include 标记,指向我正在检查的文件。不幸的是,浏览器不“包含”它。我收到诸如“没有属性‘page’”和“元素‘jsp:include’未定义”之类的错误——这表明 servlet 输出没有呈现为 Java,而是呈现为 HTML。
这是输出:
<p>Text before the servlet call</p>
<p><h4>From THX1138</h4><jsp:include page='thx/results_1138.jsp'></jsp:include></p>
<p>Text after the servlet call</p>
这是我的 servlet 的主要调用:
private String FileChecker(String section, String file) {
String result = ""; // assume file does not exist
String pathToCheck = section + "/results_" + file + ".jsp";
// realPath is defined in the init() method as config.getServletContext().getRealPath("/");
File fileToCheck = new File(realPath + pathToCheck);
if (fileToCheck.exists()) {
result = "<p><h4>" + section + "</h4><jsp:include page='" + pathToCheck + "'></jsp:include></p>";
}
return result;
}
我觉得答案很接近,但我不确定我应该在后面看什么窗帘。有什么帮助吗?
So this question suggested using a servlet to do the file check before doing the include:
How can you check if a file exists before including/importing it in JSP?
So I wrote a servlet that does that. I call it using something like
<jsp:include page='<%= "/servlet/fileChecker?section=THX&file=1138" &>'></jsp:include>
but the output of that servlet call contains a jsp:include tag, to the file I was checking for. Unfortunately, the browser doesn't "include" it. I'm getting errors like "there is no attribute 'page'" and "element 'jsp:include' undefined" -- which suggests the servlet output is not being rendered as Java but as HTML.
Here is the output:
<p>Text before the servlet call</p>
<p><h4>From THX1138</h4><jsp:include page='thx/results_1138.jsp'></jsp:include></p>
<p>Text after the servlet call</p>
Here is the main call of my servlet:
private String FileChecker(String section, String file) {
String result = ""; // assume file does not exist
String pathToCheck = section + "/results_" + file + ".jsp";
// realPath is defined in the init() method as config.getServletContext().getRealPath("/");
File fileToCheck = new File(realPath + pathToCheck);
if (fileToCheck.exists()) {
result = "<p><h4>" + section + "</h4><jsp:include page='" + pathToCheck + "'></jsp:include></p>";
}
return result;
}
I feel like the answer is close, but I'm not sure what curtain I should be looking behind. Any help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要将带有一堆 HTML 和 JSP 标记的字符串写入响应。这毫无意义。 Web 浏览器不理解 JSP 标签。
请改为调用
RequestDispatcher#include()
。并将该 HTML 移回到 JSP 中。
与具体问题无关,我知道您指的是我的旧答案,但我意识到实际上最好检查
ServletContext#getResource()
是否不存在不返回null
,而是使用File#exists()
。这样,只要 WAR 不扩展,它就可以正常工作。Do not write a string with a bunch of HTML and JSP tags to the response. This makes no sense. The webbrowser does not understand JSP tags.
Call
RequestDispatcher#include()
instead.And move that HTML back into the JSP.
Unrelated to the concrete question, I know that you're referring to an old answer of me, but I realize that it's actually better to check if
ServletContext#getResource()
doesn't returnnull
instead of usingFile#exists()
. This way it'll work as well whenever the WAR is not expanded.