Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 2 Web app architecture. High-level web app architecture  When a client request coming in and needs servlet to serve dynamic web content, what.

Similar presentations


Presentation on theme: "Chapter 2 Web app architecture. High-level web app architecture  When a client request coming in and needs servlet to serve dynamic web content, what."— Presentation transcript:

1 Chapter 2 Web app architecture

2 High-level web app architecture  When a client request coming in and needs servlet to serve dynamic web content, what has to be done? Somebody has to instantiate the servlet Somebody has to instantiate the servlet Somebody has to make a new thread to handle the request Somebody has to make a new thread to handle the request Somebody has to call the servlet’s doPost() or doGet() method Somebody has to call the servlet’s doPost() or doGet() method Somebody has to get the request and the response to the servlet Somebody has to get the request and the response to the servlet Somebody has to manage the life, death, and resources of the servlet Somebody has to manage the life, death, and resources of the servlet  That somebody is the web container

3 What is a Container  Servlets don’t have a main() method  They are under control of another Java application called a Container Tomcat is an example of a Container Tomcat is an example of a Container Example: import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class Ch1Servlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { PrintWriter out = response.getWriter(); PrintWriter out = response.getWriter(); java.util.Date today = new java.util.Date(); java.util.Date today = new java.util.Date(); out.println(" " + out.println(" " + " "+ " "+ " HF\'s Chapter1 Servlet " " HF\'s Chapter1 Servlet " + " "+today+" "+" "); + " "+today+" "+" "); }}

4 What is a container  When your web server (like Apache) gets a request for a servlet, the server hands the request not to the servlet itself, but to the container in which the servlet is deployed  Container gives the servlet the request and gets the response from the servlet

5 What does the container give you?  Communication support The container provides an easy way for your servlets to talk to your web server The container provides an easy way for your servlets to talk to your web server Otherwise, the servlets may need to create sockets in order to communicate with the web serverOtherwise, the servlets may need to create sockets in order to communicate with the web server  Lifecycle Management The container controls the life and death of your servlets The container controls the life and death of your servlets The container takes care of The container takes care of Loading the classesLoading the classes Instantiating and initializing the servletsInstantiating and initializing the servlets Invoking the servlet methodsInvoking the servlet methods Making servlet instances eligible for garbage collectionMaking servlet instances eligible for garbage collection

6 What does the container give you?  Multithreading support The container automatically creates a new Java thread for every servlet request it receives The container automatically creates a new Java thread for every servlet request it receives When the servlet’s done running the HTTP service method for one client’s request, the thread completes (dies) When the servlet’s done running the HTTP service method for one client’s request, the thread completes (dies) But the servlet does not die, it may have several other running threads serving other client’s requests to the same servletBut the servlet does not die, it may have several other running threads serving other client’s requests to the same servlet

7 What does the container give you?  Declarative Security With a container, you can use an XML deployment descriptor to configure (and modify) security without having to hard-code it With a container, you can use an XML deployment descriptor to configure (and modify) security without having to hard-code it You do not have to hard-code it into your servlet class code You do not have to hard-code it into your servlet class code  JSP support Container takes care of translating JSP code into Java code Container takes care of translating JSP code into Java code

8 How the container handles a request

9 How the container handles the request

10

11 How the container finds the servlet  The URL that comes in as part of the request from the client is mapped to a specific servlet on the server  It is up to the developer/deployer to configure the mapping

12 A servlet can have THREE names  Client-known URL name Or sometimes called pubic URL name Or sometimes called pubic URL name  Deployer-known secret internal name Or sometimes called deployment name Or sometimes called deployment name  Actual file name The developer gives a name to the servlet class The developer gives a name to the servlet class

13

14

15 A servlet can have THREE names  Mapping servlet names improves your app’s flexibility and security By mapping the name instead of coding in the real file and path name, you have the flexibility to move things around By mapping the name instead of coding in the real file and path name, you have the flexibility to move things around You do not want the client to know exact how things are structured on your server You do not want the client to know exact how things are structured on your server If the end-user can see the real path, she can type it into her browser and try to access it directlyIf the end-user can see the real path, she can type it into her browser and try to access it directly

16 Deployment Descriptor can map URLs to servlets  The two Deployment Descriptor (DD) elements for URL mapping: Maps internal name to fully-qualified class nameMaps internal name to fully-qualified class name Maps internal name to public URL nameMaps internal name to public URL name

17


Download ppt "Chapter 2 Web app architecture. High-level web app architecture  When a client request coming in and needs servlet to serve dynamic web content, what."

Similar presentations


Ads by Google