chapter 2 web app architecture. high-level web app architecture when a client request coming in and...

17
Chapter 2 Web app Chapter 2 Web app architecture architecture

Upload: madison-beasley

Post on 04-Jan-2016

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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

Chapter 2 Web app architectureChapter 2 Web app architecture

Page 2: 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

High-level web app architectureHigh-level web app architecture When a client request coming in and needs servlet When a client request coming in and needs servlet

to serve dynamic web content, what has to be to serve dynamic web content, what has to be done?done? Somebody has to instantiate the servletSomebody has to instantiate the servlet Somebody has to make a new thread to handle the Somebody has to make a new thread to handle the

requestrequest Somebody has to call the servlet’s doPost() or Somebody has to call the servlet’s doPost() or

doGet() methoddoGet() method Somebody has to get the request and the response to Somebody has to get the request and the response to

the servletthe servlet Somebody has to manage the life, death, and Somebody has to manage the life, death, and

resources of the servletresources of the servlet That somebody is the web containerThat somebody is the web container

Page 3: 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

What is a ContainerWhat is a Container

Servlets don’t have a Servlets don’t have a main() methodmain() method

They are under They are under control of another control of another Java application Java application called a called a ContainerContainer Tomcat is an example Tomcat is an example

of a Containerof a Container

Example:Example:

import javax.servlet.*;import javax.servlet.*;

import javax.servlet.http.*;import javax.servlet.http.*;

import java.io.*;import java.io.*;

public class Ch1Servlet extends HttpServlet {public class Ch1Servlet extends HttpServlet {

public void doGet(HttpServletRequest request,public void doGet(HttpServletRequest request,

HttpServletResponse response)HttpServletResponse response)

throws IOException {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("<html> " +out.println("<html> " +

"<body>"+"<body>"+

"<h1 align=center>HF\'s Chapter1 "<h1 align=center>HF\'s Chapter1 Servlet</h1>"Servlet</h1>"

+ "<br>"+today+"</body>"+"</html>");+ "<br>"+today+"</body>"+"</html>");

}}

}}

Page 4: 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

What is a containerWhat is a container When your web server (like When your web server (like

Apache) gets a request for a Apache) gets a request for a servlet, the server hands the servlet, the server hands the request not to the servlet itself, request not to the servlet itself, but to the container in which but to the container in which the servlet is deployedthe servlet is deployed

Container gives the servlet the Container gives the servlet the request and gets the response request and gets the response from the servlet from the servlet

Page 5: 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

What does the container give What does the container give you?you?

Communication supportCommunication support The container provides an easy way for your servlets The container provides an easy way for your servlets

to talk to your web serverto talk to your web server• Otherwise, the servlets may need to create sockets in order Otherwise, the servlets may need to create sockets in order

to communicate with the web serverto communicate with the web server Lifecycle ManagementLifecycle Management

The container controls the life and death of your The container controls the life and death of your servletsservlets

The container takes care ofThe 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

Page 6: 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

What does the container give What does the container give you?you?

Multithreading supportMultithreading support The container automatically creates a new The container automatically creates a new

Java thread for every servlet request it Java thread for every servlet request it receivesreceives

When the servlet’s done running the HTTP When the servlet’s done running the HTTP service method for one client’s request, the service method for one client’s request, the thread completes (dies)thread completes (dies)• But the servlet does not die, it may have several But the servlet does not die, it may have several

other running threads serving other client’s other running threads serving other client’s requests to the same servletrequests to the same servlet

Page 7: 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

What does the container give What does the container give you?you?

Declarative SecurityDeclarative Security With a container, you can use an XML With a container, you can use an XML

deployment descriptor to configure (and deployment descriptor to configure (and modify) security without having to hard-code itmodify) security without having to hard-code it

You do not have to hard-code it into your You do not have to hard-code it into your servlet class codeservlet class code

JSP supportJSP support Container takes care of translating JSP code Container takes care of translating JSP code

into Java codeinto Java code

Page 8: 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

How the container handles a requestHow the container handles a request

Page 9: 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

How the container handles the requestHow the container handles the request

Page 10: 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
Page 11: 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

How the container finds the How the container finds the servletservlet

The URL that comes in as part of the The URL that comes in as part of the request from the client is mapped to a request from the client is mapped to a specific servlet on the serverspecific servlet on the server

It is up to the developer/deployer to It is up to the developer/deployer to configure the mappingconfigure the mapping

Page 12: 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

A servlet can have THREE A servlet can have THREE namesnames

Client-known URL nameClient-known URL name Or sometimes called Or sometimes called pubic URL namepubic URL name

Deployer-known secret internal nameDeployer-known secret internal name Or sometimes called Or sometimes called deployment namedeployment name

Actual file nameActual file name The developer gives a name to the servlet The developer gives a name to the servlet

classclass

Page 13: 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
Page 14: 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
Page 15: 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

A servlet can have THREE A servlet can have THREE namesnames

Mapping servlet names improves your Mapping servlet names improves your app’s flexibility and securityapp’s flexibility and security By mapping the name instead of coding in the By mapping the name instead of coding in the

real file and path name, you have the real file and path name, you have the flexibility to move things aroundflexibility to move things around

You do not want the client to know exact how You do not want the client to know exact how things are structured on your serverthings are structured on your server• If the end-user can see the real path, she can type If the end-user can see the real path, she can type

it into her browser and try to access it directlyit into her browser and try to access it directly

Page 16: 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

Deployment Descriptor can map URLs Deployment Descriptor can map URLs to servletsto servlets

The two Deployment Descriptor (DD) The two Deployment Descriptor (DD) elements for URL mapping:elements for URL mapping: <servlet><servlet>

• Maps internal name to fully-qualified class nameMaps internal name to fully-qualified class name <servlet-mapping><servlet-mapping>

• Maps internal name to public URL nameMaps internal name to public URL name

Page 17: 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