Showing posts with label advance java. Show all posts
Showing posts with label advance java. Show all posts

Sunday, 18 December 2011

Using Scope Objects

Collaborating Web components share information via objects that are maintained as attributes of four scope objects. You access these attributes using the [get|set]Attribute methods of the class representing the scope. Table 11-4 lists the scope objects.
Table 11-4 Scope Objects 
Scope Object
Class
Accessible From
Web context
Web components within a Web context.
Session
Web components handling a request that belongs to the session.
Request
Web components handling the request.
Page
The JSP page that creates the object.

Saturday, 17 December 2011

JSP Examples Tutorial - Using Abstract Classes in JSP


<HTML>
    <HEAD>
        <TITLE>Using Abstract Classes</TITLE>
    </HEAD>
    <BODY>
        <H1>Using Abstract Classes</H1>
        <%!
            javax.servlet.jsp.JspWriter localOut;
            abstract class a
            {
                abstract String getText() throws java.io.IOException;
                public void printem() throws java.io.IOException 
                {
                    localOut.println(getText());
                }
            }
            class b extends a
            {
                String getText() throws java.io.IOException 
                {
                    return "Hello from JSP!";
                }
            }
        %>     
        <%
            localOut = out;     
            b bObject = new b();
            bObject.printem();
        %>
    </BODY>
</HTML>

Thursday, 15 December 2011

Notifying Methods to Shut Down in Servlets

See Tracking Service Request
To ensure a clean shutdown, your destroy method should not release any shared resources until all of the service requests have completed. One part of doing this is to check the service counter. Another part is to notify the long-running methods that it is time to shut down. For this notification, another field is required. The field should have the usual access methods:
public class ShutdownExample extends HttpServlet {
   private boolean shuttingDown;
   ...
   //Access methods for shuttingDown
   protected synchronized void setShuttingDown(boolean flag) {
      shuttingDown = flag;
   }
   protected synchronized boolean isShuttingDown() {
      return shuttingDown;
   }
}
 
An example of the destroy method using these fields to provide a clean shutdown follows:
public void destroy() {
   /* Check to see whether there are still service methods /*
   /* running, and if there are, tell them to stop. */
   if (numServices() > 0) {
      setShuttingDown(true);
   }

   /* Wait for the service methods to stop. */
   while(numServices() > 0) {
      try {
         Thread.sleep(interval);
      } catch (InterruptedException e) {
      }
   }
} 

Initializing a Servlet


After the Web container loads and instantiates the servlet class and before it delivers requests from clients, the Web container initializes the servlet. You can customize this process to allow the servlet to read persistent configuration data, initialize resources, and perform any other one-time activities by overriding the init method of the Servlet interface. A servlet that cannot complete its initialization process should throw UnavailableException.
All the servlets that access the bookstore database (BookStoreServletCatalogServletBookDetailsServlet, and ShowCartServlet) initialize a variable in their init method that points to the database helper object created by the Web context listener:
public class CatalogServlet extends HttpServlet {
   private BookDB bookDB;
   public void init() throws ServletException {
      bookDB = (BookDB)getServletContext().
         getAttribute("bookDB");
      if (bookDB == null) throw new
         UnavailableException("Couldn't get database.");
   }
}