引用放置在 JSP 文件中的 WEB-INF 文件夹中的资源会在资源上返回 HTTP 404 [英] Referencing a resource placed in WEB-INF folder in JSP file returns HTTP 404 on resource

查看:22
本文介绍了引用放置在 JSP 文件中的 WEB-INF 文件夹中的资源会在资源上返回 HTTP 404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在eclipse中创建了一个名为BookShopWeb的动态Web项目,目录结构如下

I have a dynamic web project called BookShopWeb which I created in eclipse, with the following directory structure

/BookShopWeb/|
  |--src
  |---WebContent
                | 
                |---META-INF
                |----WEB-INF---web.xml
                            |
                            |--css--styles.css
                            |--jsp---index.jsp 

在 web.xml 中,我将起始页设置为

In web.xml I set the start page as

<welcome-file-list>

<welcome-file>/WEB-INF/jsp/index.jsp</welcome-file>

在 index.jsp 中,我将 css 包含为

In the index.jsp I am including the css as

<head>
<link rel="stylesheet" type="text/css" href="../css/styles.css" />
</head>

加载时的索引页没有显示 css 信息.我用 firebug 检查了元素,它显示了一个错误报告

The index page when loaded however does not show the css info.I checked the element with firebug and it shows an error report

Apache Tomcat/6.0.29 - Error report..
The requested resource (/css/styles.css) is not available.

知道为什么会发生这种情况吗?我该如何纠正?谢谢标记

Any idea why this occurs?How can I correct this? thanks mark

推荐答案

/WEB-INF 文件夹中的文件不可公开访问.将 CSS 文件上一层,放在 WebContent 文件夹中,并通过在浏览器地址栏中直接输入其 URL 来确保可以访问它们.此外,您在 中指定的 URL 必须相对于请求 URL(您在打开 JSP 时在浏览器地址栏中看到的),而不是它在服务器上的位置磁盘文件系统.最好的方法是以正斜杠 / 开头,使其与域相关.

Files in /WEB-INF folder are not public accessible. Put the CSS files one level up, in the WebContent folder, and ensure that they are accessible by entering their URL straight in the browser address bar. Also, the URL which you specify in the <link href> must be relative to the request URL (which you see in the browser address bar when opening the JSP), not to its location on the server disk file system. The best approach is to make it domain-relative by starting with a forward slash /.

<link rel="stylesheet" href="/BookShopWeb/css/styles.css" />

或者更动态一点,这样您就不需要每次更改上下文路径时都更改 JSP

or a bit more dynamically so that you don't need to change your JSPs everytime whenever you change the context path

<link rel="stylesheet" href="${pageContext.request.contextPath}/css/styles.css" />

JSP 文件可以保存在 /WEB-INF 中,但这样它们只能通过调度 servlet 访问,要么通过扩展 HttpServlet 本地生成,要么由 servletcontainer 隐式例如.

JSP files can be kept in /WEB-INF, but this way they are only accessible through a dispatching servlet, either homegrown by extending HttpServlet or implicitly by the servletcontainer such as the <welcome-file>.

这篇关于引用放置在 JSP 文件中的 WEB-INF 文件夹中的资源会在资源上返回 HTTP 404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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