Microsoft web hosting - JavaSercer Pages 8.2.1.1 Thread-safe beans You probably noticed

JavaSercer Pages 8.2.1.1 Thread-safe beans You probably noticed that the CounterBean doesn’t have the type of property getter method used in the examples in previous chapters. Instead, it has a method named getNextValue( ) that takes the URI for the page as an argument. The action can be used only to display properties when the bean implements the standard no-arguments getter methods, so in Example 8.4, JSP expressions are used to display the counter values instead. The use of a method with an argument instead of a typical no-argument JavaBeans getter method is by design, and highlights a very important consideration for beans used in the session and application scopes. In Chapter 6, we discussed how multiple requests are handled by the server in parallel by separate threads. The server assigns one execution thread to each request, and all threads have access to the same variable values for variables declared with JSP declaration elements. The result is that if such a variable is changed while processing one page request, the new values are used by all other requests as well. Beans in the session and application scopes are open to the same kind of problem, since they are also shared by multiple threads. To illustrate this, let’s look at what could happen if we used a bean with the traditional setter and getter methods to implement the counter instead. Such a bean could have the properties shown in Table 8.1. Table 8.1, Traditional Counter Bean Properties Property Name Java Type Access Description uri String write The unique URI used to identify the page counter nextValue int read The counter’s value incremented by one read-only property Part of the JSP page would then look like this: times by all users since the application was started. The first time you access this page, the action creates the bean and saves it in the application scope. The action sets the uri property to the unique URI for this page. Then the action calls the nextValue property getter method. This method uses the uri property value to locate the counter for the page and increments it. As long as you use this bean in only one page, this works fine. But if you want to keep track of all hits for two pages, each with its unique URI, and the two pages are requested at almost the same time, you’re in trouble. Say one user requests the first page, called /ora/pageOne. This page sets the uri property to /ora/pageOne. But before it gets to the action, another user requests the second page, say /ora/pageTwo. Now the second page finds the same bean (applCounter) and sets the uri property to its URI (/ora/pageTwo). So when the first page eventually executes the action, it increments and displays the counter for the second page instead of its own counter. Here I used a bean in the application scope as an example, because it’s easy to understand that beans shared by all users can be accessed by more than one request at a time. But session scope beans can also be accessed at roughly the same time, by the same user. A good example of when two requests are made at nearly the same time by the same user is when an HTML frame set is used and each frame contains a JSP page. page 96
Quick Hint: If you are looking for cheap and reliable provider to host and run your servlet application check Vision servlet hosting plans

Comments are closed.