code jsp tutorial

Upload: sbalan-balan

Post on 03-Apr-2018

244 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/29/2019 code JSP Tutorial

    1/20

    JSP Tutorial

  • 7/29/2019 code JSP Tutorial

    2/20

    JSP

    A JSP pageis a text-based document that containstwo 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

    http://www.w3.org/MarkUphttp://www.w3.org/TR/SVGhttp://www.oasis-open.org/cover/wap-wml.htmlhttp://www.w3.org/TR/REC-xmlhttp://www.w3.org/TR/REC-xmlhttp://www.oasis-open.org/cover/wap-wml.htmlhttp://www.w3.org/TR/SVGhttp://www.w3.org/MarkUp
  • 7/29/2019 code JSP Tutorial

    3/20

    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.

  • 7/29/2019 code JSP Tutorial

    4/20

    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 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.

  • 7/29/2019 code JSP Tutorial

    5/20

    Servlet Life Cycle

    The life cycle of a servlet is controlled by the container in whichthe 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 responseobject.

    If the container needs to remove the servlet, it finalizes theservlet by calling the servlet's destroy method.

  • 7/29/2019 code JSP Tutorial

    6/20

    JSP Life Cycle

    Once the page has been translated and compiled, the JSPpage'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 andresponse object.

    If the container needs to remove the JSP page's servlet, it callsthe jspDestroy method.

    http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/Servlets4.htmlhttp://java.sun.com/j2ee/tutorial/1_3-fcs/doc/Servlets4.html
  • 7/29/2019 code JSP Tutorial

    7/20

    Execution

    You can control various JSP page execution

    parameters using by page directives.

  • 7/29/2019 code JSP Tutorial

    8/20

    Creating Static Content

    You create static content in a JSP page by simply writing it as if you were creating a pagethat consisted only of that content. Static content can be expressed in any text-basedformat, 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 thecontentType attribute set to the format type at the beginning of your JSP page. Forexample, if you want a page to contain data expressed in the wireless markup language(WML), you need to include the following directive:

  • 7/29/2019 code JSP Tutorial

    9/20

    Creating Dynamic Content

    You can create dynamic content byaccessing Java programming languageobjects from within scripting elements.

    Using Objects within JSP PagesYou can access a variety of objects, including enterprise beans and JavaBeans

    components, within a JSP page. JSP technology automatically makes someobjects available, and you can also create and access application-specific

    objects.

  • 7/29/2019 code JSP Tutorial

    10/20

    Implicit Objects

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

    configInitialization information for the JSP page's servlet.

    exceptionAccessible only from an error page.

    outThe output stream.

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

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

    requestThe request triggering the execution of the JSP page.

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

    sessionThe session object for the client.

  • 7/29/2019 code JSP Tutorial

    11/20

    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 areproficient 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 declarationsand accessed in scriptlets and

    expressions.

    2. Local variables of the JSP page's servlet class are created and used in scriptletsand expressions.

    3. Attributes of scope objects are created and used in scriptletsand 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.

  • 7/29/2019 code JSP Tutorial

    12/20

    Shared Objects

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

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

    If isThreadSafe is set to false, requests are dispatched one at a time, in the order theywere 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 objectsand to JavaBeans components with application or session scope is properly synchronized.

  • 7/29/2019 code JSP Tutorial

    13/20

    JSP Scripting Elements

    JSP scripting elements are used to create and access objects, define methods, and manage the flow ofcontrol. Since one of the goals of JSP technology is to separate static template data from the code neededto dynamically generate content, very sparing use of JSP scripting is recommended. Much of the work thatrequires 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 wishto use a scripting language other than the default, java, you must specify it in a page directive at thebeginning of a JSP page:

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

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

    http://java.sun.com/j2ee/tutorial/1_3-fcs/examples/src/web/bookstore2/showcart.txthttp://java.sun.com/j2ee/tutorial/1_3-fcs/examples/src/web/bookstore2/showcart.txt
  • 7/29/2019 code JSP Tutorial

    14/20

    Declarations

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

    The syntax for a declaration is as follows:

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

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

    http://java.sun.com/j2ee/tutorial/1_3-fcs/examples/src/web/bookstore2/initdestroy.txthttp://java.sun.com/j2ee/tutorial/1_3-fcs/examples/src/web/bookstore2/initdestroy.txt
  • 7/29/2019 code JSP Tutorial

    15/20

    Scriptlets

    A JSP scriptletis used to contain any code fragment that is valid for the scripting language used in a page. Thesyntax for a scriptlet is as follows:Example

    .

  • 7/29/2019 code JSP Tutorial

    16/20

    Expressions

    A JSP expressionis used to insert the value of a scripting language expression,converted into a string, into the data stream returned to the client. When thescripting language is the Java programming language, an expression istransformed 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:

    Note that a semicolon is not allowed within a JSP expression, even if the sameexpression has a semicolon when you use it within a scriptlet.

  • 7/29/2019 code JSP Tutorial

    17/20

    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.

  • 7/29/2019 code JSP Tutorial

    18/20

    The include directive

    The include directive is processed when the JSP page is translatedinto aservlet class. The effect of the directive is to insert the text contained in anotherfile--either static content or another JSP page--in the including JSP page. Youwould probably use the include directive to include banner content, copyright

    information, or any chunk of content that you might want to reuse in anotherpage.

    The syntax for the include directive is as follows:

    Forexample, all the bookstore application pages include the file banner.jspcontaining the banner content with the following directive:

    http://java.sun.com/j2ee/tutorial/1_3-fcs/examples/src/web/bookstore2/banner.txthttp://java.sun.com/j2ee/tutorial/1_3-fcs/examples/src/web/bookstore2/banner.txt
  • 7/29/2019 code JSP Tutorial

    19/20

    The jsp:include element

    The jsp:include element is processed when a JSP page is executed. The include actionallows you to include either a static or dynamic resource in a JSP file. The results ofincluding static and dynamic resources are quite different. If the resource is static, itscontent 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 theresponse from the calling JSP page.

    The syntax for the jsp:include element is as follows:

    The example to includes the page that generates the display of the localized date with thefollowing statement:

  • 7/29/2019 code JSP Tutorial

    20/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: