发布于 2024-11-18 09:31:45 字数 971 浏览 1 评论 0原文

情况 1

  <servlet-mapping>
    <servlet-name>myapp</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

情况 2

  <servlet-mapping>
    <servlet-name>myapp</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

如果我使用情况 1,那么我的任何页面都不会使用 ,但如果我使用案例 2,所有内容都会被设置样式。

有人可以帮助我理解为什么吗?

另外,有人可以告诉我应该使用哪种模式,这样我就不必担心扩展吗?就像我应该使用 /* 吗?问题是,如果我现在在开发应用程序时使用 *.do 时使用 /* ,一切似乎都会被破坏,不仅是样式,而且我没有渲染任何图片,没有 JCaptcha 以及所有与链接有关的内容。

如果我尝试从 REST 客户端(如 http://localhost:8080/myapp/user/1)发送 GET 请求,它不起作用,我需要添加 .do< /code> 结尾并发送相同的请求,如 http://localhost:8080/myapp/user/1.do

谢谢。

Case 1

  <servlet-mapping>
    <servlet-name>myapp</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

Case 2

  <servlet-mapping>
    <servlet-name>myapp</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

If I use case 1 then I dont get any of my pages styled with <link rel="stylesheet" type="text/css" href="${contextPath}/assets/styles.css" />, but if I use case 2 everything gets styled.

Could someone help me understand why?

Also, could someone tell me which pattern should be used so that I don't have to worry about extensions? Like should I be using /*? The thing is that if I use /* now when I've been using *.do while developing my application, everything seems to be breaking, not only the styles but I don't get any pictures rendered, no JCaptcha and all that has to do with links.

And If I try to send a GET request from a REST Client like http://localhost:8080/myapp/user/1 it doesn't work and I need to add .do at the end and send the same request like http://localhost:8080/myapp/user/1.do.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

双马尾 2024-11-25 09:31:45

浏览器对链接资源(例如 CSS 文件、JS 文件和图像)发送单独的 HTTP 请求。这些请求的 URL 也与 / 的 URL 模式匹配。因此,您的 myapp servlet 也会根据这些请求进行调用。但是,您的 myapp servlet 似乎无法正确处理它们,因此这些请求返回完全不同的内容。尝试单独请求这些资源,以了解您的 servlet 实际上返回到 Web 浏览器的内容:

http://localhost:8080/myapp/assets/styles.css< /p>

在您的情况下,您想让您的 myapp servlet 忽略对这些资源的请求。最好的方法是创建一个过滤器来执行此操作。假设所有这些资源都位于名为 /assets 的文件夹中,那么您可以通过将 servlet 映射到更具体的 URL 模式来实现此目的,例如 /myapp/* 并创建一个监听 /*Filter,它透明地继续 /assets 上任何请求的请求/响应链,并分派所有其他请求到 /myapp

因此,此配置

<filter>
    <filter-name>filter</filter-name>
    <filter-class>com.example.Filter</filter-class>
</filter>
<filter-mapping>
    <filter-name>filter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
    <servlet-name>controller</servlet-name>
    <servlet-class>com.example.Controller</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>controller</servlet-name>
    <url-pattern>/myapp/*</url-pattern>
</servlet-mapping>

与过滤器的 doFilter(): 中的以下内容相结合

String uri = ((HttpServletRequest) request).getRequestURI();
if (uri.startsWith("/assets")) {
    chain.doFilter(request, response); // Goes to container's default servlet.
} else {
    request.getRequestDispatcher("/myapp" + uri).forward(request, response);
}

应该适合您。

Browsers sends separate HTTP requests on linked resources such as CSS files, JS files and images. The URLs of those requests also match the URL pattern of /. So your myapp servlet is also called on those requests. However, your myapp servlet doesn't seem to process them properly, so those requests returns something entirely different. Try requesting those resources yourself separately to learn what your servlet is actually returning to the webbrowser:

http://localhost:8080/myapp/assets/styles.css

In your case, you want to let your myapp servlet ignore requests on those resources. The best way is to create a filter which does that. Assuming that all of those resources are in a folder called /assets, then you can achieve this by mapping your servlet on a more specific URL pattern, such as for example /myapp/* and creating a Filter listening on /* which transparently continues the request/response chain for any requests on /assets and dispatches all other requests to /myapp.

So, this configuration

<filter>
    <filter-name>filter</filter-name>
    <filter-class>com.example.Filter</filter-class>
</filter>
<filter-mapping>
    <filter-name>filter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
    <servlet-name>controller</servlet-name>
    <servlet-class>com.example.Controller</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>controller</servlet-name>
    <url-pattern>/myapp/*</url-pattern>
</servlet-mapping>

in combination with the following in filter's doFilter():

String uri = ((HttpServletRequest) request).getRequestURI();
if (uri.startsWith("/assets")) {
    chain.doFilter(request, response); // Goes to container's default servlet.
} else {
    request.getRequestDispatcher("/myapp" + uri).forward(request, response);
}

should work for you.

回心转意 2024-11-25 09:31:45

以下是 Oracle J2EE 教程中的相关页面: http://download.oracle.com/docs/cd/E13222_01/wls/docs81/webapp/components.html#148787

本页给出了一些示例,并引导读者参考 Servlet 规范:

有关 servlet 映射的更多信息,例如一般 servlet 映射规则和约定,请参阅 Servlet 2.3 规范的第 11 节:http://www.jcp.org/aboutJava/communityprocess/final/jsr053/

该规范值得一读,但您需要单击“我同意”即可下载 PDF,因此我无法直接链接到它。


规范所述的摘要是使用以下规则(按顺序):

  1. 尝试执行精确匹配。
  2. 尝试进行前缀匹配,优先考虑最长前缀的匹配。
  3. 尝试进行后缀匹配。
  4. “容器将
    尝试提供适合所请求资源的内容。如果是“默认”
    servlet 是为应用程序定义的,它将被使用。”

Here is the relevant page from the Oracle J2EE Tutorial: http://download.oracle.com/docs/cd/E13222_01/wls/docs81/webapp/components.html#148787

This page gives some examples, and refers the reader to the Servlet specs:

For more information on servlet mappings, such as general servlet mapping rules and conventions, refer to Section 11 of the Servlet 2.3 specification at: http://www.jcp.org/aboutJava/communityprocess/final/jsr053/

The spec is worth a read, but you need to click an "I agree" to download the PDF, so I can't link to it directly.


A summary of what the spec says is that the following rules are used (in order):

  1. An attempt is made to perform an exact match.
  2. An attempt is made to do a prefix match, with priority given to the match with the longest prefix.
  3. An attempt is made to do a suffix match.
  4. "[T]he the container will
    attempt to serve content appropriate for the resource requested. If a "default"
    servlet is defined for the application, it will be used."
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文