在中
情况 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
浏览器对链接资源(例如 CSS 文件、JS 文件和图像)发送单独的 HTTP 请求。这些请求的 URL 也与
/
的 URL 模式匹配。因此,您的myapp
servlet 也会根据这些请求进行调用。但是,您的myapp
servlet 似乎无法正确处理它们,因此这些请求返回完全不同的内容。尝试单独请求这些资源,以了解您的 servlet 实际上返回到 Web 浏览器的内容:在您的情况下,您想让您的
myapp
servlet 忽略对这些资源的请求。最好的方法是创建一个过滤器来执行此操作。假设所有这些资源都位于名为/assets
的文件夹中,那么您可以通过将 servlet 映射到更具体的 URL 模式来实现此目的,例如/myapp/* 并创建一个监听
/*
的Filter
,它透明地继续/assets
上任何请求的请求/响应链,并分派所有其他请求到/myapp
。因此,此配置
与过滤器的
doFilter()
: 中的以下内容相结合应该适合您。
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 yourmyapp
servlet is also called on those requests. However, yourmyapp
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: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 aFilter
listening on/*
which transparently continues the request/response chain for any requests on/assets
and dispatches all other requests to/myapp
.So, this configuration
in combination with the following in filter's
doFilter()
:should work for you.
以下是 Oracle J2EE 教程中的相关页面: http://download.oracle.com/docs/cd/E13222_01/wls/docs81/webapp/components.html#148787
本页给出了一些示例,并引导读者参考 Servlet 规范:
该规范值得一读,但您需要单击“我同意”即可下载 PDF,因此我无法直接链接到它。
规范所述的摘要是使用以下规则(按顺序):
尝试提供适合所请求资源的内容。如果是“默认”
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:
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):
attempt to serve content appropriate for the resource requested. If a "default"
servlet is defined for the application, it will be used."