Escolar Documentos
Profissional Documentos
Cultura Documentos
Web Applications
The Web Container Model
Review
How to build the simple web site using html and servlet?
Break down structure component in building web application
Some concepts
2. Call
3. Query
DAO
5. Response
4. Render/Send
6. Display
DB
Objectives
How to deploy the Web Application to Web
Server?
Web applications Structure
Request Parameters vs. Context
Config/Servlet Parameters
Application Segments vs. Scope
Parameters
vs.
Objectives
Topic 1 Login
Servlet
Topic 2 Search
Break Down
Topic 3 CUD, Shopping Cart
Session
Web
Advanced
Java - PBL
Enterprise
Topic 4 Login
Security
Topic 5 CRUD
MVC 2 - Scripting
Topic 6 Login
MVC 1 - Action
Topic 7 CRUD
MVC 2 Complete - JSTL - Taglib
Deploy Application
Expectation
Typed and Press Enter
Web Applications
File and Directory Structure
*.war
Web Applications
File and Directory Structure
/WEB-INF/classes for classes that exist as separate Java
classes (not packaged within JAR files). These might be
servlets or other support classes.
/WEB-INF/lib for JAR file. These can contain anything at
all the main servlets for your application, supporting
classes that connect to databases whatever.
/WEB-INF itself is the home for an absolutely crucial file
called web.xml, the web deployment descriptor file.
2 special rules apply to files within the /WEB-INF
directory
Direct client access should be disallowed with an HTTP 404 code
The order of class loading the java classes in the /WEBINF/classes directory should be loaded before classes resident in
jar files in the /WEB-INF/lib directory
Web Applications
File and Directory Structure
Static content,
JSP pages,
Servlet classes,
The deployment descriptor,
Tag libraries,
JAR files and Java class files;
and describe how to protect resource file from HTTP access.
Web Applications
Deploy Mechanism
Context Path
2. Create
Directory
7. Load lib/jar
(if any)
*.war
3. Extract to
1. Unzip/
Un-jar
4. Locate
DD
5. Parse &
Process to
look up
servlet
declaration
Web Applications
Manual Deploying
Go to the Installed_Tomcat\bin
directory, click startup.bat or
tomcat7w.exe
Web Applications
Manual Deploying
Collects all methods that are used for particular Web application in server
side
Support to interact with Servlet container
Stores some object in server side that all webs component can access
Exists from the application has been deployed to undeployed (or server is
crashed)
\day2
intro.html
\WEB-INF
web.xml
</web-app>
Descriptions
getServletName
getInitParameter
<servlet>
<servlet-name>servletName</servlet-name>
<servlet-class>servletClass</servlet-class>
<init-param>
<param-name>parName</param-name>
<param-value>parValue</param-value>
</init-param>
</servlet>
Writing Code to Retrieve ServletConfig Initialization Parameters
ServletConfig sc = getServletConfig();
String name = sc.getInitParameter(parName");
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
How To Transfer
Expectation
How To Transfer
Expectation
How To Transfer
Interactive Server Model
1. Send request
Servlet
Web/App
Server
1. Click Search
5. Response the
result page
4. Transfer &
Traverse to
display
List
DTO
2. Call Search
DAO
3. Query DB
DB
Client
Server
How To Transfer
Abstraction
Web Server
Container
1. Send request
2. Call
3. Query
5. Render/Send
7. Display
DB
DAO
6. Response
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.
Session Scope
A browser window establishes up to the point where that browser window
is closed
Open session up to the point where that session is closed, session is time
out, server is crashed.
javax.servlet.http.HttpSession
Descriptions
getAttribute
setAttribute
removeAttribute
MiddleServlet
Redirect Mechanism
3. Send request
2. Generate the
Request msg
8. Response
Resource2
7. Execute
Client
Server
A RequestDispacher object
Is created by the servlet container
Redirect the client request to a particular Web page
Descriptions
forward
include
- Include the content of another servlet into the current output stream
- Include the output of another Servlet to process the client request
- Ex
RequestDispatcher rd = request.getRequestDipatcher(home.jsp);
rd.include (request, response);
Forward Mechanism
3. Send request
9. Send response
6. Forward & execute
remained r1s code & no
response
2. Generate the
Request msg
8. Response
Resource2
7. Execute
Client
Server
Include Mechanism
3. Send request
8. Response
Client
Server
How To Transfer
Interactive Server Model Implementation
1. Send request
Servlet
Web/App
Server
1. Click Search
5. Response the
result page
4. Transfer &
Traverse to
display
List
DTO
2. Call Search
DAO
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
Q&A
Summary
Web Server
Container
1. Send request
2. Call
3. Query
5. Render/Send
7. Display
DB
DAO
6. Response
Next Lecture
How to write CUD Web Application
Session Tracking Techniques
Manipulate DB Techniques in Web Application
Break down structure component in building web
application
Next Lecture
Topic 1 Login
Servlet
Topic 2 Search
Break Down
Topic 3 CUD, Shopping Cart
Session
Web
Advanced
Java - PBL
Enterprise
Topic 4 Login
Security
Topic 5 CRUD
MVC 2 - Scripting
Topic 6 Login
MVC 1 - Action
Topic 7 CRUD
MVC 2 Complete - JSTL - Taglib
How to Transfer
DAO
How to Transfer
DAO
How to Transfer
Process Servlet
How to Transfer
Search Servlet
How to Transfer
Search Result Servlet
How to Transfer
Search Result Servlet
How to Transfer
Search Result Servlet
Appendix
Filter
1. Input and
click button/link
3. Send request
2. Generate the
Request msg
Resource
Client
Server
8. Execute &
Response
Appendix
Filter
Appendix
Usage
Filter
Authorize request
Altering request headers and modify data
Modify response headers and data
Authenticating the user
Comprising files
Encrypting data
Converting images
Logging and auditing filters
Filters that trigger resource access events
Appendix
Benefits Advantages
Filter
Appendix
Filter Life Cycle
Working of Filter
The filter intercepts the request
from a user to the servlet
The filter then provides
customized services
The filter sends the serviced
response or request to the
appropriate destination
Instantiation
and Loading
Initialization
init()
doFilter()
destroy()
Unavailable
Appendix
Filter API
Filter Interface
Must be implemented to create a filter class extends javax.servlet.Filter
An object performs filtering tasks on the request and the response
Methods
Descriptions
init
doFilter
destroy
Appendix
In Web Deployment Descriptor
Filter
<web-app>
.
<filter>
<filter-name>Name of Filters</filter-name>
<filter-class>implemented Filter Class</filter-class>
[<init-param>
<param-name>parameter name</param-name>
<param-value>value </param-value>
</init-param>]
</filter>
<filter-mapping>
<filter-name>FilterName</filter-name>
<url-pattern>/context</url-pattern>
</filter-mapping>
.
</web-app>
Appendix
Filter Example
Building the web application shows as the following GUI in sequence
Appendix
Filter Example
Appendix
Filter Example
Appendix
Filter Example
Appendix
Filter Example
Fill your filter name
Fill/choose package
name
Appendix
Filter Example
Apply filter
Edit the Apply filter
Appendix
Filter Example
Appendix
Filter Example
Appendix
Filter Example
Appendix
Filter Example
Appendix
Filter Chain
There can be more than one filter between the user and the
endpoint Invoke a series of filters
A request or a response is passed through one filter to the next in
the filter chain. So each request and response has to be serviced by
each filter forming a filter chain
If the Calling filter is last filter, will invoke web resource
FilterChain Interface
Provides an object through the web container
The object invokes the next filter in a filter chain starting from the first filter from a
particular end. If the calling filter is the last filter in the chain, it will invoke the web
resource, such as JSP and servlet.
Only implement doFilter() method.
Forces the next filter in the chain to be invoked
Appendix
Filter Chain Example
Appendix
Filter Chain Example
Appendix
Filter Chain Example
Appendix
Filter Chain Example
Appendix
Filter Chain Example
Appendix
Filter Chain Example
Appendix
Filter Chain Example Change pos
Appendix
Filter Chain Example
Appendix
Why need a Wrapper Class
Appendix
Why need a Wrapper Class
Appendix
Why need a Wrapper Class
Appendix
Wrapper Class
To modify or intercept the request or response before they can reach their logical
destination, the required object can dynamically capture the request or response
Wrapper class
Creates the object to capture the request and response before they reach server and client
respectively
The wrapper object generated by the filter implements the getWriter() and getOutputStream(),
which returns a stand-in-stream. The stand-in-stream is passed to the servlet through the wrapper
object
The wrapper object captures the response through the stand-in-stream and sends it back to the
filter
Classes
ServletRequestWrapper
Descriptions
Appendix
Wrapper Class Altering Request
Create filter class extends to
the ServletRequestWrapper
or
HttpServletRequestWrapper
class.
The object captures the
HttpRequest object from the
client and sends it to the filers
Through the objects filter
extends some services to the
request.
Appendix
Wrapper Class Altering Response
Create filter class extends to the
ServletResponseWrapper
or
HttpServletResponseWrapper
class.
The
object
captures
the
httpRequest object from the
client and sends it to the filers
Through the objects filter extends
some services to the request.
Appendix
Wrapper Class Example
Appendix
Wrapper Class Example
Appendix
Wrapper Class Example
Adding the MyPrinter class extends PrintWriter in FilterWrapper class
Appendix
Wrapper Class Example
Modifying the ResponseWrapper class uses MyPrinter to output stream
Appendix
Wrapper Class Example