Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2014
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. A servlet container may send concurrent requests through the service method of
  2. the servlet. To handle the requests the developer of the servlet must make adequate
  3. provisions for concurrent processing with multiple threads in the service method.
  4. An alternative for the developer is to implement the SingleThreadModel
  5. interface which requires the container to guarantee that there is only one request
  6. thread at a time in the service method. A servlet container may satisfy this
  7. requirement by serializing requests on a servlet, or by maintaining a pool of servlet
  8. instances. If the servlet is part of a web application that has been marked as
  9. distributable, the container may maintain a pool of servlet instances in each VM that
  10. the application is distributed across.
  11.  
  12. For servlets not implementing the SingleThreadModel interface, if the
  13. service method (or methods such as doGet or doPost which are dispatched to the
  14. service method of the HttpServlet abstract class) has been defined with the
  15. synchronized keyword, the servlet container cannot use the instance pool
  16. approach, but must serialize requests through it.
  17.  
  18. public class Person {
  19. private String firstName;
  20. private String lastName;
  21.  
  22. public void setName(String firstName, String lastName) {
  23. this.firstName = firstName;
  24. this.lastName = lastName;
  25. }
  26.  
  27. ...getter methods omitted
  28. }
  29.  
  30. public class Person {
  31. private String firstName;
  32. private String lastName;
  33.  
  34. public Person(String firstName, String lastName) {
  35. this.firstName = firstName;
  36. this.lastName = lastName;
  37. }
  38.  
  39. public String getFirstName( ) { return this.firstName; }
  40. public String getLastName( ) { return this.lastName; }
  41. }
  42.  
  43. Immutable objects are not always an option but can be a useful technique for many
  44. smaller "data helper" classes that seem to pop up in every application.
  45.  
  46. public class HomeServlet extends HttpServlet {
  47. private Customer currentCust;
  48.  
  49. protected void doGet(HttpServletRequest request,
  50. HttpServletResponse response) throws IOException,
  51. ServletException {
  52. HttpSession session = request.getSession(true);
  53. currentCust = (Customer) session.getAttribute("cust");
  54. currentCust.setLastAccessedTime(new Date( ));
  55. ...
  56. }
  57. }
  58.  
  59. public class HomeServlet extends HttpServlet {
  60. protected void doGet(HttpServletRequest request,
  61. HttpServletResponse response) throws IOException,
  62. ServletException {
  63. HttpSession session = request.getSession(true);
  64. Customer currentCust = (Customer) session.getAttribute("cust");
  65. currentCust.setLastAccessedTime(new Date( ));
  66. ...
  67. }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement