SlideShare a Scribd company logo
1 of 20
JSP Tutorial
JSP
   A JSP page is a text-based document that contains
    two types of text: static template data, which can be
    expressed in any text-based format, such as HTML,
    SVG, WML, and XML; and JSP elements, which
    construct dynamic content.

   A JSP page has extension .jsp
    example
        hello.jsp
The Life Cycle of a JSP Page


   A JSP page services requests as a servlet. Thus, the life cycle and many of the
    capabilities of JSP pages (in particular the dynamic aspects) are determined by

    Java Servlet technology




   When a request is mapped to a JSP page, it is handled by a special servlet that
    first checks whether the JSP page's servlet is older than the JSP page. If it is, it
    translates the JSP page into a servlet class and compiles the class. During
    development, one of the advantages of JSP pages over servlets is that the
    build process is performed automatically.
Translation and Compilation

   During the translation phase, each type of data in a JSP page is treated differently.
    Template data is transformed into code that will emit the data into the stream that
    returns data to the client. JSP elements are treated as follows:

   Directives are used to control how the Web container translates and executes the JSP
    page.
   Scripting elements are inserted into the JSP page's servlet class.
   Elements of the form <jsp:XXX ... /> are converted into method calls to JavaBeans

    components or invocations of the Java Servlet API.
   Both the translation and compilation phases can yield errors that are only observed
    when the page is requested for the first time. If an error occurs while the page is being
    translated (for example, if the translator encounters a malformed JSP element), the
    server will return a ParseException, and the servlet class source file will be empty or

    incomplete. The last incomplete line will give a pointer to the incorrect JSP element.
Servlet Life Cycle


   The life cycle of a servlet is controlled by the container in which
    the servlet has been deployed. When a request is mapped to a
    servlet, the container performs the following steps.
   If an instance of the servlet does not exist, the Web container
     –   Loads the servlet class.
     –   Creates an instance of the servlet class.
     –   Initializes the servlet instance by calling the init method.
   Invokes the service method, passing a request and response
    object.
   If the container needs to remove the servlet, it finalizes the
    servlet by calling the servlet's destroy method.
JSP Life Cycle
   Once the page has been translated and compiled, the JSP
    page's servlet for the most part follows the servlet life cycle
    described in the Servlet Life Cycle:
   If an instance of the JSP page's servlet does not exist, the
    container:
     –   Loads the JSP page's servlet class
     –   Instantiates an instance of the servlet class
     –   Initializes the servlet instance by calling the jspInit method
   Invokes the _jspService method, passing a request and
    response object.
   If the container needs to remove the JSP page's servlet, it calls
    the jspDestroy method.
Execution


 You can control various JSP page execution
  parameters using by page directives.
Creating Static Content
   You create static content in a JSP page by simply writing it as if you were creating a page
    that consisted only of that content. Static content can be expressed in any text-based
    format, such as HTML, WML, and XML.

   The default format is HTML.

   If you want to use a format other than HTML, you include a page directive with the
    contentType attribute set to the format type at the beginning of your JSP page. For
    example, if you want a page to contain data expressed in the wireless markup language
    (WML), you need to include the following directive:


           <%@ page contentType="text/vnd.wap.wml"%>
Creating Dynamic Content


 You can create dynamic content by
  accessing Java programming language
  objects from within scripting elements.

Using Objects within JSP Pages
  You can access a variety of objects, including enterprise beans and JavaBeans
  components, within a JSP page. JSP technology automatically makes some
  objects available, and you can also create and access application-specific

  objects.
Implicit Objects

application
      The context for the JSP page's servlet and any Web components contained in the same application.

config
      Initialization information for the JSP page's servlet.

exception
     Accessible only from an error page.

out
       The output stream.

page
       The instance of the JSP page's servlet processing the current request. Not typically used by JSP page authors.

pageContext
     The context for the JSP page. Provides a single API to manage the various scoped attributes.

request
     The request triggering the execution of the JSP page.

response
     The response to be returned to the client. Not typically used by JSP page authors.

session
     The session object for the client.
Application-Specific Objects


    When possible, application behavior should be encapsulated in
     objects so that page designers can focus on presentation
     issues. Objects can be created by developers who are
     proficient in the Java programming language and in accessing
     databases and other services. There are four ways to create
     and use objects within a JSP page:

