为什么web.xml welcome-file可以位于WEB-INF中 [英] Why can web.xml welcome-file be located inside WEB-INF

查看:466
本文介绍了为什么web.xml welcome-file可以位于WEB-INF中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经读过 WEB-INF 中的 jsp 文件无法从外部访问(必须调用它)来自 Servlet )。

I have read that a jsp file inside WEB-INF cannot be accessed from outside (it must be called from a Servlet).

但是,为什么 web.xml中的 welcome-file file,指向 WEB-INF 目录中的文件?因为这样可以从外面访问文件:

But, why can the welcome-file in web.xml file, point to a file inside the WEB-INF directory? because this way the file can be accessed from outside :

<welcome-file-list>  
    <welcome-file>WEB-INF/page.jsp</welcome-file>  
</welcome-file-list>

我正在测试 Google App引擎和默认的Servlet容器 Jetty 6

I'm testing with Google App engine and default Servlet container Jetty 6.

推荐答案

这是因为欢迎文件由 RequestDispatcher#forward() 来电。作为证据,您是否看到 /WEB-INF/page.jsp 出现在浏览器的地址栏中?没有?然后它绝对不是直接请求。如果你试图直接申请,你会看到你得到404.

It's because the welcome file is served by a RequestDispatcher#forward() call. As evidence, do you see /WEB-INF/page.jsp appearing in browser's address bar? No? Then it's already most definitely not a direct request. If you attempt to request it directly, you'll see that you get a 404.

  • RequestDispatcher.forward() vs HttpServletResponse.sendRedirect()
  • Servlets wiki page - contains Hello World examples illustrating real world use cases for forward()

无关到具体问题,你似乎基本上滥用< welcome-file> 来拥有一个主页文件。那里没有意图。当任何文件夹被请求时,它打算指定您要提供的文件夹文件的唯一文件名,例如 / / foo / / foo / bar / 等。如果指定 index.jsp ,然后 /index.jsp /foo/index.jsp /foo/bar/index.jsp 等将由前锋透明地提供,而不会更改网址。

Unrelated to the concrete problem, you seem to be basically abusing the <welcome-file> to have a "homepage file". There it is not intented for. It's intented to specify the sole filename of the folder's file you'd like to serve up when any folder is been requested, such as /, /foo/, /foo/bar/, etc. If you specify index.jsp, then the /index.jsp, /foo/index.jsp, /foo/bar/index.jsp, etc will transparently be served up by a forward without a change in URL.

这篇关于为什么web.xml welcome-file可以位于WEB-INF中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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