jsp2

Upload: achutha795830

Post on 08-Apr-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/7/2019 JSP2

    1/8

    Methods: A Method is defined similar to how a method is defined ina given java program expect the method definition is placedwithin a jsp tag.

    We can call the method from with in the jsp tag once themethod is defined, below example how this is done. In this

    example, the method is passed a student grade and thenapplies a curve before returning the curved grade.

    The method is called from within an HTML paragraph tagin this program. The jsp tag that calls the method must be ajsp expression tag, which begins with

  • 8/7/2019 JSP2

    2/8

    JSP Programming

    your curved grade is:

  • 8/7/2019 JSP2

    3/8

    Control Statements:

    There are two control statements used tochange the flow of a jsp program. These are if statement

    switch statement If statement evaluates a condition statement to

    determine if one or more lines of code are to beexecuted or skipped.

    Switch statement compares a value with one ormore other values associated with a casestatement. The code segment that is associatedwith the matching case statement is executed.Code segments associated with other casestatements are ignored.

  • 8/7/2019 JSP2

    4/8

    Below programs shows how to intertwine HTML tags and jsp tagsalter the flow of the jsp program.

    The if statement requires three jsp tags. The first contains thebeginning of the if statement, including the conditional expression.The second contains the else statement and the third has the closed

    French brace used to terminate the else block. Two HTML paragraph tags contain information that the browser

    displays, depending on the evaluation of the conditional expressionin the if statement only one of the HTML paragraph tags and relatedinformation are sent to the browser.

    The switch statement also divided into several jsp tags becauseeach case statement requires are HTML paragraph tag and relatedinformation associated the if statement only one HTML paragraphtag and related information associated with a case statement thatmatches the switch value are returned to the browser.

  • 8/7/2019 JSP2

    5/8

    Example:

    JSP Programming

    69)}%>

    you passed!

    Switch (grade)

    {

    case 90: %>

    your final grade is a A

    case 80: %>

    your final grade is a B

    case 70: %>

    your final grade is a C

    case 60: %>

    your final grade is a F

    }

  • 8/7/2019 JSP2

    6/8

    Loops:

    JSP loops are nearly identical to loops that you use in your javaprogram except you can repeat HTML tags and related informationmultiple times within your jsp program without having to enter theadditional HTML tags.

    There are three kinds of loops commonly used in a jsp program,

    these are The for loop

    The while loop

    The do......while loop

    The for loop repeats, usually a specified number of times althoughwe can create an endless for loop.

    The while loop executes continuously as long as a specifiedcondition remain true. However, the while loop may not executebecause the condition may never be true.

    The do.......while loop executes at least once; afterwards theconditional expression in the do....while is evaluated to determine ifthe loop should be executed another time

  • 8/7/2019 JSP2

    7/8

    Below program shows a similar routine used to populate threeHTML tables with values assigned to an array.

    All the tables appear the same, although a different loop is used tocreate each table. The jsp program initially declares and initializesan array and an integer and then begins to create the first table.

    There are two rows in each table. The first row contains threecolumn heading that are hard coded into the program. The secondrow also contains three columns each of which is a value of anelement of the array.

    The first table is created using the for loop. The opening table rowtag is entered into the program before the for loop begins.

    A pair of HTML table data cell tags are placed inside the forloop along with a jsp tag that contains an element of the array. Thejsp tag resolves to the value of the array element by the jsp virtualprogram.

  • 8/7/2019 JSP2

    8/8

    The close table row tag and the close tag are inserted into the program following the French brace that closes the for loopblock. These tags terminate the construction of the table.

    JSP Programming

    First Second Third