1.   Instance and class variables of the JSP page's servlet class are created in declarations and accessed in scriptlets and
     expressions.
2.   Local variables of the JSP page's servlet class are created and used in scriptlets and expressions.
3.   Attributes of scope objects are created and used in scriptlets and expressions.
4.   JavaBeans components can be created and accessed using streamlined JSP elements. You can also create a JavaBeans
     component in a declaration or scriptlet and invoke the methods of a JavaBeans component in a scriptlet or expression.
Shared Objects

   The conditions affecting concurrent access to shared objects apply to objects accessed
    from JSP pages that run as multithreaded servlets. You can indicate how a Web container
    should dispatch multiple client requests with the following page directive:

    <%@ page isThreadSafe="true|false" %>

    When isThreadSafe is set to true, the Web container may choose to dispatch multiple
    concurrent client requests to the JSP page. This is the default setting. If using true, you
    must ensure that you properly synchronize access to any shared objects defined at the
    page level. This includes objects created within declarations, JavaBeans components with
    page scope, and attributes of the page scope object.

    If isThreadSafe is set to false, requests are dispatched one at a time, in the order they
    were received, and access to page-level objects does not have to be controlled. However,
    you still must ensure that access to attributes of the application or session scope objects
    and to JavaBeans components with application or session scope is properly synchronized.
JSP Scripting Elements

   JSP scripting elements are used to create and access objects, define methods, and manage the flow of
    control. Since one of the goals of JSP technology is to separate static template data from the code
    needed to dynamically generate content, very sparing use of JSP scripting is recommended. Much of the
    work that requires the use of scripts can be eliminated by using custom tags.

    JSP technology allows a container to support any scripting language that can call Java objects. If you
    wish to use a scripting language other than the default, java, you must specify it in a page directive at the
    beginning of a JSP page:

            <%@ page language="scripting language" %>

    Since scripting elements are converted to programming language statements in the JSP page's servlet
    class, you must import any classes and packages used by a JSP page. If the page language is java, you
    import a class or package with the page directive:

            <%@ page import="packagename.*, fully_qualified_classname" %>

    For example, the bookstore example page showcart.jsp imports the classes needed to implement the
    shopping cart with the following directive:

            <%@ page import="java.util.*, cart.*" %>
Declarations


  A JSP declaration is used to declare variables and methods in a page's scripting language.

  The syntax for a declaration is as follows:
         <%! scripting language declaration %>

  When the scripting language is the Java programming language, variables and methods in JSP
  declarations become declarations in the JSP page's servlet class.


  The bookstore example page initdestroy.jsp defines an instance variable named bookDBEJB and the
  initialization and finalization methods jspInit and jspDestroy discussed earlier in a declaration:

  <%!

        private BookDBEJB bookDBEJB;

        public void jspInit() {
         ...
        }

        public void jspDestroy() {
        ...
        }

  %>
Scriptlets

   A JSP scriptlet is used to contain any code fragment that is valid for the scripting language used in a page. The
    syntax for a scriptlet is as follows:
    <%
              scripting language statements
    %>
Example
    <%
              Iterator i = cart.getItems().iterator();
              while (i.hasNext()) {
                                  ShoppingCartItem item = (ShoppingCartItem)i.next();
                                  BookDetails bd = (BookDetails)item.getItem();
    %>
    <tr> <td align="right" bgcolor="#ffffff">
                 <%=item.getQuantity()%>
         </td>
         <td bgcolor="#ffffaa">
                <strong>
                   <a href=" <%=request.getContextPath()%>/bookdetails?bookId= <%=bd.getBookId()%>">
                       <%=bd.getTitle()%>
                   </a>
                </strong>
        </td>
                ……….
    <%
       // End of while
       }
     %>
Expressions

   A JSP expression is used to insert the value of a scripting language expression,
    converted into a string, into the data stream returned to the client. When the
    scripting language is the Java programming language, an expression is
    transformed into a statement that converts the value of the expression into a
    String object and inserts it into the implicit out object.

   The syntax for an expression is as follows:
                    <%= scripting language expression %>

    Note that a semicolon is not allowed within a JSP expression, even if the same
    expression has a semicolon when you use it within a scriptlet.
Including Content in a JSP Page


    There are two mechanisms for including
     another Web resource in a JSP page.

