jsp:setProperty for bean not working properly
Hi I am having issue of set property tag not working properly. I have a jsp which I am including in webcenter as portlet.
<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" />
I am doing login from two different browsers from same m/c. user name value in first comes correct, while the second login prints ${pageContext.request.remoteUser}
correct but <jsp:getProperty name="pathEditor" property="username" />
prints the previous logged in user.It gives impression that setProperty is not getting called at all .can any body suggest what might be wrong here. I am using two different browsers and no static variable.I keep both browser open for this test case. can it be becos of the way portlets are handled in webcenter. if I am declaing scope of bean as page, is it not correct way for thread safety. what I can do to make it thread safe? I have made bean property variables volatile but that does not does any good. or is it possible that I destroy the bean after use? How can I destroy the bean?
so if I include this - <%@page isThreadSafe="false" %> in jsp, that should work. but it is also not working.
Edit# After debugging code I saw this unusual behaviour. I put System.out.println in my bean for seeing what value coming from jsp. though ${pageContext.request.remoteUser} prints new value - jsp:setProperty name="pathEditor" property="username" value="${pageContext.request.remoteUser}"/> passes old value to my bean setter method. this I am not able to understand. Please help.
How different are the two browsers? Different tabs/windows/instances of the same browser make will all share the same session. Better test using browsers of different make, e.g. one Firefox and other IE, Safari, Chrome or Opera.
If you're actually testing with browsers of different make and you're still encountering the same problem, then the code is most likely not threadsafe. I.e. you declared variables as a static
variable in some class or as an instance variable of a Servlet class. You know, static
variables are shared among all threads as are instance variables of a Servlet class.
Edit #1: as reply on your own edit: the code is simply not threadsafe. There's undoubtely at some level a static
or servlet instance variable (in)directly holding the information. It is hard to point the exact code line from this distance. Just run a debugger and debug the code, or post an SSCCE here, or have an local expert to review your code.
Edit #2: thread safety in jsp/servlets has nothing to do with whether to use synchronized/volatile/etc. It's just all about writing proper code. A static
variable is NOT threadsafe. Everything declared inside a servlet doXXX() method is threadsafe, but outside is NOT. That kind of things. Keep in mind: one HTTP request accounts as one thread. There is only one servlet instance during application's lifetime.
Example:
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.
}
and
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.
}
}
The page scope in JSP is certainly threadsafe. Your problem lies at higher levels. My bet on a static
variable somewhere.
精彩评论