JSP - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

JSP

Description:

There is more than one type of JSP 'tag,' depending on what you want done with the Java ... The included file is treated as just more JSP, hence it can include ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 25
Provided by: nagar7
Category:
Tags: jsp | compile

less

Transcript and Presenter's Notes

Title: JSP


1
JSP
  • Java Server Pages

2
Java Server Pages
  • JSP (Java Server Pages) is an alternate way of
    creating servlets
  • JSP is written as ordinary HTML, with a little
    Java mixed in
  • The Java is enclosed in special tags, such as lt
    ... gt
  • The HTML is known as the template text
  • JSP files must have the extension .jsp
  • JSP is translated into a Java servlet, which is
    then compiled
  • Servlets are run in the usual way
  • The browser or other client sees only the
    resultant HTML, as usual
  • Tomcat knows how to handle servlets and JSP pages

3
Jsp Execution
4
JSP scripting elements
  • There is more than one type of JSP tag,
    depending on what you want done with the Java
  • lt expression gt
  • The expression is evaluated and the result is
    inserted into the HTML page
  • lt code gt
  • The code is inserted into the servlet's service
    method
  • This construction is called a scriptlet
  • lt! declarations gt
  • The declarations are inserted into the servlet
    class, not into a method

5
Example JSP
  • ltHTMLgtltBODYgtHello!  The time is now lt new
    java.util.Date() gtlt/BODYgtlt/HTMLgt
  • Notes
  • The lt ... gt tag is used, because we are
    computing a value and inserting it into the HTML
  • The fully qualified name (java.util.Date) is
    used, instead of the short name (Date), because
    we havent yet talked about how to do import
    declarations

6
Variables
  • You can declare your own variables, as usual
  • JSP provides several predefined variables
  • request The HttpServletRequest parameter
  • response The HttpServletResponse parameter
  • session The HttpSession associated with the
    request, or null if there is none
  • out A JspWriter (like a PrintWriter) used to
    send output to the client
  • Example
  • Your hostname lt request.getRemoteHost() gt

7
Scriptlets
  • Scriptlets are enclosed in lt ... gt tags
  • Scriptlets do not produce a value that is
    inserted directly into the HTML (as is done with
    lt ... gt)
  • Scriptlets are Java code that may write into the
    HTML
  • Examplelt String queryData request.getQueryStr
    ing() out.println("Attached GET data "
    queryData) gt
  • Scriptlets are inserted into the servlet exactly
    as written, and are not compiled until the entire
    servlet is compiled
  • Examplelt if (Math.random() lt 0.5) gt
    Have a ltBgtnicelt/Bgt day!lt else gt
    Have a ltBgtlousylt/Bgt day!lt gt

8
Declarations
  • Use lt! ... gt for declarations to be added to
    your servlet class, not to any particular method
  • Caution Servlets are multithreaded, so nonlocal
    variables must be handled with extreme care
  • If declared with lt ... gt, variables are local
    and OK
  • Data can also safely be put in the request or
    session objects
  • Examplelt! private int accessCount 0 gt
    Accesses to page since server reboot lt
    accessCount gt
  • You can use lt! ... gt to declare methods as
    easily as to declare variables

9
Directives
  • Directives affect the servlet class itself
  • A directive has the form lt_at_ directive
    attribute"value" gtor lt_at_ directive
    attribute1"value1"
    attribute2"value2"
    ... attributeN"valueN"
    gt
  • The most useful directive is page, which lets you
    import packages
  • Example lt_at_ page import"java.util." gt

10
JSP Comments
  • Different from HTML comments.
  • HTML comments are visible to client.
  • lt!-- an HTML comment --gt
  • JSP comments are used for documenting JSP code .
  • JSP comments are not visible client-side.
  • lt-- a JSP comment --gt

11
The include directive
  • The include directive inserts another file into
    the file being parsed
  • The included file is treated as just more JSP,
    hence it can include static HTML, scripting
    elements, actions, and directives
  • Syntax lt_at_ include file"URL " gt
  • The URL is treated as relative to the JSP page
  • If the URL begins with a slash, it is treated as
    relative to the home directory of the Web server
  • The include directive is especially useful for
    inserting things like navigation bars

