在web项目中,为了安全,可能需要把jsp、html等页面文件放在WEB-INF目录下,这样如果我们的页面中出现超链接<a>标签或者<script></script>脚本下的location.href去直接转向到WEB-INF下的某一个jsp或者html页面,那么就会引用不到,因为这样的请求方式是客户端的请求,而WEB-INF页面只对服务端开放,对客户端是不可见的。
css/js/html没有必要放在WEB-INF下。 最终这些会被原封不动的展现在客户端,所以访问安全根本就不会成为问题。
jsp放在web-inf下,原因主要有两个
1. 远古时代的模式会把业务逻辑,数据库连接等敏感信息写在jsp里面,被用户直接访问会有安全问题。
现代模式里这个不再成为问题,不应该成为问题。
2. jsp是在服务器端运行的,而且通常都需要其他程序支持——比如后台处理好数据再让jsp渲染等,用户直接访问一则没有意义,二则会抛异常,这些都浪费服务器资源。
对于安全要求很严格的系统来说,不允许随便访问你的jsp文件,你可以放到web-inf下面,对于安全性要求没有那么严格的系统来说,你可以直接放到webroot下面。
web project网页放在WEB-INF下面受保护,不能直接访问,有下面三种方式:
假设要发布的网页为a.jsp,建表单form。
1、在web.xml中,将默认<welcome-file>index.jsp</welcome-file>改成<welcome-file>./WEB-INF/view/a.jsp</welcome-file>,其他注释:
<!-- <welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file> -->
<welcome-file>./WEB-INF/view/a.jsp</welcome-file>
访问方式为:http://localhost:8080/demo/
2、建servlet,jump.java,doGet下写方法
RequestDispatcher rd = request.getRequestDispatcher("./WEB-INF/view/a.jsp");
rd.forward(request, response);
在web.xml中给URL取访问名,jump:
<servlet-name>jump</servlet-name>
<servlet-class>database.jump</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>jump</servlet-name>
<url-pattern>/jump</url-pattern>
</servlet-mapping>
访问方法:http://localhost:8080/demo/jump
3、在web.xml中自建<servlet-mapping>
在a.jsp中修改from action的获取方式:
<form action="<%=request.getContextPath() %>/doinsert" method="post">
访问方式:访问方法:http://localhost:8080/demo/doinsert.jsp