Java Servlet Architecture in Web Applications

SE-2840 Dr. Mark L. Hornick
1
J
a
v
a
 
S
e
r
v
l
e
t
-
b
a
s
e
d
 
w
e
b
 
a
p
p
s
Servlet Architecture
R
e
c
a
l
l
:
 
T
h
e
 
i
n
t
e
r
a
c
t
i
o
n
 
b
e
t
w
e
e
n
w
e
b
 
c
l
i
e
n
t
s
 
a
n
d
 
s
e
r
v
e
r
s
 
i
s
s
t
r
u
c
t
u
r
e
d
 
a
r
o
u
n
d
 
H
T
T
P
 
R
e
q
u
e
s
t
a
n
d
 
R
e
s
p
o
n
s
e
 
m
e
s
s
a
g
e
s
SE-2840 Dr. Mark L. Hornick
2
Server is running a 
web server app
,
like Apache or Microsoft IIS.
I
n
 
t
h
e
 
s
i
m
p
l
e
s
t
 
s
c
e
n
a
r
i
o
,
 
t
h
e
 
S
e
r
v
e
r
r
e
s
p
o
n
d
s
 
t
o
 
a
 
b
r
o
w
s
e
r
 
G
E
T
 
r
e
q
u
e
s
t
b
y
 
r
e
t
u
r
n
i
n
g
 
a
 
p
r
e
-
w
r
i
t
t
e
n
,
 
s
t
a
t
i
c
H
T
M
L
 
f
i
l
e
SE-2840 Dr. Mark L. Hornick
3
Note: This diagram can
be found in your textbook
HTML file maintained on
Server, returned to the
Browser as the HTTP
response “payload”
HTTP GET
request
E
c
l
i
p
s
e
 
J
2
E
E
 
d
e
m
o
 
CS-4220 Dr. Mark L. Hornick
4
A
 
w
e
b
 
s
e
r
v
e
r
 
c
a
n
 
e
m
p
l
o
y
 
a
 
H
e
l
p
e
r
A
p
p
 
w
h
e
n
 
i
t
 
n
e
e
d
s
 
t
o
 
g
o
 
b
e
y
o
n
d
s
e
r
v
i
n
g
 
s
t
a
t
i
c
 
w
e
b
 
p
a
g
e
s
SE-2840 Dr. Mark L. Hornick
5
CGI
Helper
 app
H
o
w
 
i
t
 
w
o
r
k
s
 
i
n
 
g
e
n
e
r
a
l
SE-2840 Dr. Mark L. Hornick
6
Note: This diagram can
be found in your textbook
User enters a URL (or
clicks a link) to a CGI
program rather than a
static page
Web server “sees” that the
request is for a helper
program, so the server runs
the helper, sending along any
parameters
 sent from the
Client.
The helper app constructs the brand
new (dynamic) page and sends the
HTML back to the server.
H
o
w
 
i
t
 
w
o
r
k
s
 
f
o
r
 
J
a
v
a
 
S
e
r
v
l
e
t
s
SE-2840 Dr. Mark L. Hornick
7
Note: This diagram can
be found in your textbook
Web server app is
commonly 
Apache
Web container app is
Tomcat
Servlets are run by
Tomcat
W
h
a
t
 
d
o
e
s
 
a
 
C
o
n
t
a
i
n
e
r
 
l
i
k
e
T
o
m
c
a
t
 
d
o
?
Communication
Creates server-side sockets
Listens for client connections
Determines client HTTP request type and “decodes” HTTP headers
Servlet Lifecycle management
Figures out which Servlet should be used to process a specific request
Handles Servlet class loading
Handles Servlet instantiation/construction
Handles Servlet initialization
Servlet execution support
Launches/manages threads that service each incoming request
H
a
n
d
l
e
s
 
S
e
r
v
l
e
t
 
s
e
r
v
i
c
e
(
)
 
m
e
t
h
o
d
 
 
(
d
o
G
e
t
 
a
n
d
 
d
o
P
o
s
t
)
 
i
n
v
o
c
a
t
i
o
n
Creates and passes Request and Response objects to the Servlet
Supports Security
Supports JSP
SE-2840 Dr. Mark L. Hornick
8
H
o
w
 
T
o
m
c
a
t
 
m
a
n
a
g
e
s
 
S
e
r
v
l
e
t
s
CS-4220 Dr. Mark L. Hornick
9
Loading can be done upon
Tomcat startup, or
deferred until later
T
o
m
c
a
t
 
i
n
v
o
k
e
s
 
a
 
