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

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

问题描述

我已经为我的应用程序手动配置了 web.xml.现在,我在运行我的应用程序时遇到了问题.我试图从我的 jsp 页面访问我的 servlet.但是,它抛出错误 page not found.

servlets 被放置在下面的文件夹位置

<应用程序文件夹>/WEB-INF/classes/

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

解决方案

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

<servlet-name>AddPhotoServlet</servlet-name>//servlet名称<servlet-class>upload.AddPhotoServlet</servlet-class>//servlet类</servlet><servlet-mapping><servlet-name>AddPhotoServlet</servlet-name>//servlet名称<url-pattern>/AddPhotoServlet</url-pattern>//它应该如何出现</servlet-mapping>

如果您将 AddPhotoServleturl-pattern/AddPhotoServlet 更改为 /MyUrl.然后,可以使用 /MyUrl 访问 AddPhotoServlet servlet.出于安全原因,您想隐藏实际页面 URL.

Java Servlet url-pattern 规范:

<块引用>

  1. '/'字符开头并以'/*'结尾的字符串后缀用于路径映射.
  2. '*.'开头的字符串前缀用作扩展映射.
  3. 仅包含 '/' 字符的字符串表示应用程序的默认"servlet.在这种情况下,servlet 路径是请求 URI 减去上下文路径,路径信息是空值.
  4. 所有其他字符串仅用于精确匹配.

参考:Java Servlet 规范

您还可以阅读这篇Java Servlet 基础

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.

The servlets are placed under below folder location

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

So, what should be the entries for servlets in url-pattern and servlet-mapping. So that, servlet can be accessible through URL.

解决方案

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>

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 Specification:

  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.

Reference : Java Servlet Specification

You may also read this Basics of Java Servlet

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

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