implicit object application JSP Example

implicit object application JSP Example


"application" is one of the 9 implicit objects, JSP supports. The "application" object represents an object of javax.servlet.ServletContext interface. This we have seen in Servlets at Reading Servlets Context Parameter with Example. Let us write a different program with JSP code.

implicit object application JSP represents a global object that can be used by all JSPs of the project. Any JSP can place values with the application object which can be used by any other JSP. There can be only one application object for the whole project. A Web project contains many individual programs like JSPs, Servlets, JavaBeans etc.

Following is an example on implicit object application JSP. This example comes with three programs.
  1. HTML file, GoldSilver.html, which takes gold and silver weights from the client and calls GoldShop.jsp to estimate the cost.
  2. JSP file, Association.jsp, which sets standard rates for gold and silver with application object that can be used by all gold shops (or all JSPs).
  3. JSP file, GoldShop.jsp, which reads actual weight of gold and silver articles from client, reads the standard values of gold and silver from application object, estimates total cost and sends the cost to client as response.

1. HTML File Name: GoldSilver.html

2. JSP file: Association.jsp

Let us see the code.

setAttribute(String str, Object obj) of application object takes two parameters, the first one being a string always and the second one should be an object of Object class or object of any derived class of Object like String, Integer etc.

Observe the first parameter is string and the second one is the object of String and Integer. With setAttribute(), Programmer can set the values with application object. These values can be accessed by any other JSP (with getAttribute() method) as in GoldShop.jsp, the next program.

3. JSP file: GoldShop.jsp

Let us see the code.

request, application and out are among the 9 implicit objects, JSP supports. The getParameter("goldbox") of request object returns the value entered by the user in the HTML goldbox as a string. The string goldstr is not useful in arithmetic operations. It is parsed to double value goldweight.

The getAttribute("stdgoldrate") of application object returns always an object of Object class. It is casted to String s1 because the value set in the Association.jsp for stdgoldrate is in string form. Again string s1 is parsed to int data type to be used in arithmetic operations.

Similarly, for obj2 is casted to Integer because stdsilverrate is associated with Integer in Association.jsp. intValue() is a method of Integer class that returns the Integer value as int data type.

Observe the syntax, response (output) is given using expressions and scriptlet in different styles. Small and individual programs are available on each Scriptlet, Expression and Declaration (separately) to understand the concept(refer JSP View All at the end this topic).

First execute Association.jsp to set the standard gold and silver rates. Then execute GoldSilver.html which calls GoldShop.jsp. You get the following screens.

When Association.jsp is executed

application1

HTML Form when some data is entered.

application2

Response of GoldShop.jsp

implicit object application JSP

Leave a Reply

Your email address will not be published. Required fields are marked *