"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.
- HTML file, GoldSilver.html, which takes gold and silver weights from the client and calls GoldShop.jsp to estimate the cost.
- 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).
- 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
<% // dynamic content application.setAttribute("stdgoldrate", "296"); application.setAttribute("stdsilverrate", new Integer(46)); %>
Standard gold and silver rates are set. Any JSP can use.<%-- static HTML content --%>
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.
application.setAttribute("stdgoldrate", "296"); application.setAttribute("stdsilverrate", new Integer(46));
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
Abids, Hyderabad<% // extracting gold and silver weights from client HTML String goldstr = request.getParameter("goldbox"); String silverstr = request.getParameter("silverbox"); // parse the strings to data types double goldweight = Double.parseDouble(goldstr); double silverweight = Double.parseDouble(silverstr); /* extract standard gold and silver rates from */ Object obj1 = application.getAttribute("stdgoldrate"); // application object set earlier by Association.jsp String s1 = (String) obj1; int stdgold = Integer.parseInt(s1); Object obj2 = application.getAttribute("stdsilverrate"); Integer i1 = (Integer) obj2; int stdsilver = i1.intValue(); %> <%-- Prepare and send response using Expression --%> Weight of gold ornament: <%= goldweight %> gms. and its cost is Rs.<%= goldweight*stdgold %>. It is 22 ct. gold.
Weight of silver article: <%= silverweight +"gms." %> and its cost is <%= "Rs." + silverweight*stdsilver %>. It is pure sivler.
Total cost Rs. <% out.println((goldweight*stdgold) + (silverweight*stdsilver)); %> with no tax included.
It is your shop. Please visit again. Get discounts and gifts for coming Pongal festival.
Let us see the code.
String goldstr = request.getParameter("goldbox"); String silverstr = request.getParameter("silverbox"); double goldweight = Double.parseDouble(goldstr); double silverweight = Double.parseDouble(silverstr);
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.
Object obj1 = application.getAttribute("stdgoldrate"); String s1 = (String) obj1; int stdgold = Integer.parseInt(s1); Object obj2 = application.getAttribute("stdsilverrate"); Integer i1 = (Integer) obj2; int stdsilver = i1.intValue();
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
HTML Form when some data is entered.
Response of GoldShop.jsp