JSP A Brief Overview - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

JSP A Brief Overview

Description:

Java Server Pages (JSP) is a technology that lets you mix regular, static HTML ... http://java.sun.com/developer/onlineTraining/JSPIntro/contents.html ... – PowerPoint PPT presentation

Number of Views:270
Avg rating:3.0/5.0
Slides: 23
Provided by: philipp92
Category:
Tags: jsp | brief | java | overview

less

Transcript and Presenter's Notes

Title: JSP A Brief Overview


1
JSP - A Brief Overview
  • Philippa Rhodes
  • February 11, 2005

2
What are Java Servlets?
  • Servlets are Java technology's answer to CGI
    programming. They are programs that run on a Web
    server and build Web pages.
  • Building Web pages on the fly is useful for a
    number of reasons
  • The Web page is based on data submitted by the
    user.
  • The data changes frequently.
  • The Web page uses information from corporate
    databases or other such sources.

3
What is JSP?
  • Java Server Pages (JSP) is a technology that lets
    you mix regular, static HTML with
    dynamically-generated HTML

4
What are the Advantages of JSP?
  • vs. Static HTML.
  • Regular HTML, of course, cannot contain dynamic
    information. JSP is so easy and convenient that
    it is quite feasible to augment HTML pages that
    only benefit marginally by the insertion of small
    amounts of dynamic data. Previously, the cost of
    using dynamic data would preclude its use in all
    but the most valuable instances.

5
What are the Advantages of JSP?
  • vs. Pure Servlets.
  • JSP doesn't give you anything that you couldn't
    in principle do with a servlet. But it is more
    convenient to write (and to modify!) regular HTML
    than to have a zillion println statements that
    generate the HTML. Plus, by separating the look
    from the content you can put different people on
    different tasks your Web page design experts can
    build the HTML, leaving places for your servlet
    programmers to insert the dynamic content.

6
What are the Advantages of JSP?
  • vs. JavaScript.
  • JavaScript can generate HTML dynamically on the
    client. This is a useful capability, but only
    handles situations where the dynamic information
    is based on the client's environment. With the
    exception of cookies, HTTP and form submission
    data is not available to JavaScript. And, since
    it runs on the client, JavaScript can't access
    server-side resources like databases, catalogs,
    pricing information, and the like.

7
What are the Advantages of JSP?
  • vs. Server-Side Includes (SSI).
  • SSI is a widely-supported technology for
    including externally-defined pieces into a static
    Web page. JSP is better because it lets you use
    servlets instead of a separate program to
    generate that dynamic part. Besides, SSI is
    really only intended for simple inclusions, not
    for "real" programs that use form data, make
    database connections, and the like.

8
What are the Advantages of JSP?
  • vs. Active Server Pages (ASP).
  • ASP is a similar technology from Microsoft. 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

9
JSP Scripting Elements
  • JSP scripting elements let you insert Java code
    into the servlet that will be generated from the
    current JSP page. There are three forms
  • Expressions of the form that
    are evaluated and inserted into the output,
  • Scriptlets of the form that are
    inserted into the servlet's service method, and
  • Declarations of the form that are
    inserted into the body of the servlet class,
    outside of any existing methods.

10
JSP Expressions
  • The Java expression is evaluated, converted to a
    string, and inserted in the page.
  • Example
  • Current time
  • the most important ones are
  • request, the HttpServletRequest
  • response, the HttpServletResponse
  • session, the HttpSession associated with the
    request (if any) and
  • out, the PrintWriter (a buffered version of type
    JspWriter) used to send output to the client.

11
JSP Scriptlets
  • JSP scriptlets let you insert arbitrary code into
    the servlet method that will be built to generate
    the page.
  • Scriptlets have access to the same automatically
    defined variables as expressions.
  • Example
  • If you want output to appear in the resultant
    page, you would use the out variable.
  • out.println("Attached GET data " queryData)

12
JSP Declarations
  • A JSP declaration lets you define methods or
    fields that get inserted into the main body of
    the servlet class (outside of the service method
    processing the request).
  • Example

  • //declaration
  • Accesses to page since server reboot
  • //expression