S
e
r
v
l
e
t
s
 
s
e
r
v
i
c
e
(
)
 
m
e
t
h
o
d
,
b
u
t
 
y
o
u
r
 
H
T
T
P
S
e
r
v
l
e
t
-
d
e
r
i
v
e
d
 
c
l
a
s
s
 
s
h
o
u
l
d
o
n
l
y
 
o
v
e
r
r
i
d
e
 
d
o
G
e
t
(
)
 
o
r
 
d
o
P
o
s
t
(
)
SE-2840 Dr. Mark L. Hornick
10
The 
service
() method is given an
implementation in the HTTPServlet
base class, where the 
doGet
() and
doPost
() methods are called.
You must override these methods in
your HttpServlet-derived class
A
 
S
e
r
v
l
e
t
 
i
s
 
j
u
s
t
 
a
 
J
a
v
a
 
c
l
a
s
s
 
t
h
a
t
 
i
m
p
l
e
m
e
n
t
s
 
s
o
m
e
 
s
p
e
c
i
f
i
c
i
n
t
e
r
f
a
c
e
s
 
(
d
e
f
i
n
e
d
 
b
y
 
t
h
e
 
J
a
v
a
 
S
e
r
v
l
e
t
 
S
p
e
c
i
f
i
c
a
t
i
o
n
s
)
 
t
h
a
t
 
a
r
e
 
u
s
e
d
b
y
 
t
h
e
 
C
o
n
t
a
i
n
e
r
SE-2840 Dr. Mark L. Hornick
11
All Servlets must
implement these 5
methods
Abstract class.
Implements most of
the basic servlet
methods
Implements the service() method
and calls doGet(), doPost() etc as
appropriate
N
O
T
E
The Java classes pertaining to Servlets are 
not
part of the standard SE
They are part of the 
Java EE 
specification
Implementation of the 1.x SE is provided in the
1.x JDK/JRE System Library
This is the library you are probably most familiar with
r
t
.
j
a
r
 
i
s
 
t
h
e
 
m
a
i
n
 
j
a
r
f
i
l
e
 
i
n
 
t
h
i
s
 
l
i
b
r
a
r
y
Container vendors supply the implementation of
the classes that are part of the Servlet
specification
Tomcat comes with its own Servlet libraries
s
e
r
v
l
e
t
-
a
p
i
.
j
a
r
 
i
m
p
l
e
m
e
n
t
s
 
t
h
e
 
S
e
r
v
l
e
t
-
r
e
l
a
t
e
d
 
c
l
a
s
s
e
s
SE-2840 Dr. Mark L. Hornick
12
SE-2840 Dr. Mark L. Hornick
13
P
a
r
a
m
e
t
e
r
s
:
 
H
T
M
L
 
<
f
o
r
m
>
 
t
a
g
 
e
l
e
m
e
n
t
<form action
="http://
<url>
" 
method=“post"
>
 <!-- form elements go here -->
</form>
 
The opening <form>
tag – all form
elements go
between the opening
and closing tag.
 
The required action attribute
specifies the url of where to send
the form’s data.
 
…and the name of
the Web Resource
that will process the
form data 
if it is
submitted
 
The 
method
 attribute
specifies which HTTP
message will be used to
send the data in the form
to the server – 
default is
“get”
Note: See the examples
on the course website
G
E
T
 
v
s
.
 
P
O
S
T
 
s
c
e
n
a
r
i
o
s
SE-2840 Dr. Mark L. Hornick
14
Note: This diagram can
be found in your textbook
SE-2840 
Dr. Mark L. Hornick
15
g
e
t
 
s
p
e
c
i
f
i
e
s
 
t
h
a
t
 
a
 
H
T
T
P
 
G
E
T
 
m
e
s
s
a
g
e
s
h
o
u
l
d
 
b
e
 
u
s
e
d
,
 
w
h
i
c
h
 
a
p
p
e
n
d
s
 
t
h
e
 
f
o
r
m
d
a
t
a
 
t
o
 
t
h
e
 
e
n
d
 
o
f
 
t
h
e
 
u
r
l
http://<domain>/<resource>?firstname=Arnold&last
name=Ziffel
 
g
e
t
 
r
e
q
u
e
s
t
s
 
h
a
v
e
 
a
 
l
i
m
i
t
o
f
 
2
5
6
 
c
h
a
r
a
c
t
e
r
s
T
h
e
 
d
a
t
a
 
i
s
 
p
l
a
i
n
l
y
 
v
i
s
i
b
l
e
i
n
 
