jetty 服务器出现 HTTP 错误 404
我的项目中有以下文件夹结构。
> src/
>>main/
>>>webapp/
>>>>WEB_INF/
>>>>>pages/
>>>>>>js/
我的所有脚本文件都位于js文件夹中,我的jsp页面位于pages文件夹中。为了在我的jsp页面中包含abc.js脚本,我编写了以下行
<script type="text/javascript" src="/WEB-INF/pages/js/abc.js"></script>
但是我收到以下错误
"NetworkError: 404 Not Found - http://localhost:8080/WEB-INF/pages/js/abc.js"
我正在使用jetty服务器来部署我的项目。为了运行它,我使用
mvn jetty:run
如果我将脚本文件直接放在 webapp 文件夹中并按以下方式包含脚本,那么它工作得很好。
<script type="text/javascript" src="abc.js"></script>
但我想通过将所有脚本放在 js 文件夹中来维护项目的文件夹结构。 有人可以告诉我为什么 jetty 服务器只获取位于 webapp 文件夹中的文件吗? 我用的是windows7
I have the following folder structure in my project.
> src/
>>main/
>>>webapp/
>>>>WEB_INF/
>>>>>pages/
>>>>>>js/
All my script files are located in js folder and my jsp page is located in pages folder.In order to include abc.js script in my jsp page I wrote the following line
<script type="text/javascript" src="/WEB-INF/pages/js/abc.js"></script>
But I got following error
"NetworkError: 404 Not Found - http://localhost:8080/WEB-INF/pages/js/abc.js"
I am using jetty server to deploy my project. For running it I am using
mvn jetty:run
If I am putting my script file directly in webapp folder and including script in following way then it is working perfectly fine.
<script type="text/javascript" src="abc.js"></script>
But I want to maintain folder structure of my project by putting all scripts in js folder.
Can somebody please tell me why jetty server is taking files located only in webapp folder?
I am using windows7
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
src
不正确。 Java Servlet 规范明确规定WEB-INF
目录下的任何文档都不可用于网络服务器。您需要将 JavaScript 文件放在它之外。通常,您将 jsp 文件放置在其中,因为它们不能直接访问。它们可以通过 Servlet 或 Java 中的任何其他 Web 开发框架进行访问。
如果您想维护文件夹结构,只需将 js 文件夹放在 Web 存档根目录中即可:
WEB-INF
目录的设计正是为了防止用户访问他们应该访问的文件无法访问,例如classes
、lib
目录或web.xml
部署描述符。Your
src
is incorrect. Java Servlet specifications states explicitly that no document underWEB-INF
directory will be available to the webserver. You need to place your JavaScript file outside of it.Usually, you place jsp files inside of it, because they're not accessed directly. They're accessed through a Servlet or any other web development framework in Java.
If you want to maintain your folder structure, just put your js folder on the web archive root:
WEB-INF
directory was designed exactly to prevent that users could access files they should not get access to, such asclasses
,lib
directories, orweb.xml
deployment descriptor.