Web Applications & Web Containers
Web Applications & Web Containers
Web Applications & Web Containers
Web Applications
The Web Container Model
DB
Client Server
Review
Web Server
Container
1. Send request C 2. Call
3. Query
5. Response DAO DB
4. Render/Send
6. Display V
Objectives
• How to deploy the Web Application to Web
Server without using Netbeans/ Eclipse tools?
– Web applications Structure
– Request Parameters vs. Context Parameters vs.
Config/Servlet Parameters
– Application Segments vs. Scope
• How to transfer from resources to others
with/without data/objects?
– Attributes vs. Parameters vs. Variables
– Redirect vs. RequestDispatcher
Objectives
Day 1, 2, 3, 4, 5, 6 – Login
Servlet, JDBC
Day 7, 8, 9 – Search
Break Down
*.war
*.war
2. Create
Directory
7. Load lib/jar
(if any)
*.war
*.war
3. Extract to
1. Unzip/
Un-jar
ServletContext sc = getServletContext();
String var = sc.getInitParameter(“parName");
The Web Container Model
The ServletContext – Initialization Parameters
The Web Container Model
The ServletContext – Initialization Parameters
The Web Container Model
The ServletContext – Initialization Parameters
The Web Container Model
The ServletContext – Initialization Parameters
The Web Container Model
The ServletConfig interface
• To pass as an argument during initialization, the servlet container uses an object of
ServletConfig interface
• Configuring a servlet before processing requested data
• Retrieve servlet initialization parameters
Methods Descriptions
ServletConfig sc = getServletConfig();
String name = sc.getInitParameter(“parName");
The Web Container Model
The ServletConfig interface – Example
The Web Container Model
The ServletConfig interface – Example
The Web Container Model
The ServletConfig interface – Example
The Web Container Model
The ServletConfig interface – Example
The Web Container Model
The ServletConfig interface – Example
The Web Container Model
The ServletConfig interface – Example
How To Transfer
Requirements
• After built the web application in the first topic
– The search page allows user search appropriate the last name
of users
– The result of searching is shown in the data grid. In each row,
the information about ordinary number, username,
password, last name and roles is shown
• The GUI of web application is present as following
How To Transfer
Expectation
How To Transfer
Expectation
How To Transfer
Interactive Server Model
1. Send request
Web/App Servlet
Server
1. Click Search
5. Response the 2. Call Search
result page 4. Transfer &
Traverse to DAO
display
List
DTO 3. Query DB
DB
Client Server
How To Transfer
Abstraction
Web Server
Container
1. Send request C 2. Call
3. Query
6. Response DAO DB
5. Render/Send
4. Store data into
7. Display V
DTO
The Web Container Model
Need for using attributes
• Problems:
– How to remember an user that has already logged into the
particular website?
– How to store a collection of selected products online when the
user has already chosen while the HTTP is a stateless protocol?
Besides, they can search and choose other products
• Solutions:
– Store data or object as long as user still browses the web site
– Attributes is a qualified candidate: Attributes are a collection of
<attribute-name, value> pairs that is stored in a scope
(segment) in server
– Life cycle of them is long as its defined scope.
The Web Container Model
Attributes, Scope, and Multithreading
• Defines how long a reserved memory segment is available
in the context on the server.
• There are 3 scopes
– Request Scope
• Lasts from HTTP request hits a web container to the servlet deliveres the
HTTP response.
• javax.servlet.ServletRequest
– Session Scope
• A browser window establishes up to the point where that browser window
is opened
• Open session up to the point where that session is closed, session is time
out, server is crashed.
• javax.servlet.http.HttpSession
– Context (Application) Scope
• Is the longest-lived of the three scopes available to you.
• Exists until the web container is stopped.
• javax.servlet.ServletContext
The Web Container Model
Attributes, Scope, and Multithreading
• Choosing Scopes
– Request Scope: attributes are required for a one-off
web page and aren’t part of a longer transaction
– Session Scope: attributes are part of a longer
transaction, or are spanned several request but they are
information unique to particular client
• Ex: username or account
– Context Scope: attributes can allow any web resource
to access (e.g. public variables in application)
The Web Container Model
Attributes, Scope, and Multithreading
• Parameters vs. Attributes
– Parameters allow information to flow into a web application
(passed to web application via form or query string). They exist
in request scope
– Attributes are more of a means of handling information within the
web application. They can be shared or accessed within their
defined scope
– Data types of Parameter is String but the Attribute is Object
• The web container uses attributes as a place to
– Provide information to interested code: the way supplement the
standard APIs that yield information about the web container
– Hang on to information that your application, session, or even
request requires later.
• The developer can access the attribute value with
attribute’s name
The Web Container Model
Attributes, Scope, and Multithreading
Methods Descriptions
MiddleServlet
The Web Container Model
Need for using RequestDispatcher – Redirect
The Web Container Model
Need for using RequestDispatcher – Redirect
The Web Container Model
Need for using RequestDispatcher
1. Input and
Redirect Mechanism
click button/link
3. Send request 4. Dispatch to Servlet/Web Container
Containter
2. Generate the Web/App Resource1 5. Select and
Request msg Server execute
6’’/9. Send response 6. Send
6’. Response
Redirect
8. Response Resource2
7. Execute
Client Server
The Web Container Model
Request Dispatching
• Is a mechanism for controlling the flow of control
within the web resources in the web application
• The ServletRequest and ServletContext support the
getRequestDispacher(String path) method
– Returns RequestDispacher instance
– The path parameter can be a full path beginning at the context
root (“/”) – requirement with ServletContext
– The ServletContext offers the getNameDispatcher(String
name) method that requires providing the resource’s name to
want to execute (e.g. the name must match one of the <servlet-
name>)
• A RequestDispacher object
– Is created by the servlet container
– Redirect the client request to a particular Web page
The Web Container Model
Using RequestDispatcher
Methods Descriptions
7. Execute
Client Server
The Web Container Model
Using RequestDispatcher – Example
Change the RequestDispatch – forward method to include method
The Web Container Model
Need for using RequestDispatcher
1. Input and
Include Mechanism
click button/link
3. Send request 4. Dispatch to Servlet/Web Container
Containter
2. Generate the Web/App Resource1 5. Select and
Request msg Server 7. Compile & execute
9. Send response execute
6. Include: get
r2 and add to
r1
Resource2
8. Response
Client Server
How To Transfer
Interactive Server Model – Implementation
1. Send request
Web/App Servlet
Server
1. Click Search
5. Response the 2. Call Search
result page 4. Transfer &
Traverse to DAO
display
List
DTO 3. Query DB
DB
Client Server
Summary
• How to deploy the Web Application to Web
Server?
– Web applications Structure
– Request Parameters vs. Context Parameters vs.
Config/Servlet Parameters
– Application Segments vs. Scope
• How to transfer from resources to others
with/without data/objects?
– Attributes vs. Parameters vs. Variables
– Redirect vs. RequestDispatcher
Q&A
Summary
Web Server
Container
1. Send request C 2. Call
3. Query
6. Response DAO DB
5. Render/Send
4. Store data into
7. Display V get (if any)
DTO
Next Lecture
• How to upgrade Application in previous
topics approach MVC Model
– Using JSP to View
– MVC Pattern Design
Next Lecture
Day 1, 2, 3, 4, 5, 6 – Login
Servlet, JDBC
Day 7, 8, 9 – Search
Break Down