Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- A servlet container may send concurrent requests through the service method of
- the servlet. To handle the requests the developer of the servlet must make adequate
- provisions for concurrent processing with multiple threads in the service method.
- An alternative for the developer is to implement the SingleThreadModel
- interface which requires the container to guarantee that there is only one request
- thread at a time in the service method. A servlet container may satisfy this
- requirement by serializing requests on a servlet, or by maintaining a pool of servlet
- instances. If the servlet is part of a web application that has been marked as
- distributable, the container may maintain a pool of servlet instances in each VM that
- the application is distributed across.
- For servlets not implementing the SingleThreadModel interface, if the
- service method (or methods such as doGet or doPost which are dispatched to the
- service method of the HttpServlet abstract class) has been defined with the
- synchronized keyword, the servlet container cannot use the instance pool
- approach, but must serialize requests through it.
- public class Person {
- private String firstName;
- private String lastName;
- public void setName(String firstName, String lastName) {
- this.firstName = firstName;
- this.lastName = lastName;
- }
- ...getter methods omitted
- }
- public class Person {
- private String firstName;
- private String lastName;
- public Person(String firstName, String lastName) {
- this.firstName = firstName;
- this.lastName = lastName;
- }
- public String getFirstName( ) { return this.firstName; }
- public String getLastName( ) { return this.lastName; }
- }
- Immutable objects are not always an option but can be a useful technique for many
- smaller "data helper" classes that seem to pop up in every application.
- public class HomeServlet extends HttpServlet {
- private Customer currentCust;
- protected void doGet(HttpServletRequest request,
- HttpServletResponse response) throws IOException,
- ServletException {
- HttpSession session = request.getSession(true);
- currentCust = (Customer) session.getAttribute("cust");
- currentCust.setLastAccessedTime(new Date( ));
- ...
- }
- }
- public class HomeServlet extends HttpServlet {
- protected void doGet(HttpServletRequest request,
- HttpServletResponse response) throws IOException,
- ServletException {
- HttpSession session = request.getSession(true);
- Customer currentCust = (Customer) session.getAttribute("cust");
- currentCust.setLastAccessedTime(new Date( ));
- ...
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement