Java JSP / Servlet:控制器servlet抛出着名的堆栈溢出 [英] Java JSP/Servlet: controller servlet throwing the famous stack overflow
问题描述
我已经阅读了几篇文档但我没理解:我知道我做错了但我不明白。我有一个完全动态生成的网站:几乎没有任何静态内容。
I've read several docs and I don't get it: I know I'm doing something wrong but I don't understand what. I've got a website that is entirely dynamically generated: there's hardly any static content at all.
所以,试着理解JSP / Servlet,我写了自己的前端控制器拦截每一个查询,它看起来像这样:
So, trying to understand JSP/Servlet, I've written my own "front controller" intercepting every single query, it looks like this:
<servlet-mapping>
<servlet-name>defaultservlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
基本上我想要任何用户请求,例如:
Basically I want any user request, like:
- example.org
- example.org/bar
- example.org/foo.html
所有人都通过我编写的默认servlet。
to all go through a default servlet which I've written.
servlet然后检查URI并找到必须分派的 .jsp 请求,然后在正确设置所有属性之后,a:
The servlet then examines the URI and find to which .jsp the request must be dispatched, and then does, after having set all the attributes correctly, a:
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/WEB-INF/jsp/index.jsp");
dispatcher.forward(req, resp);
当我使用url-pattern时(在 web.xml 中)比如说, * .html
,一切正常。但当我将其更改为 / *
(为了真正拦截所有内容)时,我进入一个无限循环,最终得到一个... StackOverflow:)
When I'm using a url-pattern (in web.xml) like, say, *.html
, everything works fine. But when I change it to /*
(to really intercept everything), I enter an endless loop and it ends up with a... StackOverflow :)
当调度请求时,URI ... / WEB-INF / jsp / index.jsp本身是否与 web匹配。我设置的xml 过滤器/ *?
When the request is dispatched, is the URI ".../WEB-INF/jsp/index.jsp" itself matched by the web.xml filter /* that I set?
编辑显然,不,因为这是索引的精确映射.jsp 因此它绕过web.xml url-pattern。所以我仍然无法理解如何进入无限循环。
EDIT apparently, no, because this is an exact mapping to index.jsp and hence it bypasses the web.xml url-pattern. So I still don't get how I can enter that endless loop.
如果我想使用/ * url-pattern拦截所有内容,我该怎么办?能够派遣/转发/?
How should I do if I want to intercept everything using a /* url-pattern and yet be able to dispatch/forward/?
我不是在这里询问规格/ Javadocs:我对大局感到困惑,我需要解释一下可能发生的事情。
I'm not asking about specs/Javadocs here: I'm really confused about the bigger picture and I'd need some explanation as to what could be going on.
我不应该拦截真的一切吗?
Am I not supposed to intercept really everything?
如果我可以拦截所有内容,我应该注意哪些转发/调度?
If I can intercept everything, what should I be aware of regarding forwarding/dispatching?
推荐答案
不幸的是,Serlvet规范不允许创建servlet映射以仅匹配传入请求,而不是转发。但是,这可以用于过滤器映射(默认情况下,过滤器映射仅匹配传入请求)。
Unfortunately, Serlvet spec doesn't allow to create a servlet mapping to match only incoming request, not forwards. However, this can be done for filter mappings (and by default filter mappings match only incoming requests).
因此,使用单个servlet拦截所有内容的典型解决方案是使用 UrlRewriteFilter :
So, the typical solution for intercepting everything with a single servlet is to use a UrlRewriteFilter:
<filter>
<filter-name>urlRewrite</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>urlRewrite</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>application</servlet-name>
<servlet-class>...</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>application</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
/WEB-INF/urlrewrite.xml
:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite
PUBLIC "-//tuckey.org//DTD UrlRewrite 3.0//EN"
"http://tuckey.org/res/dtds/urlrewrite3.0.dtd">
<urlrewrite default-match-type="wildcard">
<rule>
<from>/**</from>
<to>/app/$1</to>
</rule>
<outbound-rule>
<from>/app/**</from>
<to>/$1</to>
</outbound-rule>
</urlrewrite>
这种方式还允许您指定 / * $ c中的例外情况$ c>静态文件的映射。
This way also allows you to specify exceptions from /*
mapping for static files.
这篇关于Java JSP / Servlet:控制器servlet抛出着名的堆栈溢出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!