Please enable JavaScript.
Coggle requires JavaScript to display documents.
Servlets (web.xml (security-constraint defines access privileges to a…
Servlets
web.xml
security-constraint
defines access privileges to a collection of resources
resource-ref
reference lookup to an external resource.
servlet
maps servlet-class to servlet-name and includes init-params
login-config
- define auth-method (BASIC, FORM, CLIENT-CERT), realm-name and the optional form
servlet-mapping
maps url-name to servlet-name
listener
Define an application listener
filter-mapping
URL pattern and servlet name to which the filter name is mapped
filter
Defines filter name, class and its initialization params
session-config
definse session-timeout
context-param
- Contains context parameters which are applicable to entire app
ejb-ref
defines a reference to an ejb
Container
Multithreading Support
- Creates threads for each servlet request
Declarative Security
Manage security via web.xml
Lifecycle Management
Takes care of loading classes, instantiating and initializing servlets, invoking servlet methods, making them eligible for gc
JSP Support
Communications support
Allows servlets to talk to web server. Takes care of server sockets, listening on ports, creating streams etc
Names in web.xml
servlet-name
Deployer known internal name
servlet-class
Actual file name
url-pattern
Client-known URL name
Dir Structure for Development
Root dir of web app
src
- Java code
classes
compiled classes
lib
3rd party jars
web
- static and dynamic resources
etc
web.xml
Dir Structure for Deployment
webapps/root war
WEB-INF
classes
- compiled app code
lib
3rd party jars
web.xml
static/dynamic resources in root war
Processing
User clicks a link that a URL to a servlet
Container creates HttpServletResponse and HttpServletRequest objects
Container looks up web.xml finds out correct servlet based on url, allocates a thread for the request, calls the servlet's service() method and passes the two objects
Service() method looks at request and calls the appropriate doGet(), doPost() etc method and passes the two http objects
Servlet uses the response object to write response to client. Response goes back to Container
Service() metho completes, thread dies or goes back into the thread pool. Response goes back to client
Servlet LifeCycle
Finding the class
Container starts, looks for deployed web apps and servlet class files (by checking web.xml)
Container creates a servletContext and sets the context params in it
Container creates instance of ServletContextListener (if defined) and gives it the servlet context.
Container calls contextInitialized() method of listener and passes in the ServletContextEvent
Listener takes params, does any manipulations required (like setting up datasource), sets an attribute in the context
Loads the class
Container loads the class and then creates an object using no-args constructor.
Only one servlet instance created!
Initialization
Container calls init() after construction but before servlet is called by client. Gives access to ServletConfig and ServletContext. Init parameters are read only at startup
Client Request:
When a client request comes in, Container starts a new thread (or allocates from pool) and causes the servlet's service method to be invoked
Service method calls doGet, doPost etc based on http method in the request
When container shuts down, it calls the destroy() method
Container calls the contextDestroyed() method in servlet context listener
HttpServletRequest
Getters for attribute, localPort, remotePort, serverPort, parameter, contextPath, cookies, header, method, session etc
HttpServletResponse
Getters: outputStream, writer
Setters: contentType, contentLength, status
Others: addCookie, addHeader, encodeURL, sendError, sendRedirect
Function
taken in a request, process it and send back a response
Listeners