Home Jsp Simple-jsp-example Using Protected Access in JSP
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Using Protected Access in JSP

Advertisement
In java there are three types of access specifiers: public, protected, private.

Using Protected Access in JSP

        

In java there are three types of access specifiers: public, protected, private. We always declare access specifiers to be more accessible. That means we have declared anything as a private, then we can make it either protected or public but not the other way round. The public specifier is accessible by anyone, the protected specifier can be accessed within the package only.

In this example we have created a jsp page. Inside the jsp page we have used the declaration tag. Inside this declaration tag we have made a reference of JspWriter class. JspWriter class is inside the javax.servlet.jsp package. We have defined a class Monitor inside which we have declared a method as protected.  Now make another class computer which extends Monitor, inside which we have declared one method getCPU(). Now close the declaration tag. To process the business logic now use the scriptlet directive. Inside this directive assigned the out implicit object to localOut object. Create a object of computer class. Now by the use of the reference of the object call the methods of a computer class and a Monitor class.  

The code of the program is given below:

 

<HTML>
    <HEAD>
        <TITLE>Using Protected Access in Jsp</TITLE>
    </HEAD>
    <BODY>
        <H1>Using Protected Access in Jsp</H1>
        <%!
            javax.servlet.jsp.JspWriter localOut;
            class Monitor
            {
                protected void getMonitor()  throws java.io.IOException
                {
                    localOut.println("Monitor...<BR>");
                }
            }
            class computer extends Monitor
            {
                public void getCPU() throws java.io.IOException 
                {
                    localOut.println("CPU...<BR>");
                }
            }
        %>     
        <%
            localOut = out;     
            out.println("Creating an computer...<BR>");
            computer a = new computer();
            a.getMonitor();
            a.getCPU();
        %>
    </BODY>
</HTML>

Output of the Program:


 
this example.
Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 11, 2008

Ask Questions?    Discuss: Using Protected Access in JSP  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com