Why can web.xml welcome-file be located inside WEB-INF

Deadly 提交于 2019-12-08 08:30:06

问题


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

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>

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


回答1:


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.

See also:

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

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.



来源:https://stackoverflow.com/questions/17821984/why-can-web-xml-welcome-file-be-located-inside-web-inf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!