我打算把javascript写到一个.js文件里,然后再引用他,如下面
<html>
<head>
</head>
<body>
<script type="text/javascript" src="${pageContext.request.contextPath}/WEB-INF/myjs/aa.js"></script>
</body>
</html>
aa.js
alert("aa");
但是始终没有"aa"这个提示
之后改成
<html>
<head>
</head>
<body>
<script type="text/javascript" src="${pageContext.request.contextPath}/WEB-INF/myjs/aa.js"></script>
<script type="text/javascript">
gg();
</script>
</body>
</html>
aa.js
function gg(){
alert("zz");
}
但是提示
gg(); 这行缺少对象
于是我怀疑是否aa.js没导入进去,但是检查路径没错。。
之后我把文件夹myjs存到WEB-INF外面,并更改相关的路径就找到了aa.js了
<html>
<head>
</head>
<body>
<script type="text/javascript" src="${pageContext.request.contextPath/myjs/aa.js"></script>
<script type="text/javascript">
gg();
</script>
</body>
</html>
所以我怀疑是.js不能存入WEB-INF里,不然无法识别
于是我在tomcat网站找到这样一段话
To facilitate creation of a Web Application Archive file in the required format, it is convenient to arrange the "executable" files of your web application (that is, the files that Tomcat actually uses when executing your app) in the same organization as required by the WAR format itself. To do this, you will end up with the following contents in your application's "document root" directory:
注意:application's "document root" directory就相当于项目跟路径,即(${pageContext.request.contextPath)
*.html, *.jsp, etc. - The HTML and JSP pages, along with other files that must be visible to the client browser (such as JavaScript, stylesheet files, and images) for your application. In larger applications you may choose to divide these files into a subdirectory hierarchy, but for smaller apps, it is generally much simpler to maintain only a single directory for these files.
这里告诉用户:javascrip等文件对于用户的浏览器必须是可见的。
/WEB-INF/web.xml - The Web Application Deployment Descriptor for your application. This is an XML file describing the servlets and other components that make up your application, along with any initialization parameters and container-managed security constraints that you want the server to enforce for you. This file is discussed in more detail in the following subsection.
转自文档http://tomcat.apache.org/tomcat-7.0-doc/appdev/deployment.html#Deployment_With_Tomcat
而/WEB-INF/里对于用户是不可见的
Tomcat doesn't let you access anything in the META-INF or WEB-INF folders. These folders have config for your system, and may contain passwords (such as META-INF/context.xml which has passwords for the datasources). Besides, people can get into WEB-INF/lib and WEB-INF/classes and web.xml and see your implementation details.
上面一段话转自:http://tomcat.10.n6.nabble.com/Unable-to-access-images-stored-in-webapps-lt-myapp-gt-WEB-INF-images-td4334193.html