13
JSP Directives
  • affects the overall structure of the servlet
    class.
  • page, which lets you do things like import
    classes, customize the servlet superclass, and
    the like and
  • include, which lets you insert a file into the
    servlet class at the time the JSP file is
    translated into a servlet.

14
The JSP page Directive
  • Some Others
  • contentType"MIME-Type"
  • isThreadSafe"truefalse".
  • session"truefalse". predefined variable session
    ?
  • buffer"sizekbnone

15
The JSP include Directive

16
Example Using Scripting Elements and Directives
  • Transitional//EN"
  • Using JavaServer Pages
  • hall_at_apl.jhu.edu"
  • Pages,servlets"
  • of the four main JSP tags." HREF"My-Style-Sheet.css" TYPE"text/css"
  • LINK"0000EE" VLINK"551A8B" ALINK"FF0000"
  • Using JavaServer Pages
  • Some dynamic content created using various
    JSP mechanisms
    • Expression.
      Your hostname request.getRemoteHost() .
    • Scriptlet.
      GET data " request.getQueryString())
    • Declaration (plus expression).
      private int accessCount 0 Accesses to page
      since server reboot
    • Directive (plus expression).
      page import "java.util." Current date new Date()

    17
    Possible Page
    18
    Predefined Variables
    • To simplify code in JSP expressions and
      scriptlets, you are supplied with eight
      automatically defined variables, sometimes called
      implicit objects.
    • The available variables are request, response,
      out, session, application, config, pageContext,
      and page.

    19
    • request
    • This is the HttpServletRequest associated with
      the request, and lets you look at the request
      parameters (via getParameter), the request type
      (GET, POST, HEAD, etc.), and the incoming HTTP
      headers (cookies, Referer, etc.). Strictly
      speaking, request is allowed to be a subclass of
      ServletRequest other than HttpServletRequest, if
      the protocol in the request is something other
      than HTTP. This is almost never done in practice.
    • response
    • This is the HttpServletResponse associated with
      the response to the client. Note that, since the
      output stream (see out below) is buffered, it is
      legal to set HTTP status codes and response
      headers, even though this is not permitted in
      regular servlets once any output has been sent to
      the client.

    20
    • out
    • This is the PrintWriter used to send output to
      the client. However, in order to make the
      response object (see the previous section)
      useful, this is a buffered version of PrintWriter
      called JspWriter. Note that you can adjust the
      buffer size, or even turn buffering off, through
      use of the buffer attribute of the page
      directive. Also note that out is used almost
      exclusively in scriptlets, since JSP expressions
      automatically get placed in the output stream,
      and thus rarely need to refer to out explicitly.
    • session
    • This is the HttpSession object associated with
      the request. Recall that sessions are created
      automatically, so this variable is bound even if
      there was no incoming session reference. The one
      exception is if you use the session attribute of
      the page directive to turn sessions off, in which
      case attempts to reference the session variable
      cause errors at the time the JSP page is
      translated into a servlet.

    21
    • application
    • This is the ServletContext as obtained via
      getServletConfig().getContext().
    • config
    • This is the ServletConfig object for this page.
    • pageContext
    • JSP introduced a new class called PageContext to
      encapsulate use of server-specific features like
      higher performance JspWriters. The idea is that,
      if you access them through this class rather than
      directly, your code will still run on "regular"
      servlet/JSP engines.
    • page
    • This is simply a synonym for this, and is not
      very useful in Java. It was created as a
      placeholder for the time when the scripting
      language could be something other than Java.

    22
    Some Tutorials
    • http//www.apl.jhu.edu/hall/java/Servlet-Tutorial
      /Servlet-Tutorial-JSP.html
    • http//www.jsptut.com/
    • http//www.apl.jhu.edu/hall/java/Servlet-Tutorial
      /
    • http//java.sun.com/developer/onlineTraining/JSPIn
      tro/contents.html
Write a Comment
User Comments (0)
About PowerShow.com