Unit-7 JSP, RMI [Compatibility Mode]

download Unit-7 JSP, RMI [Compatibility Mode]

of 51

  • date post

    30-Oct-2014
  • Category

    Documents

  • view

    29
  • download

    0

Embed Size (px)

Transcript of Unit-7 JSP, RMI [Compatibility Mode]

Java Server Pages

Shreenath Acharya St. Joseph Engg. College, Mangalore

1

Java Server pages(JSP) is a server side program that is similar in design and functionality to a Java servlet. JSP is called by a client to provide a Web Service whose nature depends upon the J2EE application. A JSP processes the request by using logic built into the JSP or by calling other web components built using Java Servlet technology or Enterprise Java Bean (EJB) technology. Once a request is processed, the JSP responds by sending the results to the client. JSP is written in HTML, XML or in the clients format that is interspersed with scripting elements, directives and actions comprised of Java Programming language and JSP syntax.

Shreenath Acharya St. Joseph Engg. College, Mangalore

2

JSP JSP is simpler to create than a Java servlet since it is written in HTML rather than Java programming language. JSP offers the same features like Servlets, because a JSP is converted to a Java Servlet the first time that a client requests the JSP. The three methods that are automatically called when a JSP is requested and when a JSP is terminated are: i). jspInit( ): It is identical to init( ) method in Java Servlet and an applet. It is called first when the JSP is requested and is used to initialize objects and variables that are used throughout the life cycle of the JSP. ii). jspDestroy( ): It is identical to the destroy( ) method in Java Servlet and an applet. It is called automatically when the JSP terminates. It is not called when the JSP abruptly terminates such as server crash. It is used for cleanup to release the resources used during the execution of the JSP such as disconnecting from a database. iii). service( ): It is automatically called and retrieves a connection to HTTP.Shreenath Acharya St. Joseph Engg. College, Mangalore 3

Installation Once a JSP is created it should be placed in the same directory as HTML pages(unlike servlet which requires to be placed in a separate directory). Referencing a JSP does not require to set the CLASSPATH. The three factors to be considered while installing JSP are: 1). Web services called by a JSP must be installed properly. For ex: A Java servlet called by a JSP must be placed in the designated directory for Java servlets and referenced on the CLASSPATH. The development environment used to create the J2EE application determines the designated directory. 2). Avoid placing the JSP in the WEB-INF or META-INF directories. The development environment prohibits it. 3). The directory name used to store a JSP must not have the same name as the prefix of the URL of the web application.Shreenath Acharya St. Joseph Engg. College, Mangalore 4

JSP Tags A JSP program consists of a combination of HTML tags and JSP tags. JSP tags define Java code to be executed before the output of the JSP program is sent to the browser. A JSP tag begins with a . Another version of JSP tags known as Extendable Markup Language(XML) are formatted as . JSP tags are embedded into the HTML component of a JSP program and are processed by a JSP virtual engine such as Tomcat. Tomcat reads the JSP program whenever the program is called by a browser and resolves JSP tags, then sends the HTML tags and related information to the browser. The Java code associated with JSP tags in the JSP program is executed when encountered by Tomcat, and the result of that process is sent to the browser. The browser knows how to display the result since the JSP tag is enclosed within an open and closed HTML tag.Shreenath Acharya St. Joseph Engg. College, Mangalore

5

Five Types of JSP tags1. Comment tag: It opens with It is followed by a comment that describes the functionality of statements that follow the comment tag. 2. Declaration statement tags: It opens with 3. Directive tags: It opens with

Shreenath Acharya St. Joseph Engg. College, Mangalore

6

The three commonly used directive tags are: import, include and taglib. 1. import tag is used to import Java packages into the JSP program. 2. include tag inserts a specified file into the JSP program replacing the include tag. 3. taglib tag specifies a file that contains a tag library. Example: // import tag imports the package //include tag includes the file //taglib tag - loads myTags.tld library 4. Expression tags: It opens with 5. Scriptlet tags: It opens with Shreenath Acharya St. Joseph Engg. College, Mangalore 7

Variables and Objects In a JSP program the declaration statement must appear as a JSP tag within the JSP program before the variable or object is used in the program.

Example: JSP Programming Your age is :

Shreenath Acharya St. Joseph Engg. College, Mangalore

8

Example Explanation: The declaration tells the JSP virtual engine to make statements contained in the tag variable available to other JSP tags in the program. It needs to be done every time when we declare variables or objects in our program unless they are only to be used within the JSP tag where they are declared.

The variable age is used in the expression tag inside a HTML paragraph as, Your age is : The JSP virtual engine resolves the JSP expression before sending the output of the JSP program to the browser. ie, the JSP tag is replaced with its value 29 before sending the information to the browser.

Shreenath Acharya St. Joseph Engg. College, Mangalore

9

Multiple statements could be placed within a JSP tag by extending the close JSP tag to another line in the JSP program.

Example: JSP Programming

Shreenath Acharya St. Joseph Engg. College, Mangalore

10

Apart from variables, we will also be able to declare objects, arrays and Java collections within a JSP tag using similar techniques as in a Java program.

Example - to show how to declare an object and an array: JSP Programming Here the JSP program creates three String objects, the first two declarations implicitly allocate memory and the third, explicitly allocates memory. In addition, it creates arrays and a Vector.Shreenath Acharya St. Joseph Engg. College, Mangalore 11

Methods In a JSP program a method is defined similar to a method definition in a Java program except that the method definition must be placed within a JSP tag. The methods could be called from within the JSP tag once it is defined.

Example: JSP Programming Your curved grade is: Shreenath Acharya St. Joseph Engg. College, Mangalore 12

In the previous example the method is called from within an HTML paragraph. Although any appropriate tag can be used to call the method, technically the method is called from within the JSP tag that is enclosed within the HTML paragraph tag. The JSP tag that calls the method must be a JSP expression tag, which begins with You passed! Better luck next time.

Shreenath Acharya St. Joseph Engg. College, Mangalore

15

Your final grade is A case 80: } %> Your final grade is B case 70: } %> Your final grade is C case 60: } %> Your final grade is F Shreenath Acharya St. Joseph Engg. College, Mangalore 16

Loops - Example JSP loops are identical to loops that we use in our Java program except that, we can repeat HTML tags and related information multiple times within our JSP program without having to enter the additional HTML tags. The three kinds of loops used are: for loop, while loop and do while loop. The following example creates the same table using the above three kinds of looping constructs. Here the JSP program initially declares and initializes an array and an integer, and then begins to create the first table. There are two rows in each table. The first row contains three column headings that are hard coded into the JSP program. The second row also contains three columns each of which is a value of an element of the array.

JSP Programming Shreenath Acharya St. Joseph Engg. College, Mangalore 17

First Second Third