This document discusses servlets, servlet configuration, servlet context, session tracking techniques, and servlet filters in Java web applications. It explains that servletconfig is used to configure individual servlets while servletcontext is used for global application configuration. Session tracking can be done via cookies, hidden form fields, URL rewriting, or HTTP session. Filters provide preprocessing and postprocessing of requests and have initialization, processing, and destruction lifecycle methods.
3. Servlet config Interface
• An object of ServletConfig is created by the web
container for each servlet.
• This object can be used to get configuration
information from web.xml file
Advantage:
• No need to update in web.xml if any changes
occurs.
Method:
getServletConfig()
6. ServletContext Interface
• An object of ServletContext is created by the
web container at time of deploying the project
• <context-param>element is used
Method:
getServletContext() method of ServletConfig
interface returns the object of ServletContext.
Syntax:
public ServletContext getServletContext()
8. Diff between ServletConfig & ServletContext
ServletConfig ServletContext
ServletConfig available
in javax.servlet.*; package
ServletContext available in
javax.servlet.*; package
ServletConfig object is one per servlet
class
ServletContext object is global to
entire web application
Object of ServletConfig will be created
during initialization process of the
servlet
Object of ServletContext will be
created at the time of web
application deployment
Scope: As long as a servlet is
executing, ServletConfig object will be
available, it will be destroyed once the
servlet execution is completed.
Scope: As long as web application is
executing, ServletContext object will
be available, and it will be destroyed
once the application is removed from
the server.
We should give request explicitly, in
order to create ServletConfig object for
the first time
ServletContext object will be available
even before giving the first request
In web.xml – <init-param> tag will be
appear under <servlet-class> tag
In web.xml – <context-param> tag will
be appear under <web-app> tag
10. Session Tracking
• Session refers a particular interval of time.
• Session tracking is used to maintain the state
of an user. It is known as session management.
11. Session Tracking Techniques:
There are four techniques used in Session
tracking:
• Cookies
• Hidden Form Field
• URL Rewriting
• HttpSession
13. Cookies
• It is a piece of information.
• In cookies, request from same client is not
considered as the new request.
14. Types of cookies.
1. Non-Persistent cookie.
Valid for single session
2.Persistent cookie.
Valid for multiple session.
Advantage of Cookies:
• State maintenance
Disadvantages:
• Cookie can be disabled from the browser.
• Only textual information can be set.
15. Cookie creation:
• Cookie ck=new Cookie("user","sonoo jaiswal");
//creating cookie object
• response.addCookie(ck);
//adding cookie in the response
Cookie deletion:
• Cookie ck=new Cookie("user","");
//deleting value of cookie
• ck.setMaxAge(0);
//changing the maximum age to 0 seconds
• response.addCookie(ck);
//adding cookie in the response
17. Hidden Form field
• We store the information in the hidden field
and get it from another servlet.
<input type="hidden" name="uname" value=“java">
Advantage:
• Work when the cookie is disabled.
Disadvantages:
• Only textual information can be sent.
• Maintained at server side.
19. URL Rewriting
• Appending session ID to the url is known as
url Rewriting.
• We implement url rewriting by caling,
response.encodeURL(String url);
Advantage:
• Work when the cookie is disabled.
• No form submission is not required as in case
of hidden form field.
21. Http Session
• Used to view and manipulate information
about a session, such as the session identifier,
creation time, and last accessed time.
• HttpSession session=request.getSession();
23. Servlet Filters
• A filter is an object that is invoked at the
preprocessing and postprocessing of a request.
• servlet filter is pluggable, i.e if we remove filter in
web.xml, filter is automatically removed.
Advantage
• Encryption and decryption
• Input validation
• Recording all incoming requests
24. Filter API
Three interfaces:
• Filter
• FilterChain
• FilterConfig
Filter interface life cycle methods:
1.init(filterConfig)
2.doFilter(ServletRequest,ServletResponse,FilterChain)
3.destroy()
25. FilterConfig interface life cycle methods:
1.getInitParameter()
2.getInitParameterNames()
3.getServletContext()
4.getFilterName()
FilterChain interface life cycle method:
1.doFilter(ServletRequest,ServletResponse)