Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS-4220 Dr. Mark L. Hornick1 Single class doing too many things =Bad Code Smell.

Similar presentations


Presentation on theme: "CS-4220 Dr. Mark L. Hornick1 Single class doing too many things =Bad Code Smell."— Presentation transcript:

1 CS-4220 Dr. Mark L. Hornick1 Single class doing too many things =Bad Code Smell

2 SE-2840 Dr. Mark L. Hornick2 Servlet Threads and Servlet Contexts

3 SE-2840 Dr. Mark L. Hornick3 Servlet execution What are some ramifications of running each doGet() or doPost() on a separate thread??

4 What can happen here? SE-2840 Dr. Mark L. Hornick4 User 1 User 1 hits Submit on a form page. Thread 19 User 2 Thread 20 Data store User 2 hits Submit on the same form page at about the same time. doPost(request, response) Assume the Datastore is managed via a Servlet-owned reference. Composition (“has a”)

5 Multithreading is a fact of a Servlet’s life The only code objects that are thread-safe are the ones that are stack-based (or readonly): HttpServletRequest object HttpServletResponse object Local Servlet method variables Servlet class/instance constants These are NOT thread-safe: Servlet class or instance variables SE-2840 Dr. Mark L. Hornick5 These first three are unique to each thread – each call to doGet or doPost gets a separate copy Can’t write to constants, and reading is thread-safe These are objects are shared among threads, so reading or writing simultaneously on separate threads is dangerous – and will result in errors

6 Are any of the following good approaches to avoid threading problems? 1. Synchronize a Servlet’s entire service methods Let only a single thread at a time execute doGet(), doPost(), etc 2. Synchronize a block of code within a method Let only a single thread at a time execute critical sections. SE-2840 Dr. Mark L. Hornick6 What are the tradeoffs of each approach?

7 A related problem: If we use a Servlet’s attributes to store data, only that Servlet can access the data SE-2840 Dr. Mark L. Hornick7 What if we wanted a different Servlet to generate the response, in order to separate class responsibilities and improve cohesion? And what happens if our Servlet is used in another web app on the same server??? Thread 19 Thread 20 Data store doPost(request, response) Composition (“has a”)

8 The ServletContext is a Data Store that is accessible to all Servlets in the web app. SE-2840 Dr. Mark L. Hornick8 Thread 19 Thread 20 Data store doGet(request, response) doPost(request, response) Composition (“has a”) Servlet 2 The ServletContext is initialized by Tomcat before any Servlet is initialized. doPost(request, response) Thread 22

9 How Tomcat manages Servlets CS-4220 Dr. Mark L. Hornick9 Called only ONCE in the servlet’s life (and must complete before Container calls service() and AFTER the ServletContext has been created Container calls destroy() to give the servlet a chance to clean up; like init(), destroy() is only called ONCE The methods doGet() or doPost() are executed to process requests This is where the servlet spends most of its life Web Container (Tomcat) Your servlet class no-arg ctor runs (you should NOT write a ctor; just use the compiler- supplied default.

10 ServletContext Attributes The ServletContext object is basically a hashmap: Attributes can be created/modified by code Attributes are name/value pairs, where the name is a String and value is an Object Attributes are read/write CS-4220 Dr. Mark L. Hornick10

11 On last thing: We can also initialize a complex ServletContext attribute before any Servlets are initialized Solution: Use a class that implements the ServletContextListener interface SE-2840 Dr. Mark L. Hornick11 This is one of 8 different Listeners The event class

12 The contextInitialized() event handler is called by Tomcat at startup In the contextInitialized() method, we can create a ServletContext attribute that is a complex datatype: public void contextInitialized(ServletContextEvent e) { ServletContext context = e.getServletContext(); context.setAttribute(“foo”, new MyComplexType() ); } // later, any Servlet will be able to access MyComplexType via a call to getServletContext().getAttribute(“foo”); SE-2840 Dr. Mark L. Hornick12

13 We need to register ServletContextListeners In Tomcat 7, just use the @Weblistener annotation! In Tomcat 6, you must edit the DD (web.xml):... MyServlet test.HelloWorldServlet...... Some other servlet’s defn goes here... myPackage.MyContextListener </listener... SE-2840 Dr. Mark L. Hornick13

14 Finally…thread-safe data accessed as a ServletContext attribute All users sharing the same object maintained by the ServletContext… But is this really what we want? Next step: Sessions SE-2840 Dr. Mark L. Hornick14


Download ppt "CS-4220 Dr. Mark L. Hornick1 Single class doing too many things =Bad Code Smell."

Similar presentations


Ads by Google