显示带有 url 模式“/*”的转发 JSP
为了提高我的 java 技能,我正在尝试构建一个简单的 j2ee 框架 (MVC)。
我构建它是为了处理 FrontServlet 中的每个请求。这是我使用的映射:
web.xml :
<servlet>
<servlet-name>Front</servlet-name>
<servlet-class>test.FrontServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Front</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
我的问题是,当我将请求从 FrontServlet 转发到 JSP 时,显然,JSP 请求由 FrontServlet 处理,并且视图未呈现。
- < strong>如何通过保留 url-pattern“/*”来解决这个问题?
- 有没有一种方法可以在 Servlet 中呈现 JSP 而不会造成性能损失?
提前感谢您的帮助回复 !
- 解决方案 1 (@Bryan Kyle)
我正在尝试遵循你的建议。我创建了这个过滤器:
public void doFilter(ServletRequest request,
ServletResponse response, FilterChain chain)
throws IOException, ServletException
{
HttpServletRequest req = (HttpServletRequest) request;
if(!req.getRequestURL().toString().endsWith("jsp"))
{
// I changed the servlet url-pattern to "/front.controller"
req.getRequestDispatcher("/front.controller").forward(req, response);
/*chain.doFilter(req, resp);*/
}
}
<filter>
<filter-name>Filter</filter-name>
<filter-class>test.Filter</filter-class>
</filter>
<filter-mapping>
<filter-name>Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
- 是吗?
谢谢!
To improve my java skills, I'm trying to build a simple j2ee framework (MVC).
I built it to handle every request in a FrontServlet. Here is the mapping that I used :
web.xml :
<servlet>
<servlet-name>Front</servlet-name>
<servlet-class>test.FrontServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Front</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
My problem is that when I forward the request from the FrontServlet to a JSP, obviously, the JSP request is handle by the FrontServlet and the view isn't rendered.
- How can I resolve this problem by keeping the url-pattern "/*" ?
- Is there a way to render a JSP in a Servlet without performance losses ?
Thanks in advance for your reply !
- Solution 1 (@Bryan Kyle)
I'm trying to follow your advise. I created this filter :
public void doFilter(ServletRequest request,
ServletResponse response, FilterChain chain)
throws IOException, ServletException
{
HttpServletRequest req = (HttpServletRequest) request;
if(!req.getRequestURL().toString().endsWith("jsp"))
{
// I changed the servlet url-pattern to "/front.controller"
req.getRequestDispatcher("/front.controller").forward(req, response);
/*chain.doFilter(req, resp);*/
}
}
<filter>
<filter-name>Filter</filter-name>
<filter-class>test.Filter</filter-class>
</filter>
<filter-mapping>
<filter-name>Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
- Is it right?
Thanks !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于前端控制器方法来说,
Filter
是一个不合适的解决方案。您想要优化 servlet 的
url-pattern
,使其匹配/pages/*
或*.do
。您不希望前端控制器介入不相关的请求,例如 CSS/JS/图像/等。以/pages/*
为例,假设您在/WEB-INF/foo.jsp
中有一个JSP,那么servlet中的以下内容应该显示: http://localhost:8080/contextname/pages/foo 上有问题的 JSP。
另请参阅:
A
Filter
is an inappropriate solution for a front controller approach.You want to refine the
url-pattern
of your servlet so that it matches e.g./pages/*
or*.do
. You don't want your front controller to kick in on irrelevant requests like CSS/JS/images/etc. To take/pages/*
as an example, assuming that you've a JSP in/WEB-INF/foo.jsp
, then the following in a servletshould display the JSP in question on http://localhost:8080/contextname/pages/foo.
See also:
我认为这里的问题可能是您使用的是
Servlet
而不是ServletFilter
。ServletFilter,顾名思义,通过对请求进行预处理和后处理来过滤请求。如果您需要执行以下操作,您可能需要使用过滤器:
看看有关 Servlet 过滤器 的文档。
I think the problem here might be that you're using a
Servlet
instead of aServletFilter
.A ServletFilter, as the name suggests filters requests by providing pre- and post-processing on the request. You'd probably want to use a Filter if you needed to do something like the following:
Have a look at the documentation about Servlet Filters.