meljun_cortes_jedi slides web programming chapter04 basic jsps

Upload: meljun-cortes-mbampa

Post on 06-Apr-2018

226 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    1/63

    Basic JSPs

    Web Programming

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    2/63

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    3/63

    What is JSP?

    3Web Programming

    Java Server Pages

    A servlet-based technology used in the web tier to presentboth dynamic and static content.

    Text-based and contains mostly HTML template textintermixed with tags specifying dynamic content.

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    4/63

    Why JSP?

    4Web Programming

    Since JSPs are text documents much like HTML,developers avoid having to format and manipulate apossibly very long String to produce output. The HTMLcontent is now not embedded within a lot of Java code. This

    makes it easier to maintain. JSPs are immediately familiar to anyone with knowledge of

    HTML, with only the dynamic markup to learn. This makes itpossible for dedicated site designers to create the HTMLtemplate of the site, with developers processing it later toinclude the tags producing dynamic content. This makes forease of web page development.

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    5/63

    Why JSP?

    5Web Programming

    JSPs have built-in support for the use of reusable softwarecomponents (JavaBeans). This not only lets developersavoid possibly reinventing the wheel for each application,having support for separate software components to handle

    logic promotes separation of presentation and businesslogic.

    JSPs, as part of Java's solution to web applicationdevelopment, are inherently multi-platform and can be run inany compatible servlet container, regardless of the vendoror operating system.

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    6/63

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    7/63

    Sample JSP

    7Web Programming

    Welcome

    Greetings!

    Thank you for accessing our site.

    The time is now

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    8/63

    Output of Sample JSP

    8Web Programming

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    9/63

    Running the Sample JSP

    9Web Programming

    Using the IDE

    Assuming that a project already exists, simply place the JSP filewithin the Web Pages folder within the Project view.

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    10/63

    Running the Sample JSP

    10Web Programming

    (Using the IDE continued)

    The specific JSP page can then be run from the IDE directly bypressing Shift + F6.

    Alternatively, the web project can be packaged as a WAR file anduploaded into a server. The JSP can then be accessed by typing inthe following URL:

    http://[host]:[port]/[WEB_PROJECT_NAME]/[JSP_NAME]

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    11/63

    Running the Sample JSP

    11Web Programming

    Using an Ant build file

    The JSP can also be run by packaging it into a WAR file using abuild tool, and then deploying the WAR file into a web server.

    Following the development directory structure discussed in the earlier chapter,our JSP file should be placed in the web directory.

    After build script has executed, will contain the application in a directory structure recognized byservlet containers.

    After build script has executed, will contain the WAR file.

    Used to contain whatever documentation is used by the dev team.

    Directory under which all Java source files must be placed.

    Directory containing all static content of the app (HTML, JSP), will be the basis of the documentroot of your project.

    Directory containing configuration files such as the deployment descriptor (web.xml),tag library descriptors, etc.

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    12/63

    JSP Lifecycle

    12Web Programming

    The servlet container manages JSPs in a similar manner asit does to servlets: through the use of a well-definedlifecycle.

    JSPs have a three-phase lifecycle:

    Initialization

    Service

    Destruction

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    13/63

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    14/63

    14Web Programming

    package org.apache.jsp;

    import javax.servlet.*;import javax.servlet.http.*;import javax.servlet.jsp.*;

    public final class index_jsp extends org.apache.jasper.runtime.HttpJspBaseimplements org.apache.jasper.runtime.JspSourceDependent {

    private static java.util.Vector _jspx_dependants;

    public java.util.List getDependants() {return _jspx_dependants;

    }

    public void _jspService(HttpServletRequest request, HttpServletResponse response)throws java.io.IOException, ServletException {

    JspFactory _jspxFactory = null;PageContext pageContext = null;HttpSession session = null;ServletContext application = null;ServletConfig config = null;JspWriter out = null;Object page = this;JspWriter _jspx_out = null;PageContext _jspx_page_context = null;

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    15/63

    15Web Programming

    try {_jspxFactory = JspFactory.getDefaultFactory();response.setContentType("text/html");response.addHeader("X-Powered-By", "JSP/2.0");pageContext = _jspxFactory.getPageContext(this, request, response,

    null, true, 8192, true);_jspx_page_context = pageContext;application = pageContext.getServletContext();config = pageContext.getServletConfig();session = pageContext.getSession();out = pageContext.getOut();_jspx_out = out;

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

    out.write("\n");out.write(" \n");out.write(" Greetings!
    \n");out.write("\n");out.write(" Thank you for accessing our site.
    \n");out.write("\n");out.write(" The time is now ");out.print( new java.util.Date());out.write("\n");out.write("\n");

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    16/63

    16Web Programming

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

    }catch (Throwable t) {if (!(t instanceof SkipPageException)){

    out = _jspx_out;if (out != null && out.getBufferSize() != 0)

    out.clearBuffer();if (_jspx_page_context != null)

    _jspx_page_context.handlePageException(t);}

    } finally {if (_jspxFactory != null)

    _jspxFactory.releasePageContext(_jspx_page_context);}

    }}

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    17/63

    JSP and Equivalent Servlet

    17Web Programming

    It is not important to understand the code given.

    What is important here is to see that JSPs are handled justlike Servlets, even if it is not immediately obvious.

    JSPs vs servlets: JSPs are more text-oriented.

    Servlets allow the developer greater freedom with Java code.

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    18/63

    JSP Syntax and Semantics

    18Web Programming

    Elements and Template Data

    Components of all JavaServer Pages can be divided into twogeneral categories:

    Elements: dynamically produced information

    Template data: static information that takes care of presentation

    Hello World!

    Hello World! It's

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    19/63

    JSP Syntax and Semantics

    19Web Programming

    (Elements and Template Data continued)

    Two Types of Syntax:

    JSP Style: designed to be easier to the author

    XML Style: normal syntax modified to become XML-compliant and preferred

    when using JSP authoring tools

    Choosing one syntax format over the other is just a matter ofpreference and standardization.

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    20/63

    JSP Syntax and Semantics

    20Web Programming

    Scripting Elements

    JSPs may be viewed as HTML or XML documents with embeddedJSP scripts. JSP scripting elements allow you to insert Java codeinto the Servlet that will be generated from the JSP page.

    The simplest way to make a JSP dynamic is by directly embeddingscripting elements into template data.

    JSP scripting elements:

    Scriptlets

    Expressions

    Declarations

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    21/63

    Scriptlets

    21Web Programming

    Provide a way to directly insert bits of Java code in betweenchunks of template data:

    Just the same as writing normal Java code except that there is no

    need for a class declaration.

    If you want to use the characters "%>" inside a scriptlet, write "%\>"instead.

    Useful for embedding simple java codes

    No specific limit as to the complexity of Java codes Overusing scriptlets:

    Putting too heavy computation inside scriptlets is a code maintainability issue.

    Using scriptlets extensively violates JSPs role of being primarily a presentationlayer component.

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    22/63

    Scriptlets

    22Web Programming

    Simple println

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    23/63

    Scriptlets

    23Web Programming

    For-Loop in a scriplet

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    24/63

    Scriptlets

    24Web Programming

    Output of the For-Loop scriptlet

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    25/63

    Scriptlets

    25Web Programming

    (Output continued)

    Take note that the scriptlet itself is not sent to the client but only itsoutput. Try viewing the source of the jsp output you've produced inyour browser to understand this point. All you should see are HTML

    tags plus the scriptlet output and minus the scriptlet. The XML-compatible syntax for writing scriptlets is:

    Java code;.

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    26/63

    Expressions

    26Web Programming

    Provide a way to insert Java values directly into the output

    It is actually short for out.println().

    Notice that a semicolon ( ; ) does not appear at the end of the codeinside the tag.

    Evaluated at run-time, converted to a string, and inserted in thepage.

    Since it is evaluated at run-time, expressions have full

    access to information about the request.

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    27/63

    Expressions

    27Web Programming

    A number of predefined variables, called implicit objects, aremade available to JSP authors to simplify expressions. Themost important ones are:

    request: the HttpServletRequest;

    response: the HttpServletResponse;

    session: the HttpSession associated with the request (if any)

    out: the PrintWriter (a buffered version of type JspWriter) used tosend output to the client

    Example:Hostname:

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    28/63

    Expressions

    28Web Programming

    XML-compatible syntax:

    Java Expression

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    29/63

    Declarations

    29Web Programming

    Allow you to define methods or variables.

    Used to embed code just like scriptlets but declarations getinserted into the main body of the servlet class, outside ofthe _jspService() method processing the request.

    Advantage

    Code embedded in a declaration can be used to declare new methods and globalclass variables.

    Disadvantage

    Code in declarations are not thread-safe.

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    30/63

    Declarations

    30Web Programming

    Reminders in using the declaration tag:

    Before the declaration you must have .

    Code placed in this tag must end in a semicolon ( ; ). Declarations do not generate output but are used with JSP

    expressions or scriptlets.

    Since declarations do not generate any output, they arenormally used in conjunction with JSP expressions or

    scriptlets.

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    31/63

    Declarations

    31Web Programming

    Example: AccessCountDeclaration.jsp

    This JSP is able to print out the number of visits by declaring aclass-wide variable accessCount, using a scriptlet to increment thevalue of the number of page visits and an expression to display thevalue.

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    32/63

    Declarations

    32Web Programming

    Sample output of AccessCountDeclaration.jsp refreshed 4times

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    33/63

    33Web Programming

    AccessCountDeclaration_jsp.java

    package org.apache.jsp.JSP;

    import javax.servlet.*;import javax.servlet.http.*;import javax.servlet.jsp.*;

    public final class AccessCountDeclaration_jspextends org.apache.jasper.runtime.HttpJspBaseimplements org.apache.jasper.runtime.JspSourceDependent {

    private int accessCount = 0;

    private static java.util.Vector _jspx_dependants;

    public java.util.List getDependants() {return _jspx_dependants;

    }

    public void _jspService (HttpServletRequest request, HttpServletResponse response)throws java.io.IOException, ServletException {

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    34/63

    34Web Programming

    JspFactory _jspxFactory = null;PageContext pageContext = null;HttpSession session = null;ServletContext application = null;ServletConfig config = null;JspWriter out = null;Object page = this;JspWriter _jspx_out = null;PageContext _jspx_page_context = null;

    try {

    _jspxFactory = JspFactory.getDefaultFactory();response.setContentType("text/html");pageContext = _jspxFactory.getPageContext(this, request, response, null,

    true, 8192, true);

    _jspx_page_context = pageContext;application = pageContext.getServletContext();config = pageContext.getServletConfig();

    session = pageContext.getSession();

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    35/63

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    36/63

    36Web Programming

    if (_jspx_page_context != null)

    _jspx_page_context.handlePageException(t);

    }

    } finally {if (_jspxFactory != null)

    _jspxFactory.releasePageContext(_jspx_page_context);

    }

    }

    }

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    37/63

    Declarations

    37Web Programming

    XML-compatible syntax:

    Java Code;

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    38/63

    Template Text

    38Web Programming

    Use

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    39/63

    Predefined Variables

    39Web Programming

    JSP implicit objects are automatically declared by the JSPcontainer and are always available for use in expressionsand scriptlets.

    request: The instance of the javax.servlet.http.HttpServletRequest

    object associated with request of the client. response: The instance of the

    javax.servlet.http.HttpServletResponse object associated withresponse to the client.

    pageContext: The PageContext object associated with current page.

    out: References the javax.servlet.jsp.JspWriter object which may beused to write actions and template data in a JSP page, similar to thatof the PrintWriter object we used in the servlet discussion. Theimplicit variable out is initialized automatically using methods in thePageContext object.

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    40/63

    Predefined Variables

    40Web Programming

    (JSP implicit objects continued)

    session: An instance of a javax.servlet.http.HttpSession object. It isequivalent to calling the HttpServletRequest.getSession() method.

    application: The ServletContext is an instance of the

    javax.servlet.ServletContext object. It is equivalent to callinggetServletConfig().getContext() method. This implicit object isshared by all servlets and JSP pages on the server.

    config: The config implicit object is an instance of thejavax.servlet.ServletConfig object for this page. Same as Servlets,JSPs have access to the parameters initially defined in the Web

    Application Deployment Descriptor.

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    41/63

    JSP Directives

    41Web Programming

    Messages to a JSP container

    Affect the overall structure of the servlet class

    Note: Whitespaces after are optional.

    Do not produce any visible output when the page isrequested but they change the way the JSP Engineprocesses the page.

    Give special information about the page to the JSP Engine.

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    42/63

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    43/63

    Page Directive

    43Web Programming

    Defines the processing information for this page.

    Allows you to import classes, customize the servletsuperclass, and the like.

    Note that attributes are case-sensitive.

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    44/63

    44

    Web Programming

    Directive Description Sample Usage

    extends Superclass used by the JSPengine for the translatedServlet. Analogous to theextends keyword in the Javaprogramming language.

    language Indicates which scriptinglanguage the scriptlets,expressions and declarationsfound in the JSP page uses.The only defined value for thisattribute is java.

    import Import the classes in a javapackage into the current JSP

    page.

    session Indicates whether the pagemakes use of sessions. Bydefault all JSP pages havesession data available.Switching session to false hasperformance benefits.

    Default is set to true.

    buffer Controls the use of bufferedoutput for a JSP page. Ifvalues is none, then there isno buffering and output is

    written directly to theappropriate PrintWriter.Default value of buffer is 8kb.

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    45/63

    45

    Web Programming

    autoFlush When set to true, flushes theoutput buffer when it is full.

    isThreadSafe Indicates if the generated

    Servlet deals with multiple

    requests. If true, a new

    thread is started to handle

    requests simultaneously.

    Default value is true.

    info Developer uses info attribute

    to add information/document

    for a page. Typically this is

    used to add author, version,copyright and date info.

    errorPage Defines the page that deals

    with errors. Must be URL to

    error page.

    IsErrorPage A flag that is set to true to

    make a JSP page a specialError Page. This page has

    access to the implicit objectexception

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    46/63

    Page Directive

    46

    Web Programming

    XML syntax:

    For example, the XML equivalent of:

    is

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    47/63

    Include Directive

    47

    Web Programming

    Defines the files to be included in the page.

    Lets you insert a file into the servlet class during translationtime.

    Typically used for navigation, tables, headers and footers,etc.

    Components that are common to multiple pages.

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    48/63

    Tag Lib Directive

    48

    Web Programming

    A collection of custom tags

    Defines the tag library to be used in the page

    Tells the container which markup in the page should beconsidered custom code and what code the markup links to

    Custom tags were introduced in JSP 1.1 and allowed JSPdevelopers to hide complex server side code from webdesigners.

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    49/63

    49

    Web Programming

    index.jsp

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    50/63

    JavaBeans in JSP

    50

    Web Programming

    The use of JavaBeans are not mandated in the JSPspecification.

    Provide useful functionality in that usage of JavaBeans cangreatly reduce the amount of scripting elements to be found

    in a Java page.

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    51/63

    JavaBeans

    51

    Web Programming

    Simple Java classes adhering to a certain coding standard:

    Provides a default, no-argument constructor

    Provides get and set methods for properties it wishes to expose

    package jedi.beans;

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    52/63

    52Web Programming

    public class User {private String name;private String address;

    private String contactNo;

    public User() {}

    public String getName() {return name;

    }

    public void setName(String name) {this.name = name;

    }

    public String getAddress() {return address;

    }

    public void setAddress(String address) {this.address = address;

    }

    bli St i tC t tN () {

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    53/63

    53Web Programming

    public String getContactNo() {return contactNo;

    }

    public void setContactNo(String contactNo) {this.contactNo = contactNo;}

    }

    How are JavaBeans used in

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    54/63

    How are JavaBeans used inJSP?

    54Web Programming

    As a Data Transfer Object

    JavaBeans are more widely used in JSPs as objects that transferdata from another source. In most applications, actual processing is

    done in a servlet, not in a JSP. Only the results of the passing arepassed on to the JSP in the form of one or more JavaBeans.

    As a Helper object

    In some small applications, it is not efficient to have a separateservlet to process the data. In this case, it is better to place the

    functionality inside JavaBeans which can be accessed by the JSP.

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    55/63

    j B

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    56/63

    56Web Programming

    To use a JavaBeans component, the first thing to do is toenable the use of a bean within the page throughinstantiation, which we can do with this action.

    Attributes:

    id: This attribute specifies the name of the bean and how you willrefer to it in the page.

    scope : This attribute specifies the scope in which you want to storethe bean instance. It can be set to page (the default), session,request, or application.

    class: This attribute specifies the Java class that the bean is drawnfrom. If you have specified beanName, you do not have to specifyclass.

    j B

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    57/63

    57Web Programming

    (Attributes continued)

    beanName: This attribute specifies the name of a bean that is storedon the server. You refer to it as you would a class (for example,com.projectalpha.PowerBean). If you have specified class, you donot need to specify beanName.

    type: This attribute specifies the type of scripting variable returnedby the bean. The type must relate to the class of the bean.

    j B

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    58/63

    58Web Programming

    Sample Usage:

    When the page encounters a useBean action, it first tries to see ifthere is already a JavaBean instance of the given type located in thegiven scope.

    If there is, the page will make use of that bean instance.

    If not, the container will automatically create a new bean instance using thebean's default no-argument constructor and place that bean in the given scope.

    If the above functionality were to be expressed as a scriptlet, itwould look like this:

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    59/63

    59Web Programming

    Once a bean has been enabled using the jsp:useBeanaction, it can be used inside the JSP page like any otherobject instance using the name specified in the id attribute.

    Example

    Hello !!

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    60/63

    60Web Programming

    Retrieves the value of a property inside a specifiedJavaBean and outputs it immediately to the responsestream.

    Has two attributes:

    name: The name of the JavaBean whose property is to be retrieved.This must have the same value as the id attribute used in an earlier action.

    property: The name of the property whose value will be retrieved.

    If the developer wishes to be able to retrieve the value of aJavaBean property without immediately placing it in theoutput stream, there is no choice but to make use ofscriptlets or EL.

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    61/63

    61Web Programming

    Allows developers to set the properties of a given JavaBeanwithout the developer having to write a line of scriptlet code.

    Has the same attributes with the getProperty action, with theaddition of two more:

    value: The value to be set into the property. This can be either astatic value or an expression that can be evaluated at runtime.

    param: Specifies the request parameter from which the property willretrieve its value.

    Developers making use of this action must specify one of

    either value or param attribute. Including both in the actionwill cause an exception to be thrown.

    Error Handling

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    62/63

    Error Handling

    62Web Programming

    JSPs can make use of the page directive to specify a pagethat will handle any uncaught exceptions.

    The errorPage attribute of the page directive can be passed arelative URL to the JSP page designated as an error page.

    The page thus designated can set the isErrorPage attribute to true.Doing so will give them access to an implicit object named exception this contains details about the exception thrown.

    A JSP page using the page directive to describe an error page that will be used forunhandled exceptions:

  • 8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter04 Basic JSPs

    63/63

    63Web Programming

    // call a method on the null object, so that an exception will be thrown and the error pagecalled

    The length of the string is

    The designated error page:

    Error!An Error has occurred.

    Sorry, but an error has occurred with the page you were previously accessing. Please contactany member of the support team, and inform them that was the cause of the error