简体   繁体   English

从servlet映射中剥离图像,CSS和JS

[英]Strip Images, CSS and JS from servlet-mapping

I am using the following servlet-mapping in my web.xml file: 我在web.xml文件中使用以下servlet映射:

<servlet>
    <servlet-name>PostController</servlet-name>
    <servlet-class>com.webcodei.controller.PostController</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>PostController</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping> 

To do some kind of a search. 做某种搜索。 ex: 例如:

 http://www.myweb.com/The search string here 

But the problem is that CSS, JS and Images are treated like a search request. 但是问题在于CSS,JS和图片被视为搜索请求。

There are any patterns that strip out *.css, *.js, *.gif and etc, so the requests don't need to pass through my controller? 有任何模式可以去除* .css,*。js,*。gif等,因此请求不需要通过我的控制器吗?

Thank you so much, bye bye! 非常感谢,再见!

Two options come to mind: 我想到两个选择:

1) Typically, in web app like this, the "action" URLs that are handled by a servlet, are given either a sub-directory like " /actions/* " or are given an extension like " *.action " or " *.do " (this is what Struts does). 1)通常,在像这样的Web应用程序中,由servlet处理的“操作” URL被赋予一个子目录,例如“ /actions/* ”,或者被赋予一个扩展名,例如“ *.action ”或“ *.do*.do ”(这是Struts所做的)。 This way it's clear which URLs are intended for the servlet. 这样,很清楚哪些URL用于servlet。 This is more of an inclusive solution, rather than the exclusive one you're asking for, but I don't think what you want is possible. 这更多是一种包容性解决方案,而不是您要的专有解决方案,但是我认为您想要的是不可能的。

2) The slightly more adventurous option is to set up your web app server behind an apache install that serves up the images, css, etc as flat files, sending only everything else onto the app server. 2)比较冒险的选择是在apache安装之后设置Web应用程序服务器,该安装程序将图像,css等作为平面文件提供,仅将其他所有内容发送到应用程序服务器上。 Typically, this is done to take the load off your app server. 通常,这样做是为了减轻应用服务器的负担。 It would require you to copy all these files to a separete directory for apache to handle. 这将需要您将所有这些文件复制到一个单独的目录中,以供apache处理。

Rather than blacklisting certain extensions, you might consider whitelisting the URL patterns that reach your PostController servlet instead. 您可以考虑将到达您的PostController servlet的URL模式列入白名单,而不是将某些扩展列入黑名单。 For instance: 例如:

 <servlet>
    <servlet-name>PostController</servlet-name>
    <servlet-class>com.webcodei.controller.PostController</servlet-class>
 </servlet>
 <servlet-mapping>
    <servlet-name>PostController</servlet-name>
    <url-pattern>/*.jsp</url-pattern>
 </servlet-mapping>

if you are using simple JSPs. 如果您使用的是简单的JSP。 Now, HTTP GET requests for files with extension *.css, *.gif, etc. will not be routed through this servlet. 现在,对扩展名为* .css,*。gif等的文件的HTTP GET请求将不会通过此servlet进行路由。

As the questioner pointed out, there are many more URLs that should not be routed through this controller than otherwise. 正如发问者所指出的那样,不应通过此控制器路由的URL比其他方法要多得多。

Jetty interprets web.xml as you're expecting it to. Jetty会按您期望的那样解释web.xml。 I exposed this problem recently when I moved an application from jetty to tomcat, and all of a sudden couldn't see my static resources anymore. 最近,当我将应用程序从码头转移到tomcat时,我暴露了这个问题,突然之间看不到我的静态资源。 Very frustrating. 非常沮丧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM