问题描述
我已经为我的应用程序手动配置了web.xml
.现在,我在运行应用程序时遇到了问题.我试图从我的jsp
页面访问我的servlet
.但是,它会抛出错误page not found
.
I have manually configured web.xml
for my application. Now, I'm facing issues while running my application. I'm trying to access my servlet
from my jsp
page. But, it is throwing error as page not found
.
该servlet放置在文件夹位置下方
The servlets are placed under below folder location
<application folder>/WEB-INF/classes/<package>
因此,url-pattern
和servlet-mapping
中servlet的条目应该是什么.这样,可以通过URL访问servlet
.
So, what should be the entries for servlets in url-pattern
and servlet-mapping
. So that, servlet
can be accessible through URL.
推荐答案
url-pattern
在web.xml
中用于将servlet
映射到特定URL.请参见下面的xml代码,您可以在web.xml
配置文件中找到类似的代码.
url-pattern
is used in web.xml
to map your servlet
to specific URL. Please see below xml code, similar code you may find in your web.xml
configuration file.
<servlet>
<servlet-name>AddPhotoServlet</servlet-name> //servlet name
<servlet-class>upload.AddPhotoServlet</servlet-class> //servlet class
</servlet>
<servlet-mapping>
<servlet-name>AddPhotoServlet</servlet-name> //servlet name
<url-pattern>/AddPhotoServlet</url-pattern> //how it should appear
</servlet-mapping>
如果将AddPhotoServlet
的url-pattern
从/AddPhotoServlet
更改为/MyUrl
.然后,可以使用/MyUrl
访问AddPhotoServlet
servlet.出于安全性考虑,这是很好的选择,您想在其中隐藏实际的页面URL.
If you change url-pattern
of AddPhotoServlet
from /AddPhotoServlet
to /MyUrl
. Then, AddPhotoServlet
servlet can be accessible by using /MyUrl
. Good for the security reason, where you want to hide your actual page URL.
Java Servlet url-pattern
规范:
Java Servlet url-pattern
Specification:
- 以'/'字符开头并以'/*'结尾的字符串 后缀用于路径映射.
- 以'*.'开头的字符串 前缀用作扩展名映射.
- 仅包含'/'字符的字符串表示应用程序的默认" servlet.在这种情况下,servlet路径 是请求URI减去上下文路径,并且路径信息为 空值.
- 所有其他字符串仅用于完全匹配.
- A string beginning with a '/' character and ending with a '/*' suffix is used for path mapping.
- A string beginning with a '*.' prefix is used as an extension mapping.
- A string containing only the '/' character indicates the "default" servlet of the application. In this case the servlet path is the request URI minus the context path and the path info is null.
- All other strings are used for exact matches only.
参考文献: Java Servlet规范
您还可以阅读 Java Servlet的基础
这篇关于web.xml中url-pattern的意义是什么?如何配置servlet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!