developing web applications using java server pages (jsp) final

Upload: as-roni

Post on 06-Jul-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    1/99

     

    Applications using Java

    Server Pages (JSP)

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute 1

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    2/99

    Table of Contents

    •  

    • Chapter 2: JSP Syntax•  

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 2

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    3/99

     

    JSP Basics

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute 3

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    4/99

    Chapter 1 Outline

    What is Java Server Page (JSP)?

     

    What is the difference between servlets and JSP?

    van ages o .

    How JSP look like?

    How JSP works?

     javax.servlet.jsp package. JspPage interface

    HttpJspPage Interface

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 4

    JSP Classes

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    5/99

    What is Java Server Page (JSP) ?

    • “An extension of the servlet technology created

    “  .

     

     – Most Web pages are HTML.

     –

    the servlet programmer.

     – No clear separation of presentation and logic.

     – Little bit difficult for web programmers.

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 5

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    6/99

    What is the difference between servlets andJSP?

    • Servlets:

     – .

     – Generate dynamic content. – .

    • JavaServer Pages: –

    they can be pure XML.

     – .

     – Separate static and dynamic content.

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 6

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    7/99

    Benefits of JSP

    • Easier to author (Web-site development tools).

    .

    • Business and presentation logic are separated.

    • mp y eve opmen w , ava eans an

    custom tags.•

    components.

     the source file.

    •JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 7

      .

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    8/99

    How JSP look like?

    • tags stay as they are. When you need to add dynamiccontent, all you need to do is enclose your code in tags

    Using JSP

    Enumeration parameters = request.getParameterNames();

    String param = null;

    while (parameters.hasMoreElements()) {

    param = (String) parameters.nextElement();

    out.println(param + ":" + request.getParameter(param) +"
    ");}

    out.close();%>

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 8

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    9/99

    How JSP works ?

    J2EE container 

    1Firsttime

     YesWeb

    container ( page compiler )

    http://host/date.jspCreate servletdate.javaNo

    2

     date.class

    Servlet life cycle

    3

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 9

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    10/99

    Example

    public void _jspService(…..)

    {

    out.write("\r\n");

    " ".

    out.write("\r\n");

    out.write("\r\n");< ou .pr n n s easy ; > ou .pr n n s easy ;

    out.write("\r\n");

    out.write("\r\n");

    out.write("");

    }

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 10

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    11/99

    Notes on JSP life Cycle

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 11

    * Source: Core Servlets and Javaserver Pages By Marty Hall, Larry Brown ISBN 0130092290

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    12/99

    Why JSP developers need servlets?

    1. JSP pages are translated into servlets.

    . cons s s o s a c , ags, an

    Java code (Servlet code).

    . ome as s are e er accomp s e y serv e s

    than by JSP.. .

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 12

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    13/99

     javax.servlet.jsp.*

     – javax.servlet.jsp – javax.servlet.jsp.tagext   custom tags - Advanced

    • javax.servlet.jsp contains 2 interfaces , 2

    Exceptions and 4 classes

    • Interfaces :

     – JspPage – HttpJspPage

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 13

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    14/99

     javax.servlet.jsp.* (cont’d)

    • Classes: – sp ac ory

     – PageContext

     –

     – JspWriter 

     – JspException

     – Js Error.

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 14

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    15/99

    JspPage interface

    • The JspPage is the interface that must be implementedb all JSP servlet classes.

    • It contains two methods: – public void jspInit()

     – ublic void s Destro

    Example:

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    16/99

    HttpJspPage Interface

    • It extends the JspPage interface.

    • It has got only one method :

     – public void _jspService ( HttpServletRequest

    request,HttpServletResponse response ) throws

    ServletException, IOException.

    • It’s called by the JSP container, to generate the content

    of the JSP page.

    • you can't include this method in a JSP page, because

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 16

      .

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    17/99

    JSP Classes

    • The JspEngineInfo class• “ ” 

    • The JspWriter class

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 17

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    18/99

    1-The JspFactory Class

    • n a s rac c ass a prov es me o s or o a n ngother objects needed for the JSP page processing.

    • The class has the static method getDefaultFactory that

    returns a JspFactory object.

    • From the JspFactory object, a PageContext and aJspEngineInfo object can be obtained using

    :

     – public abstract JspEngineInfo getEngineInfo( )

     – pu c a s rac age on ex ge age on ex erv erequestingServlet, ServletRequest request,ServletResponse response, String errorPageURL,

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 18

      , ,

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    19/99

    The JspFactory Class (cont.)

    • The followin code is art of the s Servicemethod that is generated by the JSP container:

     _

    PageContext pageContext = null;

     _jspxFactory =JspFactory.getDefaultFactory();

    pageContext = _jspxFactory.getPageContext ( this,

    request,response, ,true, 8192,true);

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 19

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    20/99

    2-The JspEngineInfo Class

     –

    that provides information on the JSPcontainer.

     – It has only one method,

    getSpecificationVersion, returns the JSPcontainer's version number.

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 20

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    21/99

    3-The PageContext Class

     – PageContext represents a class that provides

    methods that are im lementation-de endent. 

     – ,

    the _ jspService method of a JSP servlet

    class, a PageContext object is obtained bycalling the getPageContext method of the

    JspFactory class.

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 21

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    22/99

    The PageContext Class (cont.)

     – The PageContext class provides methods thatare used to create other objects.

    • getOut method returns a JspWriter object that is

    used to send strings to the web browser.

    • ge eques , re urns a erv e eques o ec .

    • getResponse, returns a ServletResponse object., .

    • getServletContext, returns a ServletContext object.

    • etSession  returns an Htt Session ob ect.

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 22

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    23/99

    4-The JspWriter Class

    • The JspWriter class is derived from the

    ava.io.Writer class and re resents a Writer that 

    you can use to write to the client browser.

    • Of its many methods, the most important are the

    print and println methods.

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 23

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    24/99

    Implicit Objects of PageContext class

    • . . .

    • response  javax.servlet.http.HttpServletResponse• . . .

    • session  javax.servlet.http.HttpSession

      . .

    • config  javax.servlet.ServletConfig

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 24

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    25/99

    Review

    file• If the code written in the JSP a e was onl :

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 25

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    26/99

    Strategies for invoking dynamic code

    . a ava co e rec y cr p e s Simple

    . ,

    only the Java code needed to invoke the utility classes.

    3. Develop separate utility classes structured as beans.

    4. MVC architecture. A servlet respond to original request,

    look up data, and store results in beans, forward to a

    1. Use the JSP expression language.Complex

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 26

    2. Use custom tags.

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    27/99

    Strategies for invoking dynamic code (cont’)

    • Why limiting the amount of Java code in JSP

    a es ?

     – Development. – Compilation.

     – Debugging.

     – Division of labor. – Testing.

     – Reuse.

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 27

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    28/99

     

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute 28

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    29/99

    Assignments

    • .

    • Make an html page that redirect a request to anJSP

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 29

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    30/99

     

    JSP Syntax

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute 30

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    31/99

    Chapter 2 Outline

    JSP tags :

     

    Scripting elements

    Action elements

    Converting into XML syntax

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 31

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    32/99

    JSP Syntax

    • JSP contains 2 main things :

     –   ags e emen s : ma e up e syn ax an

    semantics of JSP. – Tem late Data : an thin else not understood b 

    the JSP container

    • example :HTML tags

    • JSP tags are :

     – Directive elements

     – cr p ng e emen s

     – Action elements

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 32

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    33/99

    JSP tags

    JSP tags

    ScriptingDirective Action

    page Scriptlets

    include Declarations

    taglib Expressions

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 33

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    34/99

    1-Directive Elements

    • They are messages to the JSP containercontaining information on how the JSP containermust translate a JSP page into a corresponding

    servlet.•

    not a code.

    •   – Page directives

     – Include directives

     – Tag library directives (used with custom tags )

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 34

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    35/99

    1-Directive Elements (cont’d)

     – • Exam le

     –

     –

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    36/99

    1 Directive Elements (cont d)a-Page Directive Attributes

    Atribute Value Type Default

    import Fully qualified class name None

    contentType MIME type, character set"text/html;charset=ISO-

    8859-1”session Boolean “true”

    buffer Buffer size in Kb or false 8192

    autoFlush Boolean "true”

     

    errorPage URL None

    isErrorPage Boolean "false”

    isThreadSafe Boolean "true”

    extends Class name None

    lan ua e Scri tin lan ua e name " ava"

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 36

    1-Directive Elements (cont’d)

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    37/99

    1 Directive Elements (cont d)a-Page Directive Attributes

     –  –

     –

     –  –

    • It’s not allowed to repeat assigned attributes

    excep or mpor a r u e

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 37

    1-Directive Elements (cont’d)

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    38/99

    1 Directive Elements (cont d)a-Page Directive Attributes

    Notes on import attribute

    • 1st and 2nd strategy for invoking dynamic code.

    • Utility classes should always be in packages and the JSP

    page should use the import attribute.

    • , . . ,

     javax.servlet.*, javax.servlet.jsp.*, javax.servlet.http.*.

    • The onl a e attribute that is allowed to a ear multi le

    times.

    • The utility classes should be placed in /WEB-NF/classes/

    •   Note:

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 38

    1-Directive Elements (cont’d)

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    39/99

    1 Directive Elements (cont d)b-The Include Directive

    • It enables JSP page authors to include the

    contents of other files in the current P a e. 

    • We can include static page like html or anotherJSP a e.

    • Include Syntax:

     –

    • Example:

     –

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    40/99

    2-Scripting Elements

    pages.• 1st and 2nd strate for invokin d namic code.

    • There are three types:

     – Scri tlets

     – Declarations

     – Expressions

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 40

    2-Scripting Elements (cont’d)

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    41/99

    2 Scripting Elements (cont d)a-Scriptlets

    • Inserted into the servlet’s _jspService method

    • Syntax

    • xamp e:

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 41

    2-Scripting Elements (cont’d)

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    42/99

    2 Scripting Elements (cont d)b-Declarations

    • t s use to ec are met o s or var a es.• Inserted into the body of the servlet class, outside

    any ex s ng me o s.

    • Can not access the implict objects of _jspService

    • Syntax: 

    • Examples:

     

    return Calendar.getInstance().getTime().toString();

    >

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 42

    2-Scripting Elements (cont’d)

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    43/99

    2 Scripting Elements (cont d)c-Expressions

    • It’s used to evaluate expressions and print them

    to the user. 

    • It’s used instead of out.println()

    • Syntax:

    < = > 

    • Example:

    The same result of :

    out.print( java.util.Calendar.getInstance().getTime() );

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 43

    A i t

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    44/99

    Assignment

    • Write a utility class that generates a random

    .

    • Invoke methods of this utility class from jsp page.

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 44

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    45/99

    3-Standard Action Elements

    They consists of :

    • jsp:forward• s : aram

    • jsp:plugin

    • jsp:params• jsp:fallback

    • jsp:useBean

    • jsp:setProperty

    • jsp:getProperty

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 45

    3-Standard Action Elements(cont’)

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    46/99

    ( )Include, forward and param

    • They got the same effect of RequestDispatcher

    include and forward 

    • Syntax:=" “

    ( )*

    ( )*

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 46

    3-Standard Action Elements(cont’)

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    47/99

    ( )Include, forward and param

    Notes on jsp:include• The included pages can be

    - es, p a n ex es, pages, erv e s

    - With JSP pages and servlets, only the output of the pageis included not the actual code. 

    • Do not use complete HTML documents for the included

    pages.• The included page uses the same request object as the

    originally requested page and sees the same request

    • The main page can add to or replace the request

    parameters using jsp:param.

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 47

    3-Standard Action Elements(cont’)

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    48/99

    ( )Include, forward and param

    Notes on jsp:forward• Using jsp:forward obtain the complete output from the

    .

    • The main page must not have any output.• The main a e can add to or re lace the re uest 

    parameters using jsp:param.

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 48

    jsp incl de s incl de di ecti e

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    49/99

     jsp:include vs. include directive

    sp: nc u e page= ur nc u e e= ur

    • nc u e e ou pu  o

    a page at request time

     

    main page before that

    main page is translatedinto a servlet.

    • No change needed in

    the main page when the

    • The main page must be

    updated whenever any of

    the included a es.

    •The included pages

    change.

    • The included code can

    cannot use any JSP

    constructs that affect the

    main page as a whole.

     such as field definitions

    and content-type settings

    that affect the main page

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 49

    as a whole.

    Assignment

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    50/99

    Assignment

    • Use both jsp:include and include directive to

    difference.

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 50

    3-Standard Action Elements(cont.)

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    51/99

    ( )Plugin, Fallback and params

    • Sun developed a browser plug-in for Netscape andInternet Explorer that lets you use the Java 2 platform in

    a var e y o rowser vers ons.

    • In some browsers, the normal APPLET tag will not workwith the lu -in. Instead use OBJECT ta for Internet 

    Explorer and EMBED tag for Netscape.

    • The is used to generate HTML or < > ags a ns ruc e rowser o own oa

    the Java Plugin software, if required, and initiate the

    execution of the Java applet specified.• Note: The applet .class files must not be placed in WEB-

    INF/classes, as the browser, not the server, uses them.

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 51

    3-Standard Action Elements(cont.)

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    52/99

    Plugin, Fallback and params

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    53/99

    useBean, setProperty and getProperty

     – Will be described in details in the next chapter 

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 53

    Comment tag

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    54/99

    Comment tag

    • HTML comments

    < -- --> 

    • JSP comment

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 54

    Assignment

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    55/99

    Assignment

    • Use to embed Applet in your jsppage.

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 55

    Converting into XML syntax

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    56/99

    Converting into XML syntax

     – The content of the JSP page can be validated againsta set of descriptions.

     – The JSP page can be manipulated using an XML

    tool.

     –

    representation by applying an XML transformation

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 56

    Converting into XML syntax (cont’d)

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    57/99

    Converting into XML syntax (cont d)

    • Syntax –

     – < sp: ec ara on> ec ara on co e < sp: ec ara on>

     – scriptlet code  – < > < > 

    • Examples:< s :directive. a e attr=“value” />

    String s; s =request.getParameter("user");

    …….

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 57

    Converting into XML syntax (cont’d)

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    58/99

    Converting into XML syntax (cont d)

    • Notes on XML Syntax: – XML element names can contain colons, XML-

    compa e vers on o a s an ar e emen s

    starts with the jsp prefix (or namespace).

     – XML tags are case sensitive.

     – XML tags must be explicitly closed.

     – To get ' within an attribute value, use \‘.

    To get ", use \“; to get \, use \\; to get %>, use %\>;

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 58

      , .

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    59/99

     

    JSP and Java Beans

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute 59

    Chapter 3 Outline

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    60/99

    Chapter 3 Outline

    What is a Bean?

      .

    How to use a request parameter to feed the value of a

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 60

    What is a Bean?

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    61/99

    What is a Bean?

    • A bean is a java class with certain rules defined

    by the JavaBeans specification.

    • Beans extend no particular class and use no

    particular interface.

    • The 3rd strategy for invoking dynamic code.

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 61

    Why beans?

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    62/99

    Why beans?

    • Why we should use beans in JSP ?

     – .

     – No Java syntax, instead XML-compatible tags.

     – Clear separation of roles.

     – Simpler object sharing.

     – Convenient correspondence between requestparame ers an o ec proper es.

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 62

    Java Beans Rules

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    63/99

    Java Beans Rules

    • A bean class must have a default constructor(which will be called when JSP elements or

    servlets create beans).

    • A bean class should have no public instance

    variables.

    • Persistent values (properties) should be

    accessed through methods called getXxx and

    setXxx.

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 63

    Or isXxx and setXxx for bolean properties.

    Java Beans Rules (cont’)

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    64/99

    Java Beans Rules (cont )

    • Example:- If our Book bean class has a lon instance variable

    isbn and a boolean borrowed.

    - Then your bean class should have the following

    standard JSP actions:

    long getIsbn ()

    void setIsbn (long isbn)

    boolean isBorrowed ()

    void setBorrowed (boolean borrowed)

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 64

    Java Beans Rules (cont’)

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    65/99

    Java Beans Rules (cont )

    • Notes on Bean classes :

     – Place all your beans in packages.

     – Install them in the normal Java code directories:

    WEB-INF/classes/ for individual classes and

    WEB-INF/lib for JAR files.

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 65

    Tag Structure

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    66/99

    Tag Structure

    • < sp:use ean attr ute= va ue + >• Main attributes :

     – id

     – class – , , ,

    • Example:

    “ " " "

    class=" com.mybeans.MyFirstBean"/>

    • This element can either build a new bean or

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 66

      .

    Tag Structure (cont’)

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    67/99

    Tag Structure (cont )

    • You must use the fully qualified class name forthe class attribute regardless of whether you use

    to import packages.

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 67

    & TagsSt t

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    68/99

    Structure

    • Accessing Properties in a bean through : –  jsp:getProperty

     –  jsp:setProperty

    Syntax:

    • This element reads and outputs the value of a

    bean property.

    • Reading a property means calling its getXxx

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 68

    method.

    & TagsSt t ( t’)

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    69/99

    Structure (cont’)

    < =" " 

    property=“propertyName" value="value"/>

    • This element modifies a bean property (i.e.,

      .

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 69

    & TagsStructure (cont’)

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    70/99

    Structure (cont )

    • JSP expression could be used to call a methodon the object with the variable specified by the id

    attribute.

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    71/99

    Structure (cont’)

    • JSP scriptlet could be used to explicitly callmethods on the bean object.

    • It is not advisable but can be useful in settin thevalue conditionally or calling methods other than

    get Xxx or set Xxx on the object.

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 71

    Example

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    72/99

    a p e

      .

    public class MyFirstBean {

     

    public String getFirstName()

    {

    return firstName;

    }public void setFirstName(String name)

    {

    = ;} }

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 72

     

     property called firstName.

    Example (cont.)

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    73/99

    p ( )

    < = “ ”

    class=“com.mybeans.MyFirstBean” />

    The First Name is < s : etPro ertname=“myBean” property=“firstName” />

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 73

    Example (cont.)

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    74/99

    p ( )

    • Note You can access the bean methods usingscripting tags

    < r ng name = ;

    myBean.setFirstName (name);

    The First Name is

    • Note: Whenever possible, avoid mixing the XML-

    compatible jsp:useBean tags with JSP scripting elements

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 74

    con a n ng exp c ava co e.

    How to use a request parameter to feed thevalue of a bean variable ?

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    75/99

    value of a bean variable ?

    Consider this case:

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    76/99

    value of a bean variable ? (cont )

    • JSP offers a better solution:

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    77/99

    value of a bean variable ? (cont )

    • If the request parameter name and the bean

    could be omittedExam le:

    If the request parameter has the name isbn

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 77

    Scope of the Bean

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    78/99

    p

    • request• sess on

    • application

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 78

    Scope of the Bean (cont’)

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    79/99

    p ( )

    1. page:

     – The default value.

     – The bean is bound to a local variable (accessible by.

     – The bean object is placed in the PageContext object

    for the duration of the current request. – The code can access it by calling getAttribute on the

    predefined pageContext variable of the page.

     – e ean s no s are an us a new ean w ecreated for each request.

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 79

    Scope of the Bean (cont’)

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    80/99

    p ( )

    2. request:

     – .

     – The bean object is placed in the HttpServletRequestobject for the duration of the current request.

     – The servlet code can access it by calling getAttribute

    on the request variable. – o e a wo pages or a page an a serv e

    share request objects jsp:include, jsp:forward or the

    include or forward methods of RequestDispatcher is

    used.

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 80

    Scope of the Bean (cont’)

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    81/99

    3. session:

     – The bean is bound to a local variable.

     – The bean object will be stored in the HttpSessiono ec assoc a e w e curren reques .

     – The servlet code can access it by calling getAttribute

    on the session variable.

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 81

    Scope of the Bean (cont’)

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    82/99

    4. application:

     – e ean s oun o a oca var a e.

     – The bean object will be stored in the ServletContextavailable throu h the redefined a lication variable 

    or by a call to getServletContext.

     – The ServletContext is shared by all servlets and JSPpages n e e app ca on.

     – Values in the ServletContext can be retrieved with the

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 82

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    83/99

     

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute 83

    Assignment

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    84/99

    • Write a jsp page that populates a Java bean with

     jsp page or servlet that displays the data.

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 84

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    85/99

     

    MVC Frameworks

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute 85

    Chapter 4 Outline

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    86/99

    MVC Design Pattern.

     

    MVC Frameworks.

    mp emen ng w eques spa c er.

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 86

    MVC Design Pattern

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    87/99

    • Model-View-Controller (MVC) is an architecturaldesign pattern used to build complex computer

    applications.

    • It aims to separate data (model) and usern er ace v ew .

    • The MVC architecture divides applications intoree ayers :

    • Model: The data and business logic

     • Controller: The flow control

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 87

    Why MVC?

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    88/99

    • Why MVC?

    .

    • Business logic and presentation are separated.• Flexibilit : chan e in one la er does not affect

    the other.

    2. Ease of maintenance.

    . u y, , …

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 88

    MVC Frameworks

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    89/99

    • The 4th strategy for invoking dynamic code.

    • The ori inal re uest is handled b a servlet.

    • The servlet invokes the business-logic and data-access

    code and creates beans to represent the results , decidesw c page presen ose resu s an orwar s e

    request to it.

    • This servlet is the Controller .

    • The beans representing the results are the Model.

    • The jsp that presents the results are the View.

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 89

    • The most popular MVC framework is Apache Struts.

    MVC Frameworks (cont’)

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    90/99

    Controller 1-request

    Client 3

    2

    browser 

    View

    (JSP)5-response 4

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 90

     

    Steps of Implementing MVC

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    91/99

    1. Define beans to represent the data.

    . .

    3. The servlet invokes business logic or data-access code

    to obtain the results. The results are placed in the

    beans (Populate the beans).

    4. Store the bean in the request, session, or servlet

    .

    5. Forward the request to a JSP page (using forward

    method of RequestDispatcher)

    6. Extract the data from the beans. The JSP page

    accesses beans with jsp:useBean and jsp:getProperty

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 91

    .

    Notes on the Steps of Implementing MVC

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    92/99

    • The Java beans must have a default constructor since aservlet or other Java routine (never a JSP page) will

    .

    • A controller servlet handles the initial re uest it can read 

    request parameters and request headers.

    • The controller servlet can also use the populateBean

    method of Jakarta Commons BeanUtils component to

    parameters.

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 92

    Notes on the Steps of Implementing MVC (cont’)

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    93/99

    • Jakarta Commons BeanUtils component performs thereflection and the type conversion.

    • You can download this component fromp: commons.apac e.org componen s. m

    •  

     – List the JAR files in your CLASSPATH for development.

     – Put the JAR files in the WEB-INF/lib directory of your Web

    , .

    • To share JAR files across Web applications, put JAR

    files in /common/lib.

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 93

    Example on using BeanUtils component

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    94/99

    import org.apache.commons.beanutils.BeanUtils;

    public class MyRequestServlet extends HttpServlet

    public void doGet(HttpServletRequest request,

    HttpServletResponse response) throws ServletException,

    PrintWriter out=response.getWriter();

    MyBean bean=new MyBean();ry

    BeanUtils.populate (bean, request.getParameterMap());

    }catch(Exception e)

    {

    e.printStackTrace();

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 94

     

    } }

    Notes on the Steps of Implementing MVC (cont’)

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    95/99

    • The Servlet can then call some business logic code,invoke an Enterprise JavaBeans component, or query a

    a a ase o e erm ne e resu s an n e va ue

    object beans.

    • A servlet can store data for JSP pages in three main

    places: in the HttpServletRequest, in the HttpSession,an n e erv e on ex .

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 95

    Notes on the Steps of Implementing MVC (cont’)

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    96/99

    ean ec ean = new ean ec ... ;

    request.setAttribute ("key", bean );

    BeanObject bean = new BeanObject (...);

    getServletContext().setAttribute("key", bean );

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 96

    sp:use ean = ey ype= my ac age. ean ec

    scope= “application” />

    Notes on the Steps of Implementing MVC (cont’)

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    97/99

    • You can place your jsp pages under the WEB-INF

    directory to prevent clients from accessing them directly

    .

    • Using forward method of RequestDispatcher, makes the

    client sees the URL of the original servlet, not the URL of

    the final JSP page.

    • If the destination page uses relative URLs for images or

    style sheets, it needs to make them relative to the servlet

    URL or the server root, not to the destination page’s

    actual location.

    " ”

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 97

    . . - .

    TYPE="text/css">

    Project

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    98/99

    • After the user correctly log in to the Application,a specified home page is displayed according to

    the priviliges of the user.

    • Only the managers can add, edit or delete

    another employee.

    • Implement the MVC architecture and avoid usingdirect java code in the jsp pages as much as you

    can.

    JavaTM Education & Technology Services

    Copyright© Information Technology Institute   http://jets.iti.gov.eg 98

    References & Recommended Reading

  • 8/17/2019 Developing Web Applications Using Java Server Pages (JSP) Final

    99/99

    • Core Servlets and JSP.

    • ava or e e w erv e s, , an .

    • Sun presentations.• Oracle presentations.

    • SCJWD study guide.