sevlet

Upload: dddd

Post on 07-Jul-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/19/2019 Sevlet

    1/14

    Unit III Servlet 10

    3.1 Introduction of servlet: How servelet work, installation, model diagram

    What are Servlets?

     Java Servlets are programs that run on a We or !pplication server and act as amiddle la"er etween a re#uests coming from a We rowser or other H$$% client

    and dataases or applications on the H$$% server.

    &sing Servlets, "ou can collect input from users through we page forms, presentrecords from a dataase or another source, and create we pages d"namicall".

     Java Servlets often serve the same purpose as programs implemented using the

    'ommon (atewa" Interface )'(I*. +ut Servlets oer several advantages incomparison with the '(I.

    • %erformance is signi-cantl" etter.

    Servlets eecute within the address space of a We server. It is notnecessar" to create a separate process to handle each client re#uest.

    • Servlets are platform/independent ecause the" are written in Java.

    •  Java securit" manager on the server enforces a set of restrictions to protect

    the resources on a server machine. So servlets are trusted.

    •  $he full functionalit" of the Java class liraries is availale to a servlet. It can

    communicate with applets, dataases, or other software via the sockets and0I mechanisms that "ou have seen alread".

    Servlets Architecture

     $he following diagram shows the position of Servlets in a We !pplication.

  • 8/19/2019 Sevlet

    2/14

    How Servlet works2

    It is important to learn how servlet works for understanding the servlet well. Here,

    we are going to get the internal detail aout the -rst servlet program.

     $he server checks if the servlet is re#uested for the rst time.

    If yes, we container does the following tasks:

    • oads the servlet class.

    • Instantiates the servlet class.

    • calls the init method passing the Servlet'on-g o4ect

    Else

    • calls the service method passing re#uest and response o4ects

     $he we container calls the destro" method when it needs to remove the servlet

    such as at time of stopping server or underl"ing the pro4ect.

    How we container handles the servlet re#uest2

     $he we container is responsile to handle the re#uest. et5s see how it handles the

    re#uest.

    • maps the re#uest with the servlet in the we.ml -le.

    • creates re#uest and response o4ects for this re#uest

    • calls the service method on the thread

    •  $he pulic service method internall" calls the protected service method

    •  $he protected service method calls the do(et method depending on the t"pe

    of re#uest.

    •  $he do(et method generates the response and it is passed to the client.

    • !fter sending the response, the we container deletes the re#uest and

    response o4ects. $he thread is contained in the thread pool or deleteddepends on the server implementation.

    Advantage of Servlet:  $here are man" advantages of servlet over '(I. $he

    we container creates threads for handling the multiple re#uests to the servlet.

     $hreads have a lot of ene-ts over the %rocesses such as the" share a commonmemor" area, lightweight, cost of communication etween the threads are low. $he

    asic ene-ts of servlet are as follows:

    1. !etter "erformance: ecause it creates a thread for each re#uest not

    process.

    6. #orta$ility: ecause it uses 4ava language.

  • 8/19/2019 Sevlet

    3/14

    3. %o$ust: Servlets are managed " J7 so we don5t need to worr" aoutmemor" leak, garage collection etc.

    8. Secure: ecause it uses 4ava language.

    Setting U#

    ! development environment is where "ou would develop "our Servlet, test them

    and -nall" run them.

    ike an" other Java program, "ou need to compile a servlet " using the Java

    compiler &avac and after compilation the servlet application, it would e deplo"ed

    in a con-gured environment to test and run.

     $his development environment setup involves following steps:

    Setting up Java 9evelopment it

     $his step involves downloading an implementation of the Java Software

    9evelopment it )S9* and setting up %!$H environment variale appropriatel".

     ;ou can download S9 from

  • 8/19/2019 Sevlet

    4/14

    setenv %!$H @usr@local@ 4dk1.B.C ?6C@in:%!$H

    setenv J!7!?H

  • 8/19/2019 Sevlet

    5/14

    @usr@local@apache/tomcat/B.B.6@[email protected]

    !fter startup, the default we applications included with $omcat will e availale "

    visiting htt":''localhost:(0(0' . If ever"thing is -ne then it should displa"following result:

    >urther information aout con-guring and running $omcat can e found in the

    documentation included here, as well as on the $omcat we site:

    http:@@tomcat.apache.org

     $omcat can e stopped " eecuting the following commands on windows

    machine:

    ':Aapache/tomcat/B.B.6AinAshutdown

     $omcat can e stopped " eecuting the following commands on &ni )Solaris,

    inu, etc.* machine:

    @usr@local@apache/tomcat/B.B.6@[email protected]

    Setting up '!SS%!$H

  • 8/19/2019 Sevlet

    6/14

    Since servlets are not part of the Java %latform, Standard =dition, "ou must identif"

    the servlet classes to the compiler.

    If "ou are running Windows, "ou need to put the following lines in "our

    ':Aautoeec.at -le.

    set '!$!IG!D':Aapache/tomcat/B.B.6

    set '!SS%!$HDF'!$!IG!FAcommonAliAservlet/api. 4arEF'!SS%!$HF

    !lternativel", on Windows G$@6CCC@%, "ou could also right/click on " 'omputer,

    select %roperties, then !dvanced, then =nvironment 7ariales. $hen, "ou would

    update the '!SS%!$H value and press the

  • 8/19/2019 Sevlet

    7/14

    +he init2 3ethod

     $he init method is called onl" once. It is called onl" when the servlet is created, and

    not called for an" user re#uests afterwards. So, it is used for one/time initialiKations,

     4ust as with the init method of applets.

     $he servlet is normall" created when a user -rst invokes a &0 corresponding to theservlet, ut "ou can also specif" that the servlet e loaded when the server is -rst

    started.

    When a user invokes a servlet, a single instance of each servlet gets created, with

    each user re#uest resulting in a new thread that is handed o to do(et or do%ost as

    appropriate. $he init)* method simpl" creates or loads some data that will e usedthroughout the life of the servlet.

     $he init method de-nition looks like this:

    pulic void init)* throws Servlet=ception L

    @@ InitialiKation code...

    M

    +he service2 3ethod

     $he service)* method is the main method to perform the actual task. $he servlet

    container )i.e. we server* calls the service)* method to handle re#uests coming

    from the client) rowsers* and to write the formatted response ack to the client.

    =ach time the server receives a re#uest for a servlet, the server spawns a new

    thread and calls service. $he service)* method checks the H$$% re#uest t"pe )(=$,

    %

  • 8/19/2019 Sevlet

    8/14

    ! (=$ re#uest results from a normal re#uest for a &0 or from an H$ form thathas no =$HinaliKation code...

    M

    -6 Servlet A#I: "ac7ages &ava8-servlet and &ava8-servlet-htt"

    Gow that "ou have a asic understanding of H$$%, we can move on and talk aout

    the Servlet !%I that "ou5ll e using to create H$$% servlets, or an" kind of servlets,for that matter. Servlets use classes and interfaces from two

    packages: 4ava.servlet and 4ava.servlet.http . $he 4ava.servlet package containsclasses to support generic, protocol/independent servlets. $hese classes are

    etended " the classes in the 4ava.servlet.http package to add H$$%/speci-c

    functionalit". $he top/level package name is 4ava instead of the familiar 4ava, toindicate that the Servlet !%I is a standard etension.

  • 8/19/2019 Sevlet

    9/14

    =ver" servlet must implement the 4ava.servlet.Servlet interface. ost servletsimplement it " etending one of two special classes: 4ava.

    servlet.(enericServlet or 4ava.servlet.http.HttpServlet . ! protocol/independent

    servlet should suclass (enericServlet, while an H$$% servlet shouldsuclass HttpServlet, which is itself a suclass of (enericServlet with added H$$%/

    speci-c functionalit".&nlike a regular Java program, and 4ust like an applet, a servlet does not havea main)* method. Instead, certain methods of a servlet are invoked " the server in

    the process of handling re#uests. =ach time the server dispatches a re#uest to a

    servlet, it invokes the servlet5s service)* method.

    Interfaces in 4ava.servlet package

     $here are man" interfaces in 4ava.servlet package. $he" are as follows:

    1. Servlet

    6. Servlet0e#uest

    3. Servlet0esponse

    8. 0e#uest9ispatcher

    B. Servlet'on-g

    N. Servlet'ontet

    O. Single$hreadodel

    P. >ilter

    . >ilter'on-g

    1C.>ilter'hain

    11.Servlet0e#uestistener

    16.Servlet0e#uest!ttriuteistener

    13.Servlet'ontetistener

    18.Servlet'ontet!ttriuteistener

    'lasses in 4ava.servlet package

     $here are man" classes in 4ava.servlet package. $he" are as follows:

  • 8/19/2019 Sevlet

    10/14

    1. (enericServlet

    6. ServletInputStream

    3. Servlet

  • 8/19/2019 Sevlet

    11/14

    O. Http&tils )deprecated now*

    -9 Session

    Session simpl" means a particular interval of time.

    Session +rac7ing is a wa" to maintain state )data* of an user. It is also known

    as session management in servlet.

    Http protocol is a stateless so we need to maintain state using session tracking

    techni#ues. =ach time user re#uests to the server, server treats the re#uest as the

    new re#uest. So we need to maintain the state of an user to recogniKe to particularuser.

    H$$% is stateless that means each re#uest is considered as the new re#uest. It is

    shown in the -gure given elow:

    Why use Session +rac7ing?

    +o recognie the user it is used to recogniKe the particular user.

    Session $racking $echni#ues

     $here are four techni#ues used in Session tracking:

    1. oo7ies

    6. ;idden

  • 8/19/2019 Sevlet

    12/14

    ! weserver can assign a uni#ue session I9 as a cookie to each we client and forsuse#uent re#uests from the client the" can e recogniKed using the recieved

    cookie.

     $his ma" not e an eective wa" ecause man" time rowser does not support acookie, so I would not recommend to use this procedure to maintain the sessions.

    ;idden ...* h"pertet link does not result in a form sumission, so hidden

    form -elds also cannot support general session tracking.

    U%/ %e=riting:

     ;ou can append some etra data on the end of each &0 that identi-es the session,

    and the server can associate that session identi-er with data it has stored aout

    that session.

    >or eample, with http:@@[email protected], the session

    identi-er is attached as sessionidD1638B which can e accessed at the we server

    to identif" the client.&0 rewriting is a etter wa" to maintain sessions and works for the rowsers when

    the" don5t support cookies ut here drawack is that "ou would have generate

    ever" &0 d"namicall" to assign a session I9 though page is simple static H$page.

    +he ;tt"Session *$&ect:

    !part from the aove mentioned three wa"s, servlet provides HttpSession Interfacewhich provides a wa" to identif" a user across more than one page re#uest or visit

    to a We site and to store information aout that user.

     $he servlet container uses this interface to create a session etween an H$$% clientand an H$$% server. $he session persists for a speci-ed time period, across more

    than one connection or page re#uest from the user.

    oo7ies: ty"es, advantages and disadvantages

    -> oo7ies in Servlet

  • 8/19/2019 Sevlet

    13/14

    ! coo7ie is a small piece of information that is persisted etween the multipleclient re#uests.

    ! cookie has a name, a single value, and optional attriutes such as a comment,

    path and domain #uali-ers, a maimum age, and a version numer.

    How 'ookie works

    +" default, each re#uest is considered as a new re#uest. In cookies techni#ue, we

    add cookie with response from the servlet. So cookie is stored in the cache of therowser. !fter that if re#uest is sent " the user, cookie is added with re#uest "

    default. $hus, we recogniKe the user as the old user.

     $"pes of 'ookie

     $here are 6 t"pes of cookies in servlets.

    1. Gon/persistent cookie

    6. %ersistent cookie

    )on"ersistent coo7ie

    It is valid for single session onl". It is removed each time when user closes therowser.

    #ersistent coo7ie

    It is valid for multi"le session . It is not removed each time when user closes therowser. It is removed onl" if user logout or signout.

    !dvantage of 'ookies

    1. Simplest techni#ue of maintaining the state.

    6. 'ookies are maintained at client side.

    9isadvantage of 'ookies

    1. It will not work if cookie is disaled from the rowser.

  • 8/19/2019 Sevlet

    14/14

    6.