1.   include directive
2.   jsp:include element.
The include directive
   The include directive is processed when the JSP page is translated into a
    servlet class. The effect of the directive is to insert the text contained in another
    file--either static content or another JSP page--in the including JSP page. You
    would probably use the include directive to include banner content, copyright
    information, or any chunk of content that you might want to reuse in another
    page.

    The syntax for the include directive is as follows:
                    <%@ include file="filename" %>

    For example, all the bookstore application pages include the file banner.jsp
    containing the banner content with the following directive:
                    <%@ include file="banner.jsp" %>
The jsp:include element
   The jsp:include element is processed when a JSP page is executed. The include action
    allows you to include either a static or dynamic resource in a JSP file. The results of
    including static and dynamic resources are quite different. If the resource is static, its
    content is inserted into the calling JSP file. If the resource is dynamic, the request is sent
    to the included resource, the included page is executed, and then the result is included in
    the response from the calling JSP page.

    The syntax for the jsp:include element is as follows:
          <jsp:include page="includedPage" />

    The example to includes the page that generates the display of the localized date with the
    following statement:
                      <jsp:include page="date.jsp"/>
Transferring Control to Another Web Component




     The mechanism for transferring control to another Web component
      from a JSP page uses the functionality provided by the Java Servlet
      API. You access this functionality from a JSP page with the jsp:forward
      element:

                    <jsp:forward page="/main.jsp" />

More Related Content

What's hot

JSP Scope variable And Data Sharing
JSP Scope variable And Data SharingJSP Scope variable And Data Sharing
JSP Scope variable And Data Sharingvikram singh
 
Implicit objects advance Java
Implicit objects advance JavaImplicit objects advance Java
Implicit objects advance JavaDarshit Metaliya
 
INTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE AND JSP PROCESSING
INTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE  AND JSP PROCESSINGINTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE  AND JSP PROCESSING
INTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE AND JSP PROCESSINGAaqib Hussain
 
JAVA SERVER PAGES
JAVA SERVER PAGESJAVA SERVER PAGES
JAVA SERVER PAGESKalpana T
 
Servlet and jsp interview questions
Servlet and jsp interview questionsServlet and jsp interview questions
Servlet and jsp interview questionsSujata Regoti
 
Implementing jsp tag extensions
Implementing jsp tag extensionsImplementing jsp tag extensions
Implementing jsp tag extensionsSoujanya V
 
Jsp quick reference card
Jsp quick reference cardJsp quick reference card
Jsp quick reference cardJavaEE Trainers
 
Lap trinh web [Slide jsp]
Lap trinh web [Slide jsp]Lap trinh web [Slide jsp]
Lap trinh web [Slide jsp]Tri Nguyen
 
JSP - Java Server Page
JSP - Java Server PageJSP - Java Server Page
JSP - Java Server PageVipin Yadav
 

What's hot (20)

Java server pages
Java server pagesJava server pages
Java server pages
 
JSP Scope variable And Data Sharing
JSP Scope variable And Data SharingJSP Scope variable And Data Sharing
JSP Scope variable And Data Sharing
 
Unit 4 web technology uptu
Unit 4 web technology uptuUnit 4 web technology uptu
Unit 4 web technology uptu
 
Implicit objects advance Java
Implicit objects advance JavaImplicit objects advance Java
Implicit objects advance Java
 
INTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE AND JSP PROCESSING
INTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE  AND JSP PROCESSINGINTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE  AND JSP PROCESSING
INTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE AND JSP PROCESSING
 
Jsp element
Jsp elementJsp element
Jsp element
 
Jsp advance part i
Jsp advance part iJsp advance part i
Jsp advance part i
 
Jsp
JspJsp
Jsp
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
 
JAVA SERVER PAGES
JAVA SERVER PAGESJAVA SERVER PAGES
JAVA SERVER PAGES
 
Jsp chapter 1
Jsp chapter 1Jsp chapter 1
Jsp chapter 1
 
Servlet and jsp interview questions
Servlet and jsp interview questionsServlet and jsp interview questions
Servlet and jsp interview questions
 
Implementing jsp tag extensions
Implementing jsp tag extensionsImplementing jsp tag extensions
Implementing jsp tag extensions
 
Jsp
JspJsp
Jsp
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
 
Implicit object.pptx
Implicit object.pptxImplicit object.pptx
Implicit object.pptx
 
Jsp quick reference card
Jsp quick reference cardJsp quick reference card
Jsp quick reference card
 
