编程语言
首页 > 编程语言> > java – web.xml中url-pattern的意义是什么,以及如何配置servlet?

java – web.xml中url-pattern的意义是什么,以及如何配置servlet?

作者:互联网

我为我的应用程序手动配置了web.xml.现在,我在运行我的应用程序时遇到了问题.我正试图从我的jsp页面访问我的servlet.但是,由于找不到页面,它会抛出错误.

servlet位于文件夹下方

<application folder>/WEB-INF/classes/<package>

那么,url-pattern和servlet-mapping中servlet的条目应该是什么.这样,servlet可以通过URL访问.

解决方法:

url-pattern在web.xml中用于将servlet映射到特定URL.请参阅下面的xml代码,您可以在web.xml配置文件中找到类似的代码.

<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.出于安全原因,您希望隐藏实际页面网址.

Java Servlet url-pattern规范:

  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 Specification

您可能还会阅读此Basics of Java Servlet

标签:url-pattern,java,servlets,web-xml,servlet-mapping
来源: https://codeday.me/bug/20190915/1805975.html