Servlet doFilter setAttributes 在 Servlet 中不可用

发布于 12-20 08:46 字数 156 浏览 3 评论 0原文

我试图在将请求传递给 Servlet 之前通过 doFilter 添加一个属性,以便在 JSTL 中设置一个值。我只是在过滤器中执行 req.setAttribute("b", "blah") ,但它似乎没有在 JSTL 文件中设置。我该怎么做?

I'm trying to add an Attribute to the request via doFilter before passing it to the Servlet, so that a value in JSTL will be set. I simply do req.setAttribute("b", "blah") in the filter, but it doesn't seem to get set in the JSTL file. How would I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

她比我温柔2024-12-27 08:46:06

如果不查看代码,很难查明根本原因。有几种可能的原因。

  • 您在设置属性后发送重定向,而不是继续发送相同的请求。
  • 您正在使用错误的名称访问属性(区分大小写!)。
  • 您访问该属性的方式错误。
  • 该属性在请求处理过程中已被重写。
  • 有一个同名的页面范围属性没有值。
  • 您误解了结果。
  • 等等。

顺便说一下,不存在“JSTL 文件”这样的东西。也许您的意思是“JSP 文件”。

It's hard to pinpoint the root cause without seeing the code. There are several possible causes.

  • You're sending a redirect after setting the attribute instead of continuing with the same request.
  • You're accessing the attribute with the wrong name (case sensitive!).
  • You're accessing the attribute the wrong way.
  • The attribute is been overridden somewhere further down in the request processing.
  • There's a page scoped attribute with the same name which has no value.
  • You're misinterpreting the results.
  • Etc.

By the way, there's no such thing as a "JSTL file". Perhaps you meant "JSP file".

白况2024-12-27 08:46:06

问题是 doFilter 方法使用 ServletRequest 而不是具有 setAttribute 方法的 HttpServletRequest 。我的大多数过滤器都是这样的:

public void doFilter(ServletRequest servletRequest, 
                     ServletResponse servletResponse, 
                     FilterChain chain) throws IOException, ServletException {

    HttpServletRequest request = (HttpServletRequest) servletRequest;
    HttpServletResponse response = (HttpServletResponse) servletResponse;                                                                      

    // do what you must...

    chain.doFilter(servletRequest, servletResponse);

}

The problem is that the doFilter method uses ServletRequest instead of HttpServletRequest which is the one that has the setAttribute method. Most of my filters are something like this:

public void doFilter(ServletRequest servletRequest, 
                     ServletResponse servletResponse, 
                     FilterChain chain) throws IOException, ServletException {

    HttpServletRequest request = (HttpServletRequest) servletRequest;
    HttpServletResponse response = (HttpServletResponse) servletResponse;                                                                      

    // do what you must...

    chain.doFilter(servletRequest, servletResponse);

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