Lap trinh web [Slide jsp]
Lap trinh web [Slide jsp]Lap trinh web [Slide jsp]
Lap trinh web [Slide jsp]
 
Jsp presentation
Jsp presentationJsp presentation
Jsp presentation
 
JSP - Java Server Page
JSP - Java Server PageJSP - Java Server Page
JSP - Java Server Page
 

Viewers also liked (7)

jsp tutorial
jsp tutorialjsp tutorial
jsp tutorial
 
Jsp lecture
Jsp lectureJsp lecture
Jsp lecture
 
Jsp Tutorial
Jsp TutorialJsp Tutorial
Jsp Tutorial
 
29 Jsp
29 Jsp29 Jsp
29 Jsp
 
Gcm tutorial
Gcm tutorialGcm tutorial
Gcm tutorial
 
Jsp Introduction Tutorial
Jsp Introduction TutorialJsp Introduction Tutorial
Jsp Introduction Tutorial
 
Project report on ONLINE REAL ESTATE BUSINESS
Project report on ONLINE REAL ESTATE BUSINESSProject report on ONLINE REAL ESTATE BUSINESS
Project report on ONLINE REAL ESTATE BUSINESS
 

Similar to JSP Tutorial: Learn JSP in 40 Steps

Similar to JSP Tutorial: Learn JSP in 40 Steps (20)

JSP- JAVA SERVER PAGES
JSP- JAVA SERVER PAGESJSP- JAVA SERVER PAGES
JSP- JAVA SERVER PAGES
 
Jsp in Servlet by Rj
Jsp in Servlet by RjJsp in Servlet by Rj
Jsp in Servlet by Rj
 
Introduction to JSP.pptx
Introduction to JSP.pptxIntroduction to JSP.pptx
Introduction to JSP.pptx
 
JSP.pptx
JSP.pptxJSP.pptx
JSP.pptx
 
JavaScript, often abbreviated as JS, is a programming language and core techn...
JavaScript, often abbreviated as JS, is a programming language and core techn...JavaScript, often abbreviated as JS, is a programming language and core techn...
JavaScript, often abbreviated as JS, is a programming language and core techn...
 
Unit 4 1 web technology uptu
Unit 4 1 web technology uptuUnit 4 1 web technology uptu
Unit 4 1 web technology uptu
 
Jsp
JspJsp
Jsp
 
Java server pages
Java server pagesJava server pages
Java server pages
 
Jsp
JspJsp
Jsp
 
Jsp
JspJsp
Jsp
 
Java Server Pages(jsp)
Java Server Pages(jsp)Java Server Pages(jsp)
Java Server Pages(jsp)
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
 
JSP AND XML USING JAVA WITH GET AND POST METHODS
JSP AND XML USING JAVA WITH GET AND POST METHODSJSP AND XML USING JAVA WITH GET AND POST METHODS
JSP AND XML USING JAVA WITH GET AND POST METHODS
 
J2EE - JSP-Servlet- Container - Components
J2EE - JSP-Servlet- Container - ComponentsJ2EE - JSP-Servlet- Container - Components
J2EE - JSP-Servlet- Container - Components
 
JSP overview
JSP overviewJSP overview
JSP overview
 
Jsp tutorial
Jsp tutorialJsp tutorial
Jsp tutorial
 
JSP
JSPJSP
JSP
 
Jsp interview questions by java training center
Jsp interview questions by java training centerJsp interview questions by java training center
Jsp interview questions by java training center
 
Jsp1
Jsp1Jsp1
Jsp1
 
JSP Technology I
JSP Technology IJSP Technology I
JSP Technology I
 

Recently uploaded

Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptshraddhaparab530
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
Food processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsFood processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsManeerUddin
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 

Recently uploaded (20)

Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.ppt
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
Food processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsFood processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture hons
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 

