Showing posts with label jsp technology. Show all posts
Showing posts with label jsp technology. Show all posts

Sunday, 18 December 2011

Initializing and Finalizing a JSP Page

You can customize the initialization process to allow the JSP page to read persistent configuration data, initialize resources, and perform any other one-time activities by overriding the jspInitmethod of the JspPage interface. You release resources using the jspDestroy method.
The bookstore example page initdestroy.jsp defines the jspInit method to retrieve or create an enterprise bean database.BookDBEJB that accesses the bookstore database;initdestroy.jsp stores a reference to the bean in bookDBEJB.
private BookDBEJB bookDBEJB;
public void jspInit() {
   bookDBEJB =
      (BookDB)getServletContext().getAttribute("bookDBEJB");
   if (bookDBEJB == null) {
      try {

         InitialContext ic = new InitialContext();
         Object objRef = ic.lookup(
            "java:comp/env/ejb/BookDBEJB");
         BookDBEJBHome home =
            (BookDBEJBHome)PortableRemoteObject.narrow(objRef,
               database.BookDBEJBHome.class);
         bookDBEJB = home.create();
         getServletContext().setAttribute("bookDBEJB",
            bookDBEJB);

      } catch (RemoteException ex) {
         System.out.println(
            "Couldn't create database bean." + ex.getMessage());
      } catch (CreateException ex) {
         System.out.println(
            "Couldn't create database bean." + ex.getMessage());
      } catch (NamingException ex) {
         System.out.println("Unable to lookup home: " +
            "java:comp/env/ejb/BookDBEJB."+ ex.getMessage());
      }
   }
}
 
When the JSP page is removed from service, the jspDestroy method releases the BookDBEJB variable:
public void jspDestroy() {
   bookDBEJB = null;
}
 
Since the enterprise bean is shared between all the JSP pages, it should be initialized when the application is started, instead of in each JSP page. Java Servlet technology provides application life cycle events and listener classes for this purpose. As an exercise, you can move the code that manages the creation of the enterprise bean to a context listener class. See Handling Servlet Life-Cycle Events for the context listener that initializes the Java Servlet version of the bookstore application.

Saturday, 17 December 2011

JSP Examples Tutorial - Using Restricted Access


<HTML>
    <HEAD>
        <TITLE>Using Restricted Access</TITLE>
    </HEAD>
    <BODY>
        <H1>Using Restricted Access</H1>
        <%!
            javax.servlet.jsp.JspWriter localOut;
            class BaseClass
            {
                protected void start()  throws java.io.IOException
                {
                    localOut.println("Starting...<BR>");
                }
            }
            class DerivedClass extends BaseClass
            {
                public void drive() throws java.io.IOException 
                {
                    localOut.println("Driving...<BR>");
                }
            }
        %>     
        <%
            localOut = out;     
            out.println("Creating an DerivedClass...<BR>");
            DerivedClass a = new DerivedClass();
            a.start();
            a.drive();
        %>
    </BODY>
</HTML>

JSP Examples Tutorial - Using Parameterized Constructors


<HTML>
    <HEAD>
        <TITLE>Using Parameterized Constructors</TITLE>
    </HEAD>
    <BODY>
        <H1>Using Parameterized Constructors</H1>
        <%!
            javax.servlet.jsp.JspWriter localOut;
            class a
            {
                a() throws java.io.IOException 
                {
                    localOut.println("In a\"s constructor...<BR>");
                }
            }
            class b extends a  
            {
                b(String s) throws java.io.IOException 
                {
                    localOut.println("In b\"s String constructor...<BR>");
                    localOut.println(s);
                }
            }
        %>     
        <%
            localOut = out;     
            b obj = new b("Hello from JSP!<BR>");
        %>
    </BODY>
</HTML>

JSP Examples Tutorial - Using Inheritance in JSP


<HTML>
    <HEAD>
        <TITLE>Using Inheritance</TITLE>
    </HEAD>
    <BODY>
        <H1>Using Inheritance</H1>
        <%!
            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("Creating an DerivedClass...<BR>");
            DerivedClass a = new DerivedClass();
            a.start();
            a.drive();
        %>
    </BODY>
</HTML>

JSP Examples Tutorial - Runtime Polymorphism


<HTML>
    <HEAD>
        <TITLE>Runtime Polymorphism</TITLE>
    </HEAD>
    <BODY>
        <H1>Runtime Polymorphism</H1>
        <%!
            javax.servlet.jsp.JspWriter localOut;
            class BaseClass
            {
                public void print() throws java.io.IOException 
                {
                    localOut.println("Hello from BaseClass...<BR>");
                }
            }
            class DerivedClass1 extends BaseClass
            {
                public void print() throws java.io.IOException 
                {
                    localOut.println("Hello from DerivedClass1...<BR>");
                }
            }
            class DerivedClass2 extends BaseClass
            {
                public void print() throws java.io.IOException 
                {
                    localOut.println("Hello from DerivedClass2...<BR>");
                }
            }
            class DerivedClass3 extends BaseClass
            {
                public void print() throws java.io.IOException 
                {
                    localOut.println("Hello from DerivedClass3...<BR>");
                }
            }
        %>     
        <%
            localOut = out;     
 
            BaseClass a1 = new BaseClass(); 
            DerivedClass1 b1 = new DerivedClass1(); 
            DerivedClass2 c1 = new DerivedClass2(); 
            DerivedClass3 d1 = new DerivedClass3(); 
 
            BaseClass baseClassVariable;
 
            baseClassVariable = a1;
            baseClassVariable.print();
 
            baseClassVariable = b1;
            baseClassVariable.print();
 
            baseClassVariable = c1;
            baseClassVariable.print();
 
            baseClassVariable = d1;
            baseClassVariable.print();
        %>
    </BODY>
</HTML>

JavaServer Pages Technology


JavaServer Pages (JSP) technology allows you to easily create Web content that has both static and dynamic components. JSP technology projects all the dynamic capabilities of Java Servlet technology but provides a more natural approach to creating static content. The main features of JSP technology are
  • A language for developing JSP pages, which are text-based documents that describe how to process a request and construct a response
  • Constructs for accessing server-side objects
  • Mechanisms for defining extensions to the JSP language