Date post: | 19-Jan-2017 |
Category: |
Education |
Upload: | mazenetsolution |
View: | 100 times |
Download: | 1 times |
Servlets
BySharmilee J
Java TrainerMazenet Solution
ServletConfig
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()
Syntax: public ServletConfig getServletConfig();
<web-app> <servlet> <init-param> <param-name>parametername</param-name> <param-value>parametervalue</param-value> </init-param> </servlet> </web-app>
ServletContext
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()
<web-app> ...... <context-param> <param-name>parametername</param-name> <param-value>parametervalue</param-value> </context-param> ...... </web-app>
Diff between ServletConfig & ServletContextServletConfig 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
Session Tracking
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.
Session Tracking Techniques:There are four techniques used in Session
tracking:• Cookies• Hidden Form Field• URL Rewriting• HttpSession
1. cookies
Cookies
• It is a piece of information.• In cookies, request from same client is not
considered as the new request.
Types of cookies.1. Non-Persistent cookie. Valid for single session2.Persistent cookie. Valid for multiple session.Advantage of Cookies:• State maintenanceDisadvantages:• Cookie can be disabled from the browser.• Only textual information can be set.
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
2. Hidden Form Field
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.
3. URL Rewriting
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.
4. HTTP Session
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();
Servlet Filters
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
Filter API
Three interfaces:• Filter• FilterChain• FilterConfigFilter interface life cycle methods:
1.init(filterConfig)2.doFilter(ServletRequest,ServletResponse,FilterChain)3.destroy()
FilterConfig interface life cycle methods:1.getInitParameter()2.getInitParameterNames()3.getServletContext()4.getFilterName()
FilterChain interface life cycle method:1.doFilter(ServletRequest,ServletResponse)