jsp_tut

Upload: sajjala-bala-krishna

Post on 10-Apr-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/8/2019 JSP_tut

    1/9

    JSP

    What is JavaServer Pages?

    JavaServer Pages (JSP) is a server-side programming technology that enables the creation of

    dynamic, platform-independent method for building Web-based applications.

    JSP have access to the entire family of Java APIs, including the JDBC API to access enterprise

    databases.

    JavaServer Pages (JSP) is a technology for developing web pages that support dynamic contentwhich helps developers insert java code in HTML pages by making use of special JSP tags, most

    of which start with .

    Using JSP, you can collect input from users through web page forms, present records from a

    database or another source, and create web pages dynamically.

    JSP tags can be used for a variety of purposes, such as retrieving information from a database or

    registering user preferences, accessing JavaBeans components, passing control between pages

    and sharing information between requests, pages etc.

    Why Use JSP?

    CGI means "Common Gateway Interface"

    JavaServer Pages often serve the same purpose as programs implemented using the Common

    Gateway Interface (CGI). But JSP offer several advantages in comparison with the CGI.

    Performance is significantly better because JSP allows embedding Dynamic Elements inHTML Pages itself instead of having a separate CGI files.

    JSP are always compiled before it's processed by the server unlike CGI/Perl which

    requires the server to load an interpreter and the target script each time the page isrequested.

    JavaServer Pages are built on top of the Java Servlets API, so like Servlets, JSP also has

    access to all the powerful Enterprise Java APIs, including JDBC, JNDI, EJB, JAXP etc.

    JSP pages can be used in combination with servlets that handle the business logic, the

    model supported by Java servlet template engines.

    Finally, JSP is an integral part of J2EE, a complete platform for enterprise class applications.This means that JSP can play a part in the simplest applications to the most complex anddemanding.

    Advantages of JSP:

    Following is the list of other advantages of using JSP over other technologies:

  • 8/8/2019 JSP_tut

    2/9

    vs. Active Server Pages (ASP): The advantages of JSP are twofold. First, the dynamic

    part is written in Java, not Visual Basic or other MS specific language, so it is more

    powerful and easier to use. Second, it is portable to other operating systems and non-Microsoft Web servers.

    vs. Pure Servlets: It is more convenient to write (and to modify!) regular HTML than to

    have plenty of println statements that generate the HTML. vs. Server-Side Includes (SSI): SSI is really only intended for simple inclusions, not for

    "real" programs that use form data, make database connections, and the like.

    vs. JavaScript: JavaScript can generate HTML dynamically on the client but can hardlyinteract with the web server to perform complex tasks like database access and image

    processing etc.

    vs. Static HTML: Regular HTML, of course, cannot contain dynamic information.

    JSP - Architecture

    The web server needs a JSP engine i.e. Container to process JSP pages. The JSP container is

    responsible for intercepting requests for JSP pages. A JSP container works with the Web serverto provide the runtime environment and other services a JSP needs. It knows how to understand

    the special elements that are part of JSPs.

    Following diagram shows the position of JSP container and JSP files in a Web Application.

    JSP Processing:

    The following steps explain how the web server creates the web page using JSP:

    As with a normal page, your browser sends an HTTP request to the web server.

    The web server recognizes that the HTTP request is for a JSP page and forwards it to aJSP engine. This is done by using the URL or JSP page which ends with .jsp instead of

    .html.

    The JSP engine loads the JSP page from disk and converts it into a servlet content. Thisconversion is very simple in which all template text is converted to println( ) statements

  • 8/8/2019 JSP_tut

    3/9

  • 8/8/2019 JSP_tut

    4/9

    A JSP life cycle can be defined as the entire process from its creation till the destruction which is

    similar to a servlet life cycle with an additional step which is required to compile a JSP into

    servlet.

    The following are the paths followed by a JSP

    Compilation

    Initialization

    Execution

    Cleanup

    The three major phases of JSP life cycle are very similar to Servlet Life Cycle and they are as

    follows:

    (1) JSP Compilation:

    When a browser asks for a JSP, the JSP engine first checks to see whether it needs to compile the

    page. If the page has never been compiled, or if the JSP has been modified since it was last

    compiled, the JSP engine compiles the page.

    The compilation process involves three steps:

    1. Parsing the JSP.2. Turning the JSP into a servlet.

    3. Compiling the servlet.

    (2) JSP Initialization:

  • 8/8/2019 JSP_tut

    5/9

  • 8/8/2019 JSP_tut

    6/9

    // Your cleanup code goes here.

    }

    The Scriptlet:

    A scriptlet can contain any number of JAVA language statements, variable or method

    declarations, or expressions that are valid in the page scripting language.

    Following is the syntax of Scriptlet:

    You can write XML equivalent of the above syntax as follows:

    code fragment

    Following is the syntax of JSP comments:

    In JSP tags can be devided into 4 different types. These are:

    1. Directives

    In the directives we can import packages, define error handling pages or the session

    information of the JSP page.

    2. Declarations

    This tag is used for defining the functions and variables to be used in the JSP.

    3. Scriplets

    In this tag we can insert any amount of valid java code and these codes are placed in_jspService method by the JSP engine.

    4. Expressions

    We can use this tag to output any data on the generated page. These data areautomatically converted to string and printed on the output stream.

    There are four types of JSP tags, which are important and often required.

  • 8/8/2019 JSP_tut

    7/9

    1. Directives

    These types of tags are used primarily to import packages.

    Altenatively you can also use these tags to define error handling

    pages and for session information of JSP page.

    1. Code: JSP

    2. Code: JSP

    3. Code: JSP

    4. Code: JSP

    5. Code: JSP

    2. Declarations

    JSP declarations starts with ''. In this you can make

    declarions such as int i = 1, double pi = 3.1415 etc. If needed, you can also writeJava code inside declarations.

    Example 1

  • 8/8/2019 JSP_tut

    8/9

    3. Scriptlets

    JSP Scriptlets starts with ''. This is where the important Java

    code for JSP page is written.

    Example

    Code: JSP

    request, response, session and out are the variables available in scriptlets.

    4. Expressions

    JSP expressions starts with ''. If you want to show some

    value, you need to put it in between these tags.

    Example:

    Code: JSP

    Area of circle is

    Area of circle is 147.0

    Area of circle is 147.0

  • 8/8/2019 JSP_tut

    9/9

    Difference between JSP and servlet?

    JSP is used mainly for presentation only. A JSP can only be HttpServlet that means

    the only supported protocol in JSP is HTTP. But a servlet can support any protocol

    like HTTP, FTP, SMTP etc.

    Servlets-

    (1)Bits of HTML embedded in java code.(2) Suitable for coding business layer of enterprise

    application.(3)Created and maintained by Java programmers

    JSP-

    (1) Bits of java code embedded in HTML.

    (2)Suitable for coding presentation layer of enterprise application(3)Created and maintained by Web designers.

    Servlets are pure java codes,here we can't used HTML. But in JSP we can use

    HTML,so we can modify and also updated the data.

    Another difference In Servlet clientside validation is possible But in jsp clientside

    validation is not possible.