12
Actions
  • Actions are XML-syntax tags used to control the
    servlet engine
  • ltjspinclude page"URL " /gt
  • Inserts the indicated relative URL at execution
    time (not at compile time, like the include
    directive does)
  • This is great for rapidly changing data
  • ltjspforward page"URL" /gtltjspforward page"lt
    JavaExpression gt" /gt
  • Jump to the (static) URL or the (dynamically
    computed) JavaExpression resulting in a URL

13
JSP in XML
  • JSP can be embedded in XML as well as in HTML
  • Due to XMLs syntax rules, the tags must be
    different (but they do the same things)
  • HTML lt expression gtXML ltjspexpressiongtexpre
    ssionlt/jspexpressiongt
  • HTML lt code gtXML ltjspscriptletgtcodelt/jspscr
    iptletgt
  • HTML lt! declarations gtXML ltjspdeclarationgtde
    clarationslt/jspdeclarationgt
  • HTML lt_at_ include fileURL gtXML
    ltjspdirective.include file"URL"/gt

14
Action useBean tag
  • The useBean action tag is the most commonly used
    tag because of its powerful features.
  • It allows a JSP to create an instance or receive
    an instance of a Java Bean.
  • It is used for creating or instantiating a bean
    with a specific name and scope.
  • Examples
  • ltjspuseBean idtime" scope"session"
    class"com.time.CurrentTimeBean" /gt

15
Session in jsp
  • In session management whenever a request comes
    for any resource, a unique token is generated by
    the server and transmitted to the client by the
    response object and stored on the client machine
    as a cookie.
  • Session management
  • (i) Session Object
  • (ii) Cookies
  • (iii) Hidden Form Fields
  • (iv) URL Rewriting

16
Javascript
17
JavaScript language
  • The JavaScript language is an interpreted, object
    oriented based scripting language.
  • JavaScript coding statements are commonly
    embedded inside a webpage using the HTML tags
    ltSCRIPTgt lt/SCRIPTgt.
  • When the web browser is rendering the webpage,
    any JavaScript coding statements encountered are
    immediately interpreted and run before the
    rendering continues.

18
Where to use JavaScript
  • Verify the end user input data contains
    legitimate data values.
  • Handle any arithmetic or logic processing that
    needs to be executed on the web browser.
  • Directly manipulate the web browser objects such
    as the status bar, address bar, and the web
    browser's rendered controls (such as textboxes,
    selection lists, etc...).
  • Handle and process client-side events that have
    been generated.

19
How to run JavaScripts
  • JavaScript statements are interpreted and run by
    the web browser
  • Most common web browsers contain a JavaScript
    interpreter program
  • interpret and run the JavaScript commands that
    have been embedded inside a webpage. 
  • Question1 In a web application, which entity
    executes JSPs?
  • Question2 In a web application, which entity
    executes JavaScript commands?

20
JavaScript programming example
  • ltHTMLgt
  • 2 ltHEADgtltTITLEgt JavaScript Example1
    lt/TITLEgtlt/HEADgt
  • 3 ltBODYgt
  • 4 ltH1gtJavascript Example1lt/H1gt
  • 5 An alert box will appear using the window
    object ltbrgt
  • 6
  • 7 ltSCRIPTgt
  • 8 window.alert("hello world")
  • 9 lt/SCRIPTgt
  • 10
  • 11 Goodbye!
  • 12
  • 13 lt/BODYgt
  • 14 lt/HTMLgt

21
Events
  • An event is an identifiable occurrence or action
    that has taken place in the system. Writing
    programs that generate and handle events is known
    as event programming.
  • Events are typically generated in the following
    ways.
  • GUI programs can generate an event to indicate
    there has been some interaction between the
    widget and the end user.
  • when an end user presses a button, a click event
    is generated.
  • Input/output programs can generate an event to
    indicate an input/output event has occurred.
  • A mouse event is generated when the end user
    moves the mouse.

22
Binding Events
23
Binding Events
  • In client side web programming, the web browser
    contains
  • it's own event listener.
  • Therefore you must do two things
  • in order to process an event that has been
    generated.
  • 1. Write the code that binds the event to the
    event handler.
  • 2.  Write the code for the event handler.

24
Common web browser events
Write a Comment
User Comments (0)
About PowerShow.com