SlideShare ist ein Scribd-Unternehmen logo
1 von 17
Downloaden Sie, um offline zu lesen
Java Server Pages Technology
                        (Part-1)

                          Biswabrata Banerjee
What is this JSP anyway !?
 JavaServer Pages (JSP) is a java technology that provides a simplified,
fast way to create dynamic web content. JSP technology enables rapid
development of web-based applications that are server- and platform-
independent.
     It helps software developers serve dynamically generated web pages
based on HTML, XML, or other document types. Released in 1999 as Sun's
answer to ASP and PHP, JSP Specification was designed to address the
perception that the Java programming environment didn't provide
developers with enough support for the Web.

 JSP , like other Java APIs, is a specification by Sun for vendors to
implement. The JSP specification builds on the functionality provided by the
servlet specification

                                                                Biswabrata Banerjee
• A sample JSP page (code ref-1.1):-

    Today.jsp

                <%@ page import=“java.util.Date”%>

                <html>
                          <body>
                                    The Current Date and Time is:
                                    <%=(new Date()).toString()) %>
                          </body>
                </html>




                                                                     Biswabrata Banerjee
JSP can dynamically invoke other JSPs
                                         JSP
                                      <JSP tag>
                                     <JSP code>


                      JSP
                   <JSP tag>
                  <JSP code>
                                         JSP
Client                  Translated    <JSP tag>
                                     <JSP code>


                    servlet




                                          Biswabrata Banerjee
Biswabrata Banerjee
JSP architecture




                   Biswabrata Banerjee
• No central component

• Has to embed Big chunk of Java-code to implement
  business logic

• Designers aren’t comfortable with server-side
  programmig.

• Reusability of application components are is not
  promoted.

                                                  Biswabrata Banerjee
JSP architecture



                3
       2/6
                         5
             4
             uses bean




                             Biswabrata Banerjee
MVC Architecture Explained …
       In MVC model, the request is sent by the browser to the controller or the servlet. This
request is instantiated by the servlet as a Java Bean. The main aspect is JSP are compiled
into servlets at the back end and the front end tasks are not interrupted. The servlet engine
takes up the responsibility of compiling JSP Servlet and producing the final JSP servlet class
for usage. The front end presentation modules are handled by JSP for viewing and the
manipulation of data is handled by Java Bean(model) and passed back to JSP when
needed. The Presentation part of the MVC Model has no processing logic. It performs the
task of extracting beans or objects that may have been initially created by the controller. It
also extracts the dynamic content within for insertion within its static templates.
       The Application Manager in the MVC Model is the Controller that processes HTTP
