Servlet Technology


Servlet Technology

  • Servlet is a java technology used for developing a web application.
  • It specification is developed by sun Microsystems.
  • It is the set of rules in terms of interface that have to follow for developing the web-application .
  • Servlet technology is invasive by nature.
  • It is multithreaded, independent and dynamic java Program(because written in java).
  • Servlet is a interface as well as package ,controller and web component.

Requirement for Servlet Programming:

  • Web-server
  • Web-browser
  • Servlet
  • Xml
  • Html

Description:

  • Web-server: To deploy the web –application.
  • Web-browser: To give the request .
  • Servlet: To implement business logic.
  • Xml: To configure the request page , Servlet page ,url pattern  etc means it is work as deployment descriptor.
  • Html: To design form or presentation page.

Servlet Hierarchy:



Application Hierarchy:


Web.xml Description:

  • In this programmer map the basic and execution-al information of a web-application.
  • It is also known as deployment descriptor because it descript the servlet execution.
  • To maintain the servlet info in xml ,there is two basic tag:
Ø  <servlet>:contain basic info of servlet like servlet name and servlet class name.
Ø  <servlet-mapping>:contain executional info like servlet –name and url-pattern ,on the basis of url-pattern servlet will execute.

Servlet Life Cycle:

  • Init
  • Service
  • Destroy

  • These three are the Servlet life cycle in which init and destroy can call only once throughout Servlet life cycle but service can call as more than once.

Flow of Servlet:



  •  When user dispatch the request control first goes to web-container then it load the web.xml file.
  • Then web-container match the dispatched url with url-pattern ,if it matched then  picks the corresponding servlet-name then control goes to the servlet tag and match the corresponding servlet-name ,if servlet-name of servlet-mapping and servlet tag match then control goes to servlet-class otherwise generate error.
  • After servlet class will instantiate and then servlet life cycle comes in existence.
  • After that init will call then service method i.e business logic will execute and after dispatching the result on browser destroy will call and servlet finished there work..

RequestDispatcher:

  • It is  a interface .
  • Used to move from one page to another in a web-application.
  • It use request object to move from one page to another.
  • When move from one page to another url will not change.
  • It have two methods include and forward .
             
                 
  include: it first include the source and destination page content(which have to show on browser)                                 then dispatched it to browser as output.
                  
  forward :only forwards the control from one page to another doesn't include any content.


Use of RequestDispatcher:




  • url can be a servlet url-pattern, a jsp page and a html page.
  • It will use for  validating the user on the basis of entered value.
  • For example:
If user input name and password ,it first goes to the web.xml with the dispatched action(url) .Then it goes to corresponding servlet and validate the dispatched value from database if it validate then using forward ,control forwarded to  home page otherwise it again redirect to the login page with some message like  ‘username or password is invalid’ using include .
Incase of include it include the message from servlet page and show it onto the login page and incase of forward it only show the home page.

SendRedirect:



  • It is a  method of HttpServletResponse interface.
  • Use response object to move from one page to another.
  • It can move inside the application as well as outside the application. But its basic work is to move control outside the web-application.
  • It is server-side because it use response object but work like client-side because it moves browser to browser.

url: it can be a html page , jsp page or complete url like ‘http://www.google.com’.
When it move one page to another url will completely change.



Commonly used methods in Servlet:



  • getParameter()
  • setAttribute()
  • getAttribute() 


 getParameter():





  • This method is used to get the value from dispatched form or request.
  • It use request object to get value.
  • It’s return type is string.



setAttribute():





  • It is binding variable/object that is used to bind some values and move from one page to another within a web-application.
  • It use request , session , application… object for bind the values.



getAttribute():



  • It’s return type is object so need to type cast the value when we get it.
  • Scope:
  • request: it allow to get the value only on the dispatched page.
  • session: it allow to get the value on entire web-application on any page till session is validate.
  • application: it allow to get the value at anywhere in a web-application either session is validate or invalidate.

Session and Session Tracking:        

  • Simply Session  is a time of interval.It corresponds to one user, who may visit a web-application many times.For each request of user a session will create that uniquely identify the valid user.
  • The server can maintain a session in many ways such as using cookies ,URL-rewriting,hidden form field etc. 
  • It validates the user on the basis of dispatched request for a particular time being .  it generate a unique id on the basis of this id user is valid through out the application.
  • It also maintain the state of user .
  • Http protocol is a state less protocol so in web application we have to manually maintain the state.
  • Way to maintain session (user state) in a web-application is session Tracking


     






HttpSession:




  • HttpSession is interface ,belongs to ‘javax.servlet.http’ package.
  • The servlet container uses this interface to create a session between an HTTP client and an HTTP server.
  • The session persists for a specified time period, across more than one connection or page request from the user.
  • This interface allows servlets to View and manipulate information about a session, such as the session identifier, creation time, and last accessed time
  • Bind objects to sessions, allowing user information to persist across multiple user connections.
  • Provides a way to identify a user across more than one page request or visit to a Web site and to store information about that user. 
. ..

*************************************************************************
Reach us At: - 0120-4029000 / 24 / 25 / 27 / 29 Mobile: 9953584548
Write us at: - Smruti@apextgi.com and pratap@apextgi.com








No comments:

Post a Comment