Use Constructor in JSP
This section illustrates you how to use constructors in jsp.
Constructors are used to initialize the object. They are just like method declaration but they do not return value. Constructors are defined by their class name. Here is an example which explains you to use constructors in jsp.
Here is the code of jspConstructor.jsp
<title>Use Constructor in jsp</title>
<H1>Using Constructor in jsp</H1>
X() throws java.io.IOException
con.println("<b>Area and Perimeter of Square</b><br>");
class Y extends X
Y(int side) throws java.io.IOException
con.println("Area of Square=");
con.println("Perimeter of Square=");
con = out;
Y obj = new Y(4);
In the above example, we are calculating the Area and Perimeter of Square. For this, class X is defined and variables are initialized here. It invokes its constructor X(). Another class Y is defined which extends the class X and invokes a constructor Y(int side). This constructor calls the super class X by super() and define area and perimeter. Create the object of class Y and pass the value 4. It will take side=4 and calculate the result by area=side*side and perimeter=4*side.
Here is the output.
If you enjoyed this post then why not add us on Google+? Add us to your Circles
Liked it! Share this Tutorial