如何删除JSP的本机请求URL? [英] How to remove JSP's native request URL?
问题描述
我有一个名为page.jsp
的网页,并且我只希望它具有2个URL:
I have a webpage page called page.jsp
, and I only want it to have 2 URLs:
- /page-url-1
- /page-url-2
- /page-url-1
- /page-url-2
在 web.xml 中,我写道:
<servlet>
<servlet-name>Page</servlet-name>
<jsp-file>page.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>Page</servlet-name>
<url-pattern>/page-url-1</url-pattern>
<url-pattern>/page-url-2</url-pattern>
</servlet-mapping>
但是现在,它具有3个URL:
But now, it has 3 URLs:
- /page.jsp < ==不需要
- /page-url-1
- /page-url-2
- /page.jsp <== Not wanted
- /page-url-1
- /page-url-2
问题: :如何删除JSP的本机URL,并为其分配自定义的URL?
Question: How do I remove the JSP's native URL, and assign it custom one(s)?
推荐答案
URL'page.jsp'有效,因为'page.jsp'放置在Web应用程序的文档根目录中.容器中除"WEB-INF"文件夹外的任何文件都可以直接提供给客户端.
URL 'page.jsp' works because 'page.jsp' is placed in document root of your web application. Any file stored in your container except 'WEB-INF' folder can be served directly to the client.
即.如果在网址中键入文件"/js/myJavascirpt.js",则将对其进行访问.同样适用于图像,比方说'/images/picture.png'.
viz. file '/js/myJavascirpt.js' will be accessed if you type it in your url. Same applies for images, lets say '/images/picture.png'.
根据WEB-INF的Servlet规范:
According to Servlet specification for WEB-INF:
在名为WEB-INF的应用程序层次结构中存在一个特殊目录.此目录包含与应用程序相关的所有内容,这些内容不在应用程序的文档根目录中. WEB-INF节点不属于应用程序的公共文档树.容器不能将WEB-INF目录中包含的文件直接提供给客户端.但是,使用ServletContext上的getResource和getResourceAsStream方法调用,Servlet代码可以看到WEB-INF目录的内容,并且可以使用RequestDispatcher调用公开WEB-INF目录的内容.
A special directory exists within the application hierarchy named WEB-INF. This directory contains all things related to the application that aren’t in the document root of the application. The WEB-INF node is not part of the public document tree of the application. No file contained in the WEB-INF directory may be served directly to a client by the container. However, the contents of the WEB-INF directory are visible to servlet code using the getResource and getResourceAsStream method calls on the ServletContext, and may be exposed using the RequestDispatcher calls.
根据规范,很明显,如果您不想让客户端直接访问文件,则应将这些文件存储在"WEB-INF"文件夹下.这就是Java Web应用程序中所有类和库都存储在"WEB-INF"文件夹下的原因.
From the specification, it is clear that if you don't want to give client direct access of your files, those files should be stored under 'WEB-INF' folder. That's the reason why all classes and libraries in java web application are stored under 'WEB-INF' folder.
涉及到您的问题时,您只需将jsp文件放在"WEB-INF"文件夹下即可.
When it comes to your problem, you could simply place your jsp file under 'WEB-INF' folder.
例如: 将您的jsp文件放在"WEB-INF"文件夹下:
eg: Place your jsp file under 'WEB-INF' folder:
WEB-INF/views/jsp/page.jsp
WEB-INF/views/jsp/page.jsp
并像这样更改web.xml中的条目:
and change your entry in web.xml like this:
<servlet>
<servlet-name>Page</servlet-name>
<jsp-file>/WEB-INF/views/jsp/page.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>Page</servlet-name>
<url-pattern>/page-url-1</url-pattern>
<url-pattern>/page-url-2</url-pattern>
</servlet-mapping>
来源: 访问 JSR-000315 JavaTM Servlet 3.0 以获取Java Servlet规范(Ch 10.5).
Source: Visit JSR-000315 JavaTM Servlet 3.0 for Java Servlet Specification(Ch 10.5).
这篇关于如何删除JSP的本机请求URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!