显示带有 url 模式“/*”的转发 JSP

发布于 2024-10-19 06:53:24 字数 1629 浏览 2 评论 0原文

为了提高我的 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 技术交流群。

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

发布评论

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

评论(2

情话难免假 2024-10-26 06:53:24

对于前端控制器方法来说,Filter 是一个不合适的解决方案。

您想要优化 servlet 的 url-pattern,使其匹配 /pages/**.do。您不希望前端控制器介入不相关的请求,例如 CSS/JS/图像/等。以/pages/*为例,假设您在/WEB-INF/foo.jsp中有一个JSP,那么servlet中的以下内容

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    request.getRequestDispatcher("/WEB-INF" + request.getPathInfo() + ".jsp").forward(request, response);
}

应该显示: 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 servlet

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    request.getRequestDispatcher("/WEB-INF" + request.getPathInfo() + ".jsp").forward(request, response);
}

should display the JSP in question on http://localhost:8080/contextname/pages/foo.

See also:

微暖i 2024-10-26 06:53:24

我认为这里的问题可能是您使用的是 Servlet 而不是 ServletFilter

ServletFilter,顾名思义,通过对请求进行预处理和后处理来过滤请求。如果您需要执行以下操作,您可能需要使用过滤器:

  • 在整个应用程序中提供安全检查
  • 设置由 servlet 或 jsp 获取的请求属性
  • 压缩响应
  • 记录计时信息
  • 等等。

看看有关 Servlet 过滤器 的文档。

I think the problem here might be that you're using a Servlet instead of a ServletFilter.

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:

  • Provide security checks across an entire application
  • Set request properties that are picked up by a servlet or jsp
  • Compress a response
  • Log timing information
  • Etc.

Have a look at the documentation about Servlet Filters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文