requests. They are not responsible for presentation tasks. That can be either servlets or JSP.
They take the task of managing the application state, security, and presentation uniformity
and thus, have a single point of entry.
JSP Life-cycle
Phase Name                                              Description
Page Translation                                        A Servlet (java) file is created
Page compilation                                        The java file compiled.
Load class                                              Compiled class loaded
Create Instance                                         An instance of Servlet is created
Call jspInit()                                          This method is called before other method to allow
Syn : public void jspInit() { //.. }                    initialization.

Call _jspService()                                      This method is called for each request
Syn : public void _jspService(HttpServletRequest req,
HttpServletResponse res )
throws ServletException,IOException { //..}




Call jspDestory()                                       This method is called when Servlet decided to take the
Syn :    public void jspDestroy() { //.. }              Servlet out of service
// not exact code ! Depends on vendor specification.
import java.util.Date;
    public final class Todat_jsp implements javax.servlet.Servlet,
    javax.servlet.jsp.HttpJspPage {

          public void jspInit(){
                     // initialization parameters
          }
          public void _jspService( javax.servlet.http.HttpServletRequest request,
                                  javax.servlet.http.HttpServletResponse response )
          throws javax.servlet.ServletException , java.io.IOException
           {
                     javax.servlet.ServletConfig config = …; // Get the servlet config
                     out.wite( (new Date().toString() ) ;
          }
          public void jspDestroy(){
                     // …
          }
}
JSP Nuts & Bolts

                                page


 Directives                       include                                 Comments

                                                                      Expression
                                taglib                    Scriptlet
                                            Declaration
Scripting Elements




Actions

                                getProperty               include                  plugin
              useBean
                        setProperty             param                   forward
Attribute      Description                                                    Default value
language       For defining scripting language                                java
extends        To indicate which clas the generated servlet will              Omitted
               extend
Import         Reason is Same as import in a java class                       Omitted
session        specify whether a page participates in a session               True
buffer         models Buffer for the output stream of the client              Implementation
                                                                              dependent;at least 8kb
autoFlush      To empty the buffer if full                                    True
isThreadSafe   Defines the level of thread safety                             true
Info           Defines an informative string [   servlet.getServletInfo() ]   Omitted
errorPage      To invoke another JSP page on error                            Omitted
isErrorPage    If this is an error page of some other JSP page                false
contentType    The character encoding & MIME (Multipurpose                    MIME type is text/html
               Internet Mail Extensions) type.                                CHARSET is ISO-8859-1
An example:
 <%@ page
      [ language="java" ]
      [ extends="package.class" ]
      [ import="{package.class | package.*}, ..." ]
      [ session="true | false" ]
      [ buffer="none | 8kb | sizekb" ]
      [ isThreadSafe="true | false" ]
      [ info="text" ]
      [ errorPage="relativeURL" ]
      [ contentType="mimeType [ ;charset=characterSet ]" | "text/html ;
                                      charset=ISO-8859-1" ]
      [ isErrorPage="true | false" ]
 %>
 <html>
             < !- - do anything here as you Like -->
 </html>
The include directive
     Include Directive Includes a file of text or code when the JSP
page is translated i.e., at the time of compilation.
JSP Syntax
<%@ include file="relativeURL" %>
Examples
     include.jsp:
     <html>
        <head><title>An Include Test</title></head>
        <body bgcolor="white">
               <font color="blue">
                         The current date and time are
               <%@ include file="date.jsp" %>
               </font>
        </body>
     </html>
The include directive                                                       … Cont’d

date.jsp:
<%@ page import="java.util.*" %>
<%= (new java.util.Date() ).toLocaleString() %>
Displays in the page:
The current date and time are
Aug 30, 1999 2:38:40

Description
   The <%@ include %> directive inserts a file of text or code in a JSP file at
   translation time, when the JSP file is compiled. When you use the <%@ include %>
   directive, the include process is static. A static include means that the text of the
   included file is added to the JSP file. The included file can be a JSP file, HTML file,
   or text file. If the included file is a JSP file, its JSP elements are parsed and their
   results included (along with any other text) in the JSP file.
The taglib directive
        Taglib Directive defines a tag library and prefix for the custom
   tags used in the JSP page.
JSP Syntax
<%@ taglib uri="URIToTagLibrary" prefix="tagPrefix" %>
Examples
<%@ taglib uri="http://www.jspcentral.com/tags" prefix="public" %>
   <public:loop>

  </public:loop>
Description

The <%@ taglib %> directive declares that the JSP file uses custom
  tags, names the tag library that defines them, and specifies their
  tag prefix.

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Java Web Programming [7/9] : Struts2 Basics
Java Web Programming [7/9] : Struts2 BasicsJava Web Programming [7/9] : Struts2 Basics
Java Web Programming [7/9] : Struts2 Basics
 
JSP
JSPJSP
JSP
 
Java Web Programming [8/9] : JSF and AJAX
Java Web Programming [8/9] : JSF and AJAXJava Web Programming [8/9] : JSF and AJAX
Java Web Programming [8/9] : JSF and AJAX
 
Java Web Programming [5/9] : EL, JSTL and Custom Tags
Java Web Programming [5/9] : EL, JSTL and Custom TagsJava Web Programming [5/9] : EL, JSTL and Custom Tags
Java Web Programming [5/9] : EL, JSTL and Custom Tags
 
Java server pages
Java server pagesJava server pages
Java server pages
 
Introduction to JSP
Introduction to JSPIntroduction to JSP
Introduction to JSP
 
Jsp Introduction Tutorial
Jsp Introduction TutorialJsp Introduction Tutorial
Jsp Introduction Tutorial
 
Jsp sasidhar
Jsp sasidharJsp sasidhar
Jsp sasidhar
 
Jsp elements
Jsp elementsJsp elements
Jsp elements
 
Implicit objects advance Java
Implicit objects advance JavaImplicit objects advance Java
Implicit objects advance Java
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
 
Jsp
JspJsp
Jsp
 
Java Web Programming [2/9] : Servlet Basic
Java Web Programming [2/9] : Servlet BasicJava Web Programming [2/9] : Servlet Basic
Java Web Programming [2/9] : Servlet Basic
 
Implicit object.pptx
Implicit object.pptxImplicit object.pptx
Implicit object.pptx
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
 
Jsp
JspJsp
Jsp
 
Java Web Programming [3/9] : Servlet Advanced
Java Web Programming [3/9] : Servlet AdvancedJava Web Programming [3/9] : Servlet Advanced
Java Web Programming [3/9] : Servlet Advanced
 
Jsp
JspJsp
Jsp
 
Jsp chapter 1
Jsp chapter 1Jsp chapter 1
Jsp chapter 1
 
JSP Technology I
JSP Technology IJSP Technology I
JSP Technology I
 

Ähnlich wie J2EE jsp_01

Ähnlich wie J2EE jsp_01 (20)

Introduction to JSP.pptx
Introduction to JSP.pptxIntroduction to JSP.pptx
Introduction to JSP.pptx
 
Jsp in Servlet by Rj
Jsp in Servlet by RjJsp in Servlet by Rj
Jsp in Servlet by Rj
 
JSP- JAVA SERVER PAGES
JSP- JAVA SERVER PAGESJSP- JAVA SERVER PAGES
JSP- JAVA SERVER PAGES
 
J2EE - JSP-Servlet- Container - Components
J2EE - JSP-Servlet- Container - ComponentsJ2EE - JSP-Servlet- Container - Components
J2EE - JSP-Servlet- Container - Components
 
Introduction to JSP
Introduction to JSPIntroduction to JSP
Introduction to JSP
 
JSP.pptx
JSP.pptxJSP.pptx
JSP.pptx
 
Introduction to JSP pages
Introduction to JSP pagesIntroduction to JSP pages
Introduction to JSP pages
 
JSP overview
JSP overviewJSP overview
JSP overview
 
JAVA SERVER PAGES
JAVA SERVER PAGESJAVA SERVER PAGES
JAVA SERVER PAGES
 
Jsp (java server page)
Jsp (java server page)Jsp (java server page)
Jsp (java server page)
 
JSP Components and Directives.pdf
JSP Components and Directives.pdfJSP Components and Directives.pdf
JSP Components and Directives.pdf
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
 
Atul & shubha goswami jsp
Atul & shubha goswami jspAtul & shubha goswami jsp
Atul & shubha goswami jsp
 
Jsp servlets
Jsp servletsJsp servlets
Jsp servlets
 
Jsp and jstl
Jsp and jstlJsp and jstl
Jsp and jstl
 
Unit 4 web technology uptu
Unit 4 web technology uptuUnit 4 web technology uptu
Unit 4 web technology uptu
 
Unit 4 1 web technology uptu
Unit 4 1 web technology uptuUnit 4 1 web technology uptu
Unit 4 1 web technology uptu
 
Jsp element
Jsp elementJsp element
Jsp element
 
Jsp1
Jsp1Jsp1
Jsp1
 
Java Server Pages(jsp)
Java Server Pages(jsp)Java Server Pages(jsp)
Java Server Pages(jsp)
 

J2EE jsp_01

  • 1. Java Server Pages Technology (Part-1) Biswabrata Banerjee
  • 2. What is this JSP anyway !?  JavaServer Pages (JSP) is a java technology that provides a simplified, fast way to create dynamic web content. JSP technology enables rapid development of web-based applications that are server- and platform- independent. It helps software developers serve dynamically generated web pages based on HTML, XML, or other document types. Released in 1999 as Sun's answer to ASP and PHP, JSP Specification was designed to address the perception that the Java programming environment didn't provide developers with enough support for the Web.  JSP , like other Java APIs, is a specification by Sun for vendors to implement. The JSP specification builds on the functionality provided by the servlet specification Biswabrata Banerjee
  • 3. • A sample JSP page (code ref-1.1):- Today.jsp <%@ page import=“java.util.Date”%> <html> <body> The Current Date and Time is: <%=(new Date()).toString()) %> </body> </html> Biswabrata Banerjee
  • 4. JSP can dynamically invoke other JSPs JSP <JSP tag> <JSP code> JSP <JSP tag> <JSP code> JSP Client Translated <JSP tag> <JSP code> servlet Biswabrata Banerjee
  • 6. JSP architecture Biswabrata Banerjee
  • 7. • No central component • Has to embed Big chunk of Java-code to implement business logic • Designers aren’t comfortable with server-side programmig. • Reusability of application components are is not promoted. Biswabrata Banerjee
  • 8. JSP architecture 3 2/6 5 4 uses bean Biswabrata Banerjee
  • 9. MVC Architecture Explained … In MVC model, the request is sent by the browser to the controller or the servlet. This request is instantiated by the servlet as a Java Bean. The main aspect is JSP are compiled into servlets at the back end and the front end tasks are not interrupted. The servlet engine takes up the responsibility of compiling JSP Servlet and producing the final JSP servlet class for usage. The front end presentation modules are handled by JSP for viewing and the manipulation of data is handled by Java Bean(model) and passed back to JSP when needed. The Presentation part of the MVC Model has no processing logic. It performs the task of extracting beans or objects that may have been initially created by the controller. It also extracts the dynamic content within for insertion within its static templates. The Application Manager in the MVC Model is the Controller that processes HTTP requests. They are not responsible for presentation tasks. That can be either servlets or JSP. They take the task of managing the application state, security, and presentation uniformity and thus, have a single point of entry.
  • 10. JSP Life-cycle Phase Name Description Page Translation A Servlet (java) file is created Page compilation The java file compiled. Load class Compiled class loaded Create Instance An instance of Servlet is created Call jspInit() This method is called before other method to allow Syn : public void jspInit() { //.. } initialization. Call _jspService() This method is called for each request Syn : public void _jspService(HttpServletRequest req, HttpServletResponse res ) throws ServletException,IOException { //..} Call jspDestory() This method is called when Servlet decided to take the Syn : public void jspDestroy() { //.. } Servlet out of service
  • 11. // not exact code ! Depends on vendor specification. import java.util.Date; public final class Todat_jsp implements javax.servlet.Servlet, javax.servlet.jsp.HttpJspPage { public void jspInit(){ // initialization parameters } public void _jspService( javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response ) throws javax.servlet.ServletException , java.io.IOException { javax.servlet.ServletConfig config = …; // Get the servlet config out.wite( (new Date().toString() ) ; } public void jspDestroy(){ // … } }
  • 12. JSP Nuts & Bolts page Directives include Comments Expression taglib Scriptlet Declaration Scripting Elements Actions getProperty include plugin useBean setProperty param forward
  • 13. Attribute Description Default value language For defining scripting language java extends To indicate which clas the generated servlet will Omitted extend Import Reason is Same as import in a java class Omitted session specify whether a page participates in a session True buffer models Buffer for the output stream of the client Implementation dependent;at least 8kb autoFlush To empty the buffer if full True isThreadSafe Defines the level of thread safety true Info Defines an informative string [ servlet.getServletInfo() ] Omitted errorPage To invoke another JSP page on error Omitted isErrorPage If this is an error page of some other JSP page false contentType The character encoding & MIME (Multipurpose MIME type is text/html Internet Mail Extensions) type. CHARSET is ISO-8859-1
  • 14. An example: <%@ page [ language="java" ] [ extends="package.class" ] [ import="{package.class | package.*}, ..." ] [ session="true | false" ] [ buffer="none | 8kb | sizekb" ] [ isThreadSafe="true | false" ] [ info="text" ] [ errorPage="relativeURL" ] [ contentType="mimeType [ ;charset=characterSet ]" | "text/html ; charset=ISO-8859-1" ] [ isErrorPage="true | false" ] %> <html> < !- - do anything here as you Like --> </html>
  • 15. The include directive Include Directive Includes a file of text or code when the JSP page is translated i.e., at the time of compilation. JSP Syntax <%@ include file="relativeURL" %> Examples include.jsp: <html> <head><title>An Include Test</title></head> <body bgcolor="white"> <font color="blue"> The current date and time are <%@ include file="date.jsp" %> </font> </body> </html>
  • 16. The include directive … Cont’d date.jsp: <%@ page import="java.util.*" %> <%= (new java.util.Date() ).toLocaleString() %> Displays in the page: The current date and time are Aug 30, 1999 2:38:40 Description The <%@ include %> directive inserts a file of text or code in a JSP file at translation time, when the JSP file is compiled. When you use the <%@ include %> directive, the include process is static. A static include means that the text of the included file is added to the JSP file. The included file can be a JSP file, HTML file, or text file. If the included file is a JSP file, its JSP elements are parsed and their results included (along with any other text) in the JSP file.
  • 17. The taglib directive Taglib Directive defines a tag library and prefix for the custom tags used in the JSP page. JSP Syntax <%@ taglib uri="URIToTagLibrary" prefix="tagPrefix" %> Examples <%@ taglib uri="http://www.jspcentral.com/tags" prefix="public" %> <public:loop> </public:loop> Description The <%@ taglib %> directive declares that the JSP file uses custom tags, names the tag library that defines them, and specifies their tag prefix.