JSP Tutorial: Learn JSP in 40 Steps

  • 2. JSP  A JSP page is a text-based document that contains two types of text: static template data, which can be expressed in any text-based format, such as HTML, SVG, WML, and XML; and JSP elements, which construct dynamic content.  A JSP page has extension .jsp example hello.jsp
  • 3. The Life Cycle of a JSP Page  A JSP page services requests as a servlet. Thus, the life cycle and many of the capabilities of JSP pages (in particular the dynamic aspects) are determined by Java Servlet technology  When a request is mapped to a JSP page, it is handled by a special servlet that first checks whether the JSP page's servlet is older than the JSP page. If it is, it translates the JSP page into a servlet class and compiles the class. During development, one of the advantages of JSP pages over servlets is that the build process is performed automatically.
  • 4. Translation and Compilation  During the translation phase, each type of data in a JSP page is treated differently. Template data is transformed into code that will emit the data into the stream that returns data to the client. JSP elements are treated as follows:  Directives are used to control how the Web container translates and executes the JSP page.  Scripting elements are inserted into the JSP page's servlet class.  Elements of the form <jsp:XXX ... /> are converted into method calls to JavaBeans components or invocations of the Java Servlet API.  Both the translation and compilation phases can yield errors that are only observed when the page is requested for the first time. If an error occurs while the page is being translated (for example, if the translator encounters a malformed JSP element), the server will return a ParseException, and the servlet class source file will be empty or incomplete. The last incomplete line will give a pointer to the incorrect JSP element.
  • 5. Servlet Life Cycle  The life cycle of a servlet is controlled by the container in which the servlet has been deployed. When a request is mapped to a servlet, the container performs the following steps.  If an instance of the servlet does not exist, the Web container – Loads the servlet class. – Creates an instance of the servlet class. – Initializes the servlet instance by calling the init method.  Invokes the service method, passing a request and response object.  If the container needs to remove the servlet, it finalizes the servlet by calling the servlet's destroy method.
  • 6. JSP Life Cycle  Once the page has been translated and compiled, the JSP page's servlet for the most part follows the servlet life cycle described in the Servlet Life Cycle:  If an instance of the JSP page's servlet does not exist, the container: – Loads the JSP page's servlet class – Instantiates an instance of the servlet class – Initializes the servlet instance by calling the jspInit method  Invokes the _jspService method, passing a request and response object.  If the container needs to remove the JSP page's servlet, it calls the jspDestroy method.
  • 7. Execution  You can control various JSP page execution parameters using by page directives.
  • 8. Creating Static Content  You create static content in a JSP page by simply writing it as if you were creating a page that consisted only of that content. Static content can be expressed in any text-based format, such as HTML, WML, and XML.  The default format is HTML.  If you want to use a format other than HTML, you include a page directive with the contentType attribute set to the format type at the beginning of your JSP page. For example, if you want a page to contain data expressed in the wireless markup language (WML), you need to include the following directive: <%@ page contentType="text/vnd.wap.wml"%>
  • 9. Creating Dynamic Content  You can create dynamic content by accessing Java programming language objects from within scripting elements. Using Objects within JSP Pages You can access a variety of objects, including enterprise beans and JavaBeans components, within a JSP page. JSP technology automatically makes some objects available, and you can also create and access application-specific objects.
  • 10. Implicit Objects application The context for the JSP page's servlet and any Web components contained in the same application. config Initialization information for the JSP page's servlet. exception Accessible only from an error page. out The output stream. page The instance of the JSP page's servlet processing the current request. Not typically used by JSP page authors. pageContext The context for the JSP page. Provides a single API to manage the various scoped attributes. request The request triggering the execution of the JSP page. response The response to be returned to the client. Not typically used by JSP page authors. session The session object for the client.
  • 11. Application-Specific Objects  When possible, application behavior should be encapsulated in objects so that page designers can focus on presentation issues. Objects can be created by developers who are proficient in the Java programming language and in accessing databases and other services. There are four ways to create and use objects within a JSP page: 1. Instance and class variables of the JSP page's servlet class are created in declarations and accessed in scriptlets and expressions. 2. Local variables of the JSP page's servlet class are created and used in scriptlets and expressions. 3. Attributes of scope objects are created and used in scriptlets and expressions. 4. JavaBeans components can be created and accessed using streamlined JSP elements. You can also create a JavaBeans component in a declaration or scriptlet and invoke the methods of a JavaBeans component in a scriptlet or expression.
  • 12. Shared Objects  The conditions affecting concurrent access to shared objects apply to objects accessed from JSP pages that run as multithreaded servlets. You can indicate how a Web container should dispatch multiple client requests with the following page directive: <%@ page isThreadSafe="true|false" %> When isThreadSafe is set to true, the Web container may choose to dispatch multiple concurrent client requests to the JSP page. This is the default setting. If using true, you must ensure that you properly synchronize access to any shared objects defined at the page level. This includes objects created within declarations, JavaBeans components with page scope, and attributes of the page scope object. If isThreadSafe is set to false, requests are dispatched one at a time, in the order they were received, and access to page-level objects does not have to be controlled. However, you still must ensure that access to attributes of the application or session scope objects and to JavaBeans components with application or session scope is properly synchronized.
  • 13. JSP Scripting Elements  JSP scripting elements are used to create and access objects, define methods, and manage the flow of control. Since one of the goals of JSP technology is to separate static template data from the code needed to dynamically generate content, very sparing use of JSP scripting is recommended. Much of the work that requires the use of scripts can be eliminated by using custom tags. JSP technology allows a container to support any scripting language that can call Java objects. If you wish to use a scripting language other than the default, java, you must specify it in a page directive at the beginning of a JSP page: <%@ page language="scripting language" %> Since scripting elements are converted to programming language statements in the JSP page's servlet class, you must import any classes and packages used by a JSP page. If the page language is java, you import a class or package with the page directive: <%@ page import="packagename.*, fully_qualified_classname" %> For example, the bookstore example page showcart.jsp imports the classes needed to implement the shopping cart with the following directive: <%@ page import="java.util.*, cart.*" %>
  • 14. Declarations A JSP declaration is used to declare variables and methods in a page's scripting language. The syntax for a declaration is as follows: <%! scripting language declaration %> When the scripting language is the Java programming language, variables and methods in JSP declarations become declarations in the JSP page's servlet class. The bookstore example page initdestroy.jsp defines an instance variable named bookDBEJB and the initialization and finalization methods jspInit and jspDestroy discussed earlier in a declaration: <%! private BookDBEJB bookDBEJB; public void jspInit() { ... } public void jspDestroy() { ... } %>
  • 15. Scriptlets  A JSP scriptlet is used to contain any code fragment that is valid for the scripting language used in a page. The syntax for a scriptlet is as follows: <% scripting language statements %> Example <% Iterator i = cart.getItems().iterator(); while (i.hasNext()) { ShoppingCartItem item = (ShoppingCartItem)i.next(); BookDetails bd = (BookDetails)item.getItem(); %> <tr> <td align="right" bgcolor="#ffffff"> <%=item.getQuantity()%> </td> <td bgcolor="#ffffaa"> <strong> <a href=" <%=request.getContextPath()%>/bookdetails?bookId= <%=bd.getBookId()%>"> <%=bd.getTitle()%> </a> </strong> </td> ………. <% // End of while } %>
  • 16. Expressions  A JSP expression is used to insert the value of a scripting language expression, converted into a string, into the data stream returned to the client. When the scripting language is the Java programming language, an expression is transformed into a statement that converts the value of the expression into a String object and inserts it into the implicit out object.  The syntax for an expression is as follows: <%= scripting language expression %> Note that a semicolon is not allowed within a JSP expression, even if the same expression has a semicolon when you use it within a scriptlet.
  • 17. Including Content in a JSP Page  There are two mechanisms for including another Web resource in a JSP page. 1. include directive 2. jsp:include element.
  • 18. The include directive  The include directive is processed when the JSP page is translated into a servlet class. The effect of the directive is to insert the text contained in another file--either static content or another JSP page--in the including JSP page. You would probably use the include directive to include banner content, copyright information, or any chunk of content that you might want to reuse in another page. The syntax for the include directive is as follows: <%@ include file="filename" %> For example, all the bookstore application pages include the file banner.jsp containing the banner content with the following directive: <%@ include file="banner.jsp" %>
  • 19. The jsp:include element  The jsp:include element is processed when a JSP page is executed. The include action allows you to include either a static or dynamic resource in a JSP file. The results of including static and dynamic resources are quite different. If the resource is static, its content is inserted into the calling JSP file. If the resource is dynamic, the request is sent to the included resource, the included page is executed, and then the result is included in the response from the calling JSP page. The syntax for the jsp:include element is as follows: <jsp:include page="includedPage" /> The example to includes the page that generates the display of the localized date with the following statement: <jsp:include page="date.jsp"/>
  • 20. Transferring Control to Another Web Component  The mechanism for transferring control to another Web component from a JSP page uses the functionality provided by the Java Servlet API. You access this functionality from a JSP page with the jsp:forward element: <jsp:forward page="/main.jsp" />