Friday, March 27, 2015

class in JSP

A brief about class in JSP

Class can be refereed as a blueprint from which individual objects are created in Java. Class contains any of variable types  and  methods which are members of the class. It can contain any number of methods for accessing the values of various kinds of methods.

  1. Local Variables :  These are variables defined inside methods, construct or blocks are called local variables. These variables will be declared and initialized within the method and the variables will be destroyed when the methods has completed.
  2. Instance variables: Instance variables are those variables  which are within a class but outside any methods. These variables are instantiated when the class is loaded. Instance variables can be accessed from inside any method, constructor or blocks of that particular class.
  3. Class variables: These variables are variables declared with in a class, outside any method, with the static keyword.


Example:
JSP File (index.jsp)

 <!DOCTYPE html>  
 <html>  
   <head>  
     <title>nirajanghimireyworkshop.blogspot.com</title>  
   </head>  
   <body>  
     <%!  
       javax.servlet.jsp.JspWriter localOut;  
       class BaseClass  
       {  
         public void start() throws java.io.IOException  
         {  
           localOut.println("Starting...<br/>");  
         }  
       }  
       class DerivedClass extends BaseClass  
       {  
         public void drive() throws java.io.IOException  
         {  
           localOut.println("Deriving...<br/>");  
         }  
       }  
     %>    
     <%  
       localOut = out;    
       out.println("Example of Classes <br/>");  
       DerivedClass a = new DerivedClass();  
       a.start();  
       a.drive();  
     %>  
   </body>  
 </html> 

No comments:

Post a Comment