t
h
e
 
u
r
l
 
(
i
n
s
e
c
u
r
e
!
)
You can bookmark a page
that is the result of
submitting a form
Use GET 
only
 to submit
small amounts of
insensitive data which the
server app will 
NOT
 use to
modify its internal state
 
SE-2840 
Dr. Mark L. Hornick
16
p
o
s
t
 
s
p
e
c
i
f
i
e
s
 
t
h
a
t
 
a
 
H
T
T
P
 
P
O
S
T
 
m
e
s
s
a
g
e
s
h
o
u
l
d
 
b
e
 
u
s
e
d
,
 
w
h
i
c
h
 
a
p
p
e
n
d
s
 
t
h
e
 
f
o
r
m
d
a
t
a
 
t
o
 
t
h
e
 
e
n
d
 
o
f
 
t
h
e
 
H
T
T
P
 
P
O
S
T
 
h
e
a
d
e
r
 
There is no limit on the size of the data packet that can
be sent to the server
You cannot bookmark a url that was generated as a
POST message, since the form data is not in the url
A post request can be encrypted  (using HTTPS) in
order to protect sensitive data, such as a credit card
numbers or passwords
Use POST to send form data that
Is sensitive (use encryption in that case)
If the data is large (>256 bytes)
Will change the state of the web application
 
 
 
Note: Detailed explanation on pp 112-114 in your text.
Be sure to read it!
SE-2840 Dr. Mark L. Hornick
17
These contain all kinds of
useful stuff
S
e
r
v
l
e
t
 
e
x
e
c
u
t
i
o
n
 
 
P
a
r
t
 
1
 
o
f
 
2
SE-2840 Dr. Mark L. Hornick
18
Note: This diagram can
be found in your textbook
S
e
r
v
l
e
t
 
e
x
e
c
u
t
i
o
n
 
 
P
a
r
t
 
2
 
o
f
 
2
T
h
e
 
H
T
T
P
 
R
e
q
u
e
s
t
 
W
r
a
p
p
e
r
 
C
l
a
s
s
SE-2840 Dr. Mark L. Hornick
19
A reference to an HTTPServletRequest
is created by the Container
and passed to the doGet() and doPost()
methods of an HTTPServlet
These methods are about
HTTP things like headers,
sessions, and cookies
T
h
e
 
H
T
T
P
 
R
e
s
p
o
n
s
e
 
W
r
a
p
p
e
r
 
C
l
a
s
s
SE-2840 Dr. Mark L. Hornick
20
A reference to an HTTPServletResponse is created by the Container
and passed to the doGet() and doPost() methods of an HTTPServlet
These methods are also
about HTTP things like
headers, sessions, and
cookies
Slide Note
Embed
Share

Interaction between web clients and servers in Java servlet-based web apps, from handling HTTP requests to employing helper apps for dynamic content generation. Overview of server responses, CGI helper programs, and how servlets work within a web container like Tomcat.

  • Java Servlets
  • Web Applications
  • Web Servers
  • Servlet Architecture
  • Dynamic Content

