Using Protected Access in JSP

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

Using Protected Access in JSP

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.