web.xml中url-pattern的意义是什么?如何配置servlet? [英] What is the significance of url-pattern in web.xml and how to configure servlet?

查看:616
本文介绍了web.xml中url-pattern的意义是什么?如何配置servlet?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为我的应用程序手动配置了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-patternservlet-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-patternweb.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>

如果将AddPhotoServleturl-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:

  1. '/'字符开头并以'/*'结尾的字符串 后缀用于路径映射.
  2. '*.'开头的字符串 前缀用作扩展名映射.
  3. 仅包含'/'字符的字符串表示应用程序的默认" servlet.在这种情况下,servlet路径 是请求URI减去上下文路径,并且路径信息为 空值.
  4. 所有其他字符串仅用于完全匹配.
  1. A string beginning with a '/' character and ending with a '/*' suffix is used for path mapping.
  2. A string beginning with a '*.' prefix is used as an extension mapping.
  3. 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.
  4. All other strings are used for exact matches only.

参考文献: Java Servlet规范

您还可以阅读 Java Servlet的基础

这篇关于web.xml中url-pattern的意义是什么?如何配置servlet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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