【问题标题】:jsp:setProperty for bean not working properlybean 的 jsp:setProperty 无法正常工作
【发布时间】:2009-12-02 13:52:29
【问题描述】:

您好,我遇到了设置属性标签无法正常工作的问题。我有一个 jsp,我将它作为 portlet 包含在 webcenter 中。

<jsp:useBean id="pathEditor" class="backing.bean.AppletBean" scope="page"/>

<jsp:getProperty name="pathEditor" property="username" />
${pageContext.request.remoteUser}
<jsp:setProperty name="pathEditor" property="username" value="${pageContext.request.remoteUser}"/>
<jsp:getProperty name="pathEditor"  property="username" />

我正在从同一个 m/c 的两个不同浏览器登录。第一个用户名值正确,而第二个登录打印${pageContext.request.remoteUser} 正确,但&lt;jsp:getProperty name="pathEditor" property="username" /&gt; 打印前一个登录用户。它给人的印象是 setProperty 根本没有被调用。任何人都可以建议这里可能有什么问题。我正在使用两个不同的浏览器并且没有静态变量。我为这个测试用例保持两个浏览器打开。可能是因为在 webcenter 中处理 portlet 的方式。如果我将 bean 的范围声明为页面,这是否不是线程安全的正确方法。我能做些什么来使它线程安全?我使 bean 属性变量 volatile 但这没有任何好处。或者我有可能在使用后破坏豆子?如何销毁 bean?

所以如果我在 jsp 中包含这个 - ,那应该可以工作。但它也不起作用。

Edit# 调试代码后,我看到了这种不寻常的行为。 我将 System.out.println 放在我的 bean 中,以查看来自 jsp 的值。 虽然 ${pageContext.request.remoteUser} 打印新值 - jsp:setProperty name="pathEditor" property="username" value="${pageContext.request.remoteUser}"/> 将旧值传递给我的 bean setter 方法。这我无法理解。请帮忙。

【问题讨论】:

    标签: jsp properties set javabeans


    【解决方案1】:

    这两种浏览器有何不同? 相同浏览器的不同选项卡/窗口/实例都将共享相同会话。使用不同品牌的浏览器进行更好的测试,例如一个 Firefox 和其他 IE、Safari、Chrome 或 Opera。

    如果您实际上是在使用不同品牌的浏览器进行测试,但仍然遇到同样的问题,那么代码很可能不是线程安全的。 IE。您将变量声明为某个类中的 static 变量或 Servlet 类的 instance 变量。你知道,static 变量在所有线程之间共享,就像 Servlet 类的实例变量一样。

    编辑 #1: 作为对您自己编辑的回复:代码根本不是线程安全的。毫无疑问,在某种程度上,static 或 servlet 实例变量 (in) 直接保存信息。从这个距离很难指出确切的代码行。只需运行调试器并调试代码,或在此处发布SSCCE,或让当地专家审核您的代码。

    编辑#2: jsp/servlets 中的线程安全与是否使用 synchronized/volatile/etc 无关。这只是关于编写正确的代码。 static 变量不是线程安全的。在 servlet doXXX() 方法中声明的所有内容都是线程安全的,但外部不是。那种东西。请记住:一个 HTTP 请求计为一个线程。在应用程序的生命周期内只有一个 servlet 实例。

    例子:

    public class MyBean { 
        private static String property1; // Certainly NOT threadsafe, there is only 1 of it during application's lifetime.
        private String property2; // Threadsafety depends on class which is holding the Bean instance.
    }
    

    public class MyServlet extends HttpServlet {
        private static Bean bean1 = new Bean(); // Certainly NOT threadsafe, there is only 1 of it during application's lifetime.
        private Bean bean2 = new Bean(); // This also NOT, because there's only 1 servlet in application's lifetime which is shared among all requests.
    
        protected void doSomething(request, response) {
            Bean bean3 = new Bean(); // Declared threadlocal, thus certainly threadsafe.
            request.setAttribute("bean", bean3); // 1 request = 1 thread, thus threadsafe.
            request.getSession().setAttribute("bean", bean3); // Session is shared among requests/threads from same client, thus NOT threadsafe.
            getServletContext().setAttribute("bean", bean3); // Context is shared among all sessions from different clients, thus certainly NOT threadsafe.
        }
    }
    

    JSP 中的页面范围当然是线程安全的。你的问题在于更高的层次。我在某处打赌static 变量。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-01
      • 2016-08-12
      • 1970-01-01
      • 2016-09-01
      • 2012-07-11
      • 2018-04-08
      • 2017-04-20
      • 2018-10-02
      相关资源
      最近更新 更多