+ All Categories
Home > Education > Java - Servlet - Mazenet Solution

Java - Servlet - Mazenet Solution

Date post: 19-Jan-2017
Category:
Upload: mazenetsolution
View: 100 times
Download: 1 times
Share this document with a friend
26
Servlets By Sharmilee J Java Trainer Mazenet Solution
Transcript
Page 1: Java - Servlet - Mazenet Solution

Servlets

BySharmilee J

Java TrainerMazenet Solution

Page 2: Java - Servlet - Mazenet Solution

ServletConfig

Page 3: Java - Servlet - Mazenet Solution

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()

Page 4: Java - Servlet - Mazenet Solution

Syntax: public ServletConfig getServletConfig(); 

<web-app>    <servlet>       <init-param>        <param-name>parametername</param-name>       <param-value>parametervalue</param-value>      </init-param>   </servlet>  </web-app>   

Page 5: Java - Servlet - Mazenet Solution

ServletContext

Page 6: Java - Servlet - Mazenet Solution

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()

Page 7: Java - Servlet - Mazenet Solution

<web-app>   ......          <context-param>      <param-name>parametername</param-name>      <param-value>parametervalue</param-value>    </context-param>   ......  </web-app>  

Page 8: Java - Servlet - Mazenet Solution

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

Page 9: Java - Servlet - Mazenet Solution

Session Tracking

Page 10: Java - Servlet - Mazenet Solution

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.

Page 11: Java - Servlet - Mazenet Solution

Session Tracking Techniques:There are four techniques used in Session

tracking:• Cookies• Hidden Form Field• URL Rewriting• HttpSession

Page 12: Java - Servlet - Mazenet Solution

1. cookies

Page 13: Java - Servlet - Mazenet Solution

Cookies

• It is a piece of information.• In cookies, request from same client is not

considered as the new request.

Page 14: Java - Servlet - Mazenet Solution

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.

Page 15: Java - Servlet - Mazenet Solution

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  

Page 16: Java - Servlet - Mazenet Solution

2. Hidden Form Field

Page 17: Java - Servlet - Mazenet Solution

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.

Page 18: Java - Servlet - Mazenet Solution

3. URL Rewriting

Page 19: Java - Servlet - Mazenet Solution

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.

Page 20: Java - Servlet - Mazenet Solution

4. HTTP Session

Page 21: Java - Servlet - Mazenet Solution

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();      

Page 22: Java - Servlet - Mazenet Solution

Servlet Filters

Page 23: Java - Servlet - Mazenet Solution

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

Page 24: Java - Servlet - Mazenet Solution

Filter API

Three interfaces:• Filter• FilterChain• FilterConfigFilter interface life cycle methods:

1.init(filterConfig)2.doFilter(ServletRequest,ServletResponse,FilterChain)3.destroy()

Page 25: Java - Servlet - Mazenet Solution

FilterConfig interface life cycle methods:1.getInitParameter()2.getInitParameterNames()3.getServletContext()4.getFilterName()

FilterChain interface life cycle method:1.doFilter(ServletRequest,ServletResponse)

Page 26: Java - Servlet - Mazenet Solution

Recommended