Java Server Pages (JSP) - PowerPoint PPT Presentation

About This Presentation
Title:

Java Server Pages (JSP)

Description:

In this example, the user is presented with choice boxes. ... FORM ACTION='choices.jsp' INPUT type='checkbox' name='music' value='Classical' Classical BR ... – PowerPoint PPT presentation

Number of Views:118
Avg rating:3.0/5.0
Slides: 15
Provided by: gu8
Category:
Tags: jsp | java | pages | server

less

Transcript and Presenter's Notes

Title: Java Server Pages (JSP)


1
Java Server Pages (JSP)
2
A Java Servlets Example
  • A simple "HelloWorld" servlet, that also prints
    the current date.
  • import java.io.
  • import javax.servlet.
  • import javax.servlet.http.
  • public class HelloWorld extends HttpServlet
  • public void doGet(HttpServletRequest req,
    HttpServletResponse res) throws ServletException,
    IOException
  • res.setContentType("text/html")
  • PrintWriter out res.getWriter()
  • out.println("ltHTMLgt")
  • out.println("ltHEADgtltTITLEgtHello
    Worldlt/TITLEgtlt/HEADgt")
  • out.println("ltBODYgt")
  • out.println("ltH1gtHello Worldlt/H1gt")
  • out.println("Today is " (new
    java.util.Date().toString()) )
  • out.println("lt/BODYgtlt/HTMLgt")
  • // doGet
  • // HelloWorld
  • In order to run it, do the following
  • Place it in a file, HelloWorld.java
  • Compile it.

3
A JSP Example
  • The previous page can be written using JSP as
    shown below
  • ltHTMLgt
  • ltHEADgt
  • ltTITLEgtHello Worldlt/TITLEgt
  • lt/HEADgt
  • ltBODYgt
  • ltH1gtHello Worldlt/H1gt
  • Today is lt new java.util.Date().toString()
    gt
  • lt/BODYgt
  • lt/HTMLgt
  • In order to run it, do the following
  • Place it in a file, HelloWorld.jsp in the same
    directory as your .html files
  • Run it by pointing browser to http//server.addre
    ss/youraccount/HelloWorld.jsp

4
Elements of JSP
  • Whenever a .jsp is requested for the first time,
    the server does the following
  • 1. Translates the .jsp page into a servlet2.
    Compiles the servlet into a class file3.
    Executes the servlet (response is sent to the
    client)
  • Subsequent requests (as long as the .jsp page is
    unchanged) use the same loaded class file.
  • Anatomy of a JSP Page
  • A JSP page is a mixture of standard HTML tags,
    web page content, and some dynamic content that
    is specified using JSP constructs. Everything
    except the JSP constructs is called Template
    Text.

5
JSP Constructs JSP Comments
  • JSP Comments Different from HTML comments
  • lt!-- an HTML comment --gt
  • lt-- a JSP comment --gt
  • JSP comments are used for documenting JSP code
    and are not visible client-side (using browser's
    View Source option) where as HTML comments are
    visible.

6
JSP Constructs JAVA Expressions
  • Format lt some_java_expression gt
  • Examplelt new java.util.Date().toString() gt
  • Output of the expression is placed in the HTML
    template at the same location.

7
JSP Constructs Variables in JAVA Expressions
  • There are some pre-defined Java variables/objects
    available for use in expressions (provide access
    to important servlet functionality)
  • requestThis is the same object as
    HttpServletRequest parameter in th get/post
    methods. Same methods (like, getParameter,
    getAttribute, etc) can be applied to it.
  • outThe servlet printwriter.
  • sessionSame as servlet session object.
  • Example
  • ltHTMLgt
  • ltHEADgt
  • ltTITLEgtJSP Expressions Predefined
    Objectslt/TITLEgt
  • lt/HEADgt
  • ltBODYgt
  • ltH1gtUsing Predefined Objectslt/H1gt
  • ltULgt
  • ltLIgt Your Hostname lt request.getRemoteHos
    t() gt
  • ltLIgt Your Session ID lt session.getId()
    gt
  • ltLIgt The value of INFO parameter lt
    request.getParameter("INFO") gt
  • lt/BODYgt
  • lt/HTMLgt

8
JSP Constructs Scriptlets
  • Scriptlets are arbitrary pieces of Java code
    inserted in the page using the format lt
    some_java_code gt

Example 2 ltHTMLgt ltHEADgt ltTITLEgtJSP
Scriptlets 2lt/TITLEgt lt/HEADgt lt String
bgColor request.getParameter("COLOR") gt
lt if (bgColor null) gt
ltBODY BGCOLOR"FFFFFF" gt lt else gt
ltBODY BGCOLOR"lt bgColor gt" gt lt
gt ltH1gtExample Scriptlet
Conditionally sets background colorlt/H1gt lt
if (bgColor null) gt You did not
supply a color, I used white. lt else
gt Here is the color you requested.
lt gt lt/BODYgt lt/HTML
Example 1 ltHTMLgt ltHEADgt ltTITLEgtJSP
Scriptletslt/TITLEgt lt/HEADgt lt
String bgColor request.getParameter("COLOR")
if (bgColor null)
bgColor "WHITE" gt ltBODY
BGCOLOR"lt bgColor gt" gt ltH1gtExample
Scriptlet Sets background colorlt/H1gt
lt/BODYgt lt/HTMLgt
9
JSP Constructs Scriptlets, contd.
  • Using Arrays One can easily use scriptlets to
    loop over arrays. In this example, the user is
    presented with choice boxes. When s/he presses
    the submit button, the choices are displayed.
  • Example
  • ltHTMLgt
  • ltBODY BGCOLOR"WHITE"gt
  • ltFORM ACTION"choices.jsp"gt
  • ltINPUT type"checkbox" name"music"
    value"Classical"gt ClassicalltBRgt
  • ltINPUT type"checkbox" name"music"
    value"Rock"gt RockltBRgt
  • ltINPUT type"checkbox" name"music"
    value"Jazz"gt JazzltBRgt
  • ltINPUT type"checkbox" name"music"
    value"Blues"gt BluesltBRgt
  • ltINPUT type"checkbox" name"music"
    value"DC-GoGo"gt DC GoGoltBRgt
  • ltINPUT type"submit" value"Submit"gt
  • lt/FORMgt
  • lt
  • String selected request.getParameterVal
    ues("music")
  • if (selected ! null selected.length !
    0)
  • gt
  • You like the following kinds of music
  • ltULgt
  • lt

10
JSP Constructs Declarations
  • You can define variables and/or methods lt! some
    JAVA declarations gt
  • Example
  • ltHTMLgt
  • ltBODYgt
  • lt!
  • private int hitCount 0
  • String randomColor()
  • java.util.Random random new java.util.Random()
  • int R (int) (random.nextFloat() 255)
  • int G (int) (random.nextFloat() 255)
  • int B (int) (random.nextFloat() 255)
  • return "" Integer.toString(R, 16)
    Integer.toString(G, 16) Integer.toString(B,
    16)
  • gt
  • ltFONT COLOR"lt randomColor() gt" gt This page
    has been accessed lt hitCount gt times.
    lt/FONTgt
  • lt/BODYgt
  • lt/HTMLgt

11
JSP Constructs Summary
  • Expressions
  • Scriptlets
  • Declarations
  • Availability of pre-defined objects

12
JSP Directives
  • Format
  • lt_at_ directive attribute"value" gt
  • lt_at_ directive attr1"value" attr2"value" ...
    attrN"value" gt
  • Directives are used to specify the structure of
    the resulting servlet. There are three
    directives page, include, and taglib

13
JSP Page Directive
  • There are 11 specifiable attributes for this
    directive import, contentType, isThreadSafe,
    session, buffer, autoflush, extends, info,
    errorPage, and language
  • Example
  • lt_at_ page language"java" contentType"text/html"
    gt
  • lt_at_ page contentType"application/vnd.ms-excel"
    gt
  • lt_at_ page import"java.util." gt
  • lt_at_ page import"java.util.,java.io." gt
  • Directives can be placed anywhere in the
    document, but are often placed at the very top.
  • lt_at_ page language"java" contentType"text/html
    import"java.util." gt
  • ltHTMLgt
  • ltHEADgt
  • ltTITLEgtHello Worldlt/TITLEgt
  • lt/HEADgt
  • ltBODYgt
  • ltH1gtHello Worldlt/H1gt
  • Today is lt new Date().toString() gt
  • lt/BODYgt
  • lt/HTMLgt

14
JSP Other Directives
  • The include Directive
  • Is used to include a file in the main document.
  • There are two versions of this. The first one,
    shown below, includes the file at translation
    time.
  • lt_at_ include file"relative URL of file" gt
  • The second version, includes the file at request
    time.
  • ltjspinclude page"ralative URL of file"
    flushtrue /gt
  • The taglib directive
  • This is used to define custom markup tags. Refer
    to JSP documentation for details on this.
Write a Comment
User Comments (0)
About PowerShow.com