Uploaded on Oct 06, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Java Servlet-based web apps Servlet Architecture SE-2840 Dr. Mark L. Hornick 1

  2. Recall: The interaction between web clients and servers is structured around HTTP Request and Response messages Server is running a web server app, like Apache or Microsoft IIS. SE-2840 Dr. Mark L. Hornick 2

  3. In the simplest scenario, the Server responds to a browser GET request by returning a pre-written, static HTML file HTTP GET request HTML file maintained on Server, returned to the Browser as the HTTP response payload Note: This diagram can be found in your textbook SE-2840 Dr. Mark L. Hornick 3

  4. Eclipse J2EE demo CS-4220 Dr. Mark L. Hornick 4

  5. A web server can employ a Helper App when it needs to go beyond serving static web pages HTTP GET or POST request (may include parameters) parameters CGI Helper app CGI* programs can be written in Perl, Python, PHP, C, or Java *Common Gateway Interface SE-2840 Dr. Mark L. Hornick 5

  6. How it works in general User enters a URL (or clicks a link) to a CGI program rather than a static page Web server sees that the request is for a helper program, so the server runs the helper, sending along any parameters sent from the Client. The helper app constructs the brand new (dynamic) page and sends the HTML back to the server. Note: This diagram can be found in your textbook SE-2840 Dr. Mark L. Hornick 6

  7. How it works for Java Servlets Web container app is Tomcat Web server app is commonly Apache Servlets are run by Tomcat Note: This diagram can be found in your textbook SE-2840 Dr. Mark L. Hornick 7

  8. What does a Container like Tomcat do? Communication Creates server-side sockets Listens for client connections Determines client HTTP request type and decodes HTTP headers Servlet Lifecycle management Figures out which Servlet should be used to process a specific request Handles Servlet class loading Handles Servlet instantiation/construction Handles Servlet initialization Servlet execution support Launches/manages threads that service each incoming request Handles Servlet service() method (doGet and doPost) invocation Creates and passes Request and Response objects to the Servlet Supports Security Supports JSP SE-2840 Dr. Mark L. Hornick 8

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

  10. Tomcat invokes a Servletsservice() method, but your HTTPServlet-derived class should only override doGet() or doPost() The service() method is given an implementation in the HTTPServlet base class, where the doGet() and doPost() methods are called. You must override these methods in your HttpServlet-derived class SE-2840 Dr. Mark L. Hornick 10

  11. A Servlet is just a Java class that implements some specific interfaces (defined by the Java Servlet Specifications) that are used by the Container class Servlet-api classes All Servlets must implement these 5 methods java.lang.Object java.lang.Object interface Servlet interface ServletConfig + destroy() : void + getServletConfig() : ServletConfig + getServletInfo() : String + init(ServletConfig) : void + service(ServletRequest, ServletResponse) : void + getInitParameter(String) : String + getInitParameterNames() : Enumeration + getServletContext() : ServletContext + getServletName() : String -config Abstract class. Implements most of the basic servlet methods Implements the service() method and calls doGet(), doPost() etc as appropriate java.lang.Object java.io.Serializable java.io.Serializable GenericServlet HttpServlet + destroy() : void + GenericServlet() : void + getInitParameter(String) : String + getInitParameterNames() : Enumeration + getServletConfig() : ServletConfig + getServletContext() : ServletContext + getServletInfo() : String + getServletName() : String + init(ServletConfig) : void + init() : void + log(String) : void + log(String, Throwable) : void + service(ServletRequest, ServletResponse) : void # doDelete(HttpServletRequest, HttpServletResponse) : void # doGet(HttpServletRequest, HttpServletResponse) : void # doHead(HttpServletRequest, HttpServletResponse) : void # doOptions(HttpServletRequest, HttpServletResponse) : void # doPost(HttpServletRequest, HttpServletResponse) : void # doPut(HttpServletRequest, HttpServletResponse) : void # doTrace(HttpServletRequest, HttpServletResponse) : void # getLastModified(HttpServletRequest) : long + HttpServlet() : void # service(HttpServletRequest, HttpServletResponse) : void + service(ServletRequest, ServletResponse) : void SE-2840 Dr. Mark L. Hornick 11

  12. NOTE The Java classes pertaining to Servlets are not part of the standard SE They are part of the Java EE specification Implementation of the 1.x SE is provided in the 1.x JDK/JRE System Library This is the library you are probably most familiar with rt.jar is the main jarfile in this library Container vendors supply the implementation of the classes that are part of the Servlet specification Tomcat comes with its own Servlet libraries servlet-api.jar implements the Servlet-related classes SE-2840 Dr. Mark L. Hornick 12

  13. Parameters: HTML <form> tag element and the name of the Web Resource that will process the form data if it is submitted The opening <form> tag all form elements go between the opening and closing tag. <form action="http://<url>" method= post"> <!-- form elements go here --> </form> The method attribute specifies which HTTP message will be used to send the data in the form to the server default is get The required action attribute specifies the url of where to send the form s data. Note: See the examples on the course website SE-2840 Dr. Mark L. Hornick 13

  14. GET vs. POST scenarios Note: This diagram can be found in your textbook SE-2840 Dr. Mark L. Hornick 14

  15. get specifies that a HTTP GET message should be used, which appends the form data to the end of the url http://<domain>/<resource>?firstname=Arnold&last name=Ziffel get requests have a limit of 256 characters The data is plainly visible in the url (insecure!) You can bookmark a page that is the result of submitting a form Use GET only to submit small amounts of insensitive data which the server app will NOT use to modify its internal state SE-2840 15 Dr. Mark L. Hornick

  16. post specifies that a HTTP POST message should be used, which appends the form data to the end of the HTTP POST header There is no limit on the size of the data packet that can be sent to the server You cannot bookmark a url that was generated as a POST message, since the form data is not in the url A post request can be encrypted (using HTTPS) in order to protect sensitive data, such as a credit card numbers or passwords Use POST to send form data that Is sensitive (use encryption in that case) If the data is large (>256 bytes) Will change the state of the web application Note: Detailed explanation on pp 112-114 in your text. Be sure to read it! SE-2840 16 Dr. Mark L. Hornick

  17. Servlet execution Part 1 of 2 These contain all kinds of useful stuff SE-2840 Dr. Mark L. Hornick 17

  18. Servlet execution Part 2 of 2 Note: This diagram can be found in your textbook 18 SE-2840 Dr. Mark L. Hornick

  19. The HTTP Request Wrapper Class class Request classes java.lang.Object java.lang.Object interface interface servlet::ServletRequest http::HttpServletRequest java.io.InputStream servlet::ServletInputStream + # readLine(byte[], int, int) : int ServletInputStream() : void -request java.lang.Object provides access to servlet::ServletRequestWrapper http::HttpServletRequestWrapper + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + getAttribute(String) : Object getAttributeNames() : Enumeration getCharacterEncoding() : String getContentLength() : int getContentType() : String getInputStream() : ServletInputStream getLocalAddr() : String getLocale() : Locale getLocales() : Enumeration getLocalName() : String getLocalPort() : int getParameter(String) : String getParameterMap() : Map getParameterNames() : Enumeration getParameterValues(String) : String[] getProtocol() : String getReader() : BufferedReader getRealPath(String) : String getRemoteAddr() : String getRemoteHost() : String getRemotePort() : int getRequestDispatcher(String) : RequestDispatcher getScheme() : String getServerName() : String getServerPort() : int isSecure() : boolean removeAttribute(String) : void ServletRequestWrapper(ServletRequest) : void setAttribute(String, Object) : void setCharacterEncoding(String) : void + + + + + + + + + + + + + + + + + + + + + + + + + + getAuthType() : String getContextPath() : String getCookies() : Cookie[] getDateHeader(String) : long getHeader(String) : String getHeaderNames() : Enumeration getHeaders(String) : Enumeration getIntHeader(String) : int getMethod() : String getPathInfo() : String getPathTranslated() : String getQueryString() : String getRemoteUser() : String getRequestedSessionId() : String getRequestURI() : String getRequestURL() : StringBuffer getServletPath() : String getSession(boolean) : HttpSession getSession() : HttpSession getUserPrincipal() : Principal HttpServletRequestWrapper(HttpServletRequest) : void isRequestedSessionIdFromCookie() : boolean isRequestedSessionIdFromURL() : boolean isRequestedSessionIdFromUrl() : boolean isRequestedSessionIdValid() : boolean isUserInRole(String) : boolean These methods are about HTTP things like headers, sessions, and cookies A reference to an HTTPServletRequest is created by the Container and passed to the doGet() and doPost() methods of an HTTPServlet property get + getRequest() : ServletRequest property set + setRequest(ServletRequest) : void SE-2840 Dr. Mark L. Hornick 19

  20. The HTTP Response Wrapper Class class Response Classes java.lang.Object java.lang.Object java.io.OutputStream interface interface servlet::ServletOutputStream servlet::ServletResponse http::HttpServletResponse -response provides access to java.lang.Object servlet::ServletResponseWrapper http::HttpServletResponseWrapper + addCookie(Cookie) : void + addDateHeader(String, long) : void + addHeader(String, String) : void + addIntHeader(String, int) : void + containsHeader(String) : boolean + encodeRedirectURL(String) : String + encodeRedirectUrl(String) : String + encodeURL(String) : String + encodeUrl(String) : String + HttpServletResponseWrapper(HttpServletResponse) : void + sendError(int, String) : void + sendError(int) : void + sendRedirect(String) : void + setDateHeader(String, long) : void + setHeader(String, String) : void + setIntHeader(String, int) : void + setStatus(int) : void + setStatus(int, String) : void + flushBuffer() : void + getBufferSize() : int + getCharacterEncoding() : String + getContentType() : String + getLocale() : Locale + getOutputStream() : ServletOutputStream + getWriter() : PrintWriter + isCommitted() : boolean + reset() : void + resetBuffer() : void + ServletResponseWrapper(ServletResponse) : void + setBufferSize(int) : void + setCharacterEncoding(String) : void + setContentLength(int) : void + setContentType(String) : void + setLocale(Locale) : void These methods are also about HTTP things like headers, sessions, and cookies property get + getResponse() : ServletResponse property set + setResponse(ServletResponse) : void A reference to an HTTPServletResponse is created by the Container and passed to the doGet() and doPost() methods of an HTTPServlet SE-2840 Dr. Mark L. Hornick 20

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#