October 24, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

The Servlet Container Model

  • October 24, 2002
  • By Que Publishing
  • Send Email »
  • More Articles »

Session

A Session is made up of multiple hits from the same browser across some period of time. The session scope includes all hits from a single machine (multiple browser windows if they share cookies). Servlets maintain state with sessions. Listing 4.13 is a modification of a sample servlet that ships with Tomcat. It demonstrates how you can use session attributes.

Listing 4.13 Servlet That Demonstrates Session Attributes

import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class SessionExample extends HttpServlet 
{

  public void doGet(HttpServletRequest request,
           HttpServletResponse response)
    throws IOException, ServletException
  {
    response.setContentType("text/html");

    PrintWriter out = response.getWriter();
    out.println("<html>");
    out.println("<body bgcolor=\"white\">");
    out.println("<head>");

    String title = "Session Servlet";
    out.println("<title>" + title + "</title>");
    out.println("</head>");
    out.println("<body>");

     out.println("<h3>" + title + "</h3>");

    HttpSession session = request.getSession();
    out.println("session id" + " " + session.getId());
    out.println("<br/>");
    out.println("session created" + " ");
    out.println(new Date(session.getCreationTime()) + 
          "<br/>");
    out.println("session lastaccessed" + " ");
    out.println(
         new Date(session.getLastAccessedTime()));

    //get these from the HTML form or query string
    String dataName = request.getParameter("dataname");
    String dataValue =request.getParameter("datavalue");
    if (dataName != null && dataValue != null) 
    {
      session.setAttribute(dataName, dataValue);
    }

    out.println("<p/>");
    out.println("session data" + "<br/>");
    Enumeration names = session.getAttributeNames();
    while (names.hasMoreElements()) 
    {
      String name = (String) names.nextElement(); 
      String value = 
          session.getAttribute(name).toString();
      out.println(name + " = " + value + "<br/>");
    }

    out.println("<p/>");
    out.print("<form action=\"");
    out.print(response.encodeURL("SessionExample"));
    out.print("\" ");
    out.println("method=POST>");
    out.println("Name of Session Attribute:");
    out.println("<input type=text size=20 " +
          "name=dataname>");
    out.println("<br/>");
    out.println("Value of Session Attribute:");
    out.println("<input type=text size=20 " +
          "name=datavalue>");
    out.println("<br/>");
    out.println("<input type=submit>");
    out.println("</form>");

    out.println("<p/>GET based form:<br/>");
    out.print("<form action=\"");
    out.print(response.encodeURL("SessionExample"));
    out.print("\" ");
    out.println("method=GET>");
    out.println("Name of Session Attribute:");
    out.println("<input type=text size=20 " +
          "name=dataname>");
    out.println("<br/>");
    out.println("Value of Session Attribute:"); 
    out.println("<input type=text size=20 " +
          "name=datavalue>");
    out.println("<br/>");
    out.println("<input type=submit>");
    out.println("</form>");

    out.print("<p/><a href=\"");
    String url = "SessionExample?dataname=scwcd&" +
           "datavalue=pass!";
    out.print(response.encodeURL(url));
    out.println("\" >URL encoded </a>");

    out.println("</body>");
    out.println("</html>");
    
    out.println("</body>");
    out.println("</html>");
  }

  public void doPost(HttpServletRequest request,
           HttpServletResponse response)
    throws IOException, ServletException
  {
    doGet(request, response);
  }

}
//returns a page that looks like:
//Session Servlet
//
//session id 9805A5C4C084F5B47788242406C22455 
//session created Tue Apr 16 22:11:06 PDT 2002
//session lastaccessed Tue Apr 16 22:13:27 PDT 2002
//
//session data
//publisher = Que
//author = trottier
//scwcd = pass!

To summarize, sessions are what you can use to track a single user over a short time. You get the session object (HttpSession) from the request object. To track multiple users over time you must jump to context, covered next.

Context

A Web application includes many parts; it rarely is just one class or one JSP. To help manage an application, you will sometimes need to set and get information that all of the servlets share together, which we say is context-wide. An example would be using a login servlet to create an application-level attribute such as application name like so:

public void init(ServletConfig config) 
   throws ServletException 
{
    super.init(config);

    // set application scope parameter 
    // to "Certification by Que"
    ServletContext context = config.getServletContext();
    context.setAttribute(
       "applicationName", "Certification by Que");
}

Later, in another servlet you may use the application name as demonstrated in Listing 4.14.

Listing 4.14 Servlet doGet Method Demonstration

public void doGet(HttpServletRequest request, 
         HttpServletResponse response)
    throws ServletException, IOException 
{
    response.setContentType("text/html");
    
    PrintWriter out = response.getWriter();
    out.print("<html>");
    out.print("<head>");
    out.print("<title>");
    
    // get value of applicationName from ServletContext
    out.print(getServletContext().getAttribute(
              "applicationName").toString());
    
    out.print("</title>");
    out.print("<head>");
    out.print("<body>");
    
    //complete body content here...
    
    out.print("</body></html>");
    out.close();
}

Besides setting and retrieving your custom attributes, you can get additional information from the Servlet Container, such as its major and minor version, the path to a given servlet, and more. The following summarizes the additional methods you might use:

  • getAttributeNames(). Returns an Enumeration object containing the attribute names available within this servlet context.

  • getContext(String uripath). Returns a ServletContext object that corresponds to a specified URL on the server.

  • getInitParameter(String name). Returns a string containing the value of the named context-wide initialization parameter, or null if the parameter does not exist.

  • getInitParameterNames(). Returns the names of the context's initialization parameters as an Enumeration of string objects, or an empty Enumeration if the context has no initialization parameters.

  • getMajorVersion(). Returns the major version as an int of the Java Servlet API that this Servlet Container supports.

  • getMimeType(java.lang.String file). Returns the MIME type as a string of the specified file, or null if the MIME type is not known.

  • getMinorVersion(). Returns the minor version as an int of the Servlet API that this Servlet Container supports.

  • getNamedDispatcher(String name). Returns a RequestDispatcher object that acts as a wrapper for the named servlet.

  • getRealPath(String path). Returns a string containing the real path for a given virtual path.

  • getRequestDispatcher(String path). Returns a RequestDispatcher object that acts as a wrapper for the resource located at the given path.

  • getResource(String path). Returns a URL to the resource that is mapped to a specified path.

  • getResourceAsStream(String path). Returns the resource located at the named path as an InputStream object.

getServerInfo(). Returns the name and version as a String of the Servlet Container on which the servlet is running.

Servlet Life-cycle

1.5 Given a life-cycle method, init, service, or destroy, identify correct statements about its purpose or about how and when it is invoked.

The servlet life-cycle is not obvious. The container calls three methods—namely, init(), service() and destroy()—in that order. Ordinarily, that is how the container talks to your servlet. With some containers, you can modify this behavior, but the exam will assume this order.


When is init() called?

A common question on the exam tests your understanding of when init() is called. Knowledge of a servlet's life-cycle is crucial to answering these types of questions. Remember, init() may be called when the server starts (tell web.xml to load servlet upon startup), when first requested, and sometimes the container management console will allow you to call it as part of the server administration. The exam expects you to know that init() will only be called once per servlet instance, that it is not used to send information back to the browser (HttpServletResponse is not a parameter), and that it throws a ServletException to the container that called the servlet if anything goes wrong.


The init method is called first, the first time the servlet is invoked. This happens one time. However, the service method is called every time a servlet is requested. Lastly, the destroy method is called one time, upon the removal of the servlet from memory due either to explicit removal or lack of use (for example, the session expires). You can configure the container to load certain servlets upon startup (<load-on-startup/> in web.xml), but most of them will be loaded upon first request. Either way, the init method is called first. Place in this method things you will use across requests, like database connections, and class member values such as finalized constants.

The destroy() method, like init(), is called only once. It is called when the servlet is taken out of service and all pending requests to a given servlet (that one with the mentioned destroy() method) are completed or have timed-out. This method is called by the container to give you a chance to release resources such as database connections and threads. You can always call super.destroy() (GenericServlet.destroy()) to add a note to the log about what is going on. You might want to do this even if only to place a timestamp in there.


destroy() is not called if the container crashes!

You should log activity from somewhere other than the destroy() method if a given piece of information is essential, but might not be logged if the logging functionality is placed in the destroy() method. This is because the destroy() method is not called if the Servlet Container quits abruptly (crashes).


Listings 4.15 and 4.16 are sample Web applications (HTML page and servlet combination) that demonstrate how to use the init(), service(), and destroy() methods, and when they are called. You could combine them and just have one servlet, but there are two pieces here to illustrate the relationship between static and dynamic parts of an application. The first part, Listing 4.15, is the HTML page.

Listing 4.15 HTML Page That Works with Servlet in Listing 4.16 Illustrating the Relationship

<html>
<head>
<title>LifeCycle Demonstration Using SQL Server</title>
</head>
<body bgcolor="#FFFFFF"> 

<p align=center>
<h1>LifeCycle Demonstration Using DB</h1>
<form name="formSearch" method="post" action=
"localhost:8080/examples/servlet/SearchLastNameServlet">
<table border="0" cellspacing="0" cellpadding="6">
  <tr>
   <td><h2>Search</h2></td>
   <td></td>
  </tr>
  <tr>
   <td><b>Last Name</b></td> 
   <td><input type="text" name="LastName" 
         value="Fuller">
   </td> 
  </tr> 
  <tr> 
   <td></td>
   <td align="center"><input type="submit" name="Submit"
     value="Submit">
   </td> 
  </tr> 
</table>
</form>
</p>
</body>
</html>

Servlet Reloading!

Servlets are loaded in one of three ways. The first way is when the Web server starts. You can set this in the configuration file. Reload can happen automatically after the container detects that its class file (under servlet dir, for example, WEB-INF/classes) has changes. The third way, with some containers, is through an administrator interface.


The HTML page contains a form with one field for a last name. When submitted, the container takes the lastname field and hands it to the servlet in the request object. This object is where you normally extract requester information. The servlet grabs the lastname, if any, and builds a SQL WHERE clause with it. Then the servlet establishes a connection with the database server (I'm using MS SQL Server) and executes the statement. Then it walks through the resultset getting the data from each field of every row. Finally, it builds the HTML page and sends it off to the client browser. While the database portion is not on the exam, it is an excellent example of how you can take advantage of the methods that are called by the container.


Servlet Synchronizing!

Servlets are run each in their own thread. When the synchronized keyword is used with a servlet's service() method, requests to that servlet are handled one at a time in a serialized manner. This means that multiple requests won't interfere with each other when accessing variables and references within one servlet. It also means the processing capabilities of the Servlet Container are reduced because the more efficient multithreaded mode has been disallowed for a given servlet that has been declared with the synchronized keyword.


Listing 4.16 is the servlet that queries the database based on the form data. Notice that you can forgo the above HTML file by appending the FirstName parameter to the URL like so: http://localhost:8080/examples/servlet/SearchLastNameServlet?LastName=Fuller. Also, you need to set up a data source with system data source names (DSNs), whether to a data source that is local to your computer or remote on the network.

Listing 4.16 Servlet That Queries a Database Based on Form Input from Listing 4.15

/* Don't use "java.io.*"
  Be explicit to see which classes are expected
*/
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.ServletConfig;

public class SearchLastNameServlet extends HttpServlet
{
  //These will be used across requests,
  //so declare it at class level, 
  //not service or doGet level.
  //While it is common to use con,stmt,rs
  //I find these cryptic so I prefer to spell
  //them out completely for clarity.
  private Connection _connection = null;
  //Can differentiate between attributes of a class 
  //and local variables within a method 
  //with preceding underscore.
  private String _driverName = 
              "sun.jdbc.odbc.JdbcOdbcDriver";
  //connects to Northwind DB in SQL Server on my machine
  private String _connectionString = 
                  "jdbc:odbc:employeeDB";
  //optional, declare these in doPost() or service()
  //to avoid conflict between requests.
  private Statement statement = null;
  private ResultSet resultset = null;
  //not here, keep query local  
  //private String query = null;

  //common types
  final int COMMA = 1;
  final int TABLE_COLUMN = 2;
  final int TABLE_HEADER = 3;
  final boolean DELIMITED = true;

  //This is called only once during lifecycle!
  public void init(ServletConfig _config) 
   throws ServletException
  {
   super.init(_config);

    //warning! userid and password is exposed:
   String username = "sa";
   String password = "sa";

   try
   {
     Class.forName(_driverName);
     //warning! userid and password is exposed:
     _connection = DriverManager.getConnection
      (_connectionString, username, password);
   } catch(Exception ex)
   {
     throw new ServletException(ex.getMessage());
   }
  }

  public void service(HttpServletRequest _request, 
       HttpServletResponse _response) 
   throws ServletException, IOException
  {
   _response.setContentType("text/html");

   String table = " Employees ";
   // query string where clause constraint
   String where = "";
   if (_request.getParameter("LastName").length() > 0) 
   {
     String lastName = _
             request.getParameter("LastName");
     where = " where LastName like \'";
     where += lastName;
     where += "%\'";
 
   } else
   {
     where = "";
   }
   
   StringBuffer htmlResult = new StringBuffer();
   try
   {
     String sqlQuery = "SELECT * from "+ table + where; 
     statement = _connection.createStatement();
     resultset = statement.executeQuery(sqlQuery);
  
     while(resultset.next())
     {   
      //Not necessary to place in array, but...
      String[] field = new String[8];
      //warning! these should be in same order as 
      //DB table field order
      //otherwise you can get errors, a Sun todo.
      field[0] = ""+resultset.getInt("EmployeeID");
      field[1] = resultset.getString("LastName");
      field[2] = resultset.getString("FirstName");
      field[3] = resultset.getString("Title");
      field[4] = ""+resultset.getDate("BirthDate");
      field[5] = resultset.getString("City");
      field[6] = resultset.getString("Region");
      field[7] = resultset.getString("Country");
      htmlResult.append( getTableBody(field) );
     }
   } catch(Exception ex)
   {
     throw new ServletException(ex.getMessage());
   }

   StringBuffer html = new StringBuffer();
   html.append( htmlHeader() );

   //build results
   html.append( getTableHeader() );
   html.append( htmlResult.toString() );
   html.append( getTableFooter() );

   html.append( htmlFooter() );

   ServletOutputStream out = response.getOutputStream();
   out.println( html.toString() );
  }

  public void destroy() 
  {
   try
   {
    // Give connection to garbage collector
     connection.close();
     connection = null;
   } catch(Exception ex)
   {
     throw new ServletException(ex.getMessage());
   }
  }  
  //
  // convenience methods providing abstraction
  //
  
  /*
  * Prints the table header.
  */
  public String getTableHeader()
  {
     StringBuffer header = new StringBuffer();

   header.append("<table border=\"2\">\n");
   header.append("<tr>\n");
   header.append("<th align=\"left\">EmployeeID</th>\n");
   header.append("<th align=\"left\">LastName</th>\n");
   header.append("<th align=\"left\">FirstName</th>\n");
   header.append("<th align=\"left\">Title</th>\n");
   header.append("<th align=\"left\">BirthDate</th>\n");
   header.append("<th align=\"left\">City</th>\n");
   header.append("<th align=\"left\">Region</th>\n");
   header.append("<th align=\"left\">Country</th>\n");
   header.append("</tr>\n");
    return header.toString();    
  }

  /*
  * Prints the table body.
  */
  public String getTableBody(String[] field)
  {
   StringBuffer body = new StringBuffer();
   body.append("<tr>\n");

   for(int index=0; index<field.length; index++)
   {
     body.append("  <td align=\"left\">"); 
     body.append(field[index]); 
     body.append("</td>\n");
   }
   body.append("</tr>\n");
   return body.toString();    
  }

  //you would bother to have a whole method for this
  //because someone might ask you to add extra
  //stuff to the bottom of every table so it is smart
  //to separate it like this.
  public String getTableFooter()
  {
   StringBuffer footer = new StringBuffer();

   footer.append("</table>\n");
   
   return footer.toString();    
  }

  /*
  * Prints the html file header.
  */
  public String htmlHeader()
  {
   StringBuffer html = new StringBuffer();

   html.append("<html>");
   html.append("<head>");
   html.append("<title>LifeCycle Servlet Response" +
         "</title>");
   html.append("</head>");
   html.append("<body bgcolor=\"#FFFFFF\"> ");
   html.append("<p align=center><h1>LifeCycle Servlet "+
         " Response</h1></p>"); 
    
   return html.toString();    
  }

  /*
  * Prints the html file footer. 
  * This will change often due to 
  * marketing and lawyers.
  */
  public String htmlFooter()
  {
   StringBuffer html = new StringBuffer();

   html.append("<a href=\"http://localhost:8080/" +
     "examples/servlets/LifeCycle.html\">" +
     "</b>BACK</b></a>");
   html.append("</p>");
   html.append("</body>");
   html.append("</html>");
    
   return html.toString();    
  }
}

Once you set up a proper System DSN, or use a fully qualified connection string, the servlet will query the database. Listing 4.15 shows how you can create an HTML form to call this servlet. The output of the servlet query looks similar to Figure 4.6.

Figure 4.6
The result of a query by a servlet.

Listing 4.16 is just an example of when you might invoke the destroy() method. This code example could be improved in two ways. First, it is not thread-safe (statement and resultset variables could be local, not instance variables). That way separate instances wouldn't walk over each other's results. Second, this example doesn't make use of the Data Access Object pattern. You could do better by having separate objects for the Presentation and Database ("separation of concerns") portions of the program. I've lumped it all together here just to demonstrate the section point of how the destroy() method is used.

Using a RequestDispatcher

1.6 Use a RequestDispatcher to include or forward to a Web resource.

The RequestDispatcher is a powerful tool. You can perform programmatic server-side includes or route the whole request to another servlet or JSP with a forward. There are three ways to get the RequestDispatcher. The first two are through the Context, with ServletContext.getRequestDispatcher(java.lang.String) or with ServletContext.getNamedDispatcher(java.lang.String). Either returns a RequestDispatcher object that acts as a wrapper for the named servlet (in web.xml, the Web application deployment descriptor). The final way is with ServletRequest.getRequestDispatcher(java.lang.String). Notice that you can use a relative pathname here. You must use absolutes with ServletContext.getRequestDispatcher(java.lang.String). Be careful with your paths. If the path begins with a "/", it is interpreted as relative to the current context root. You will get a null if the Servlet Container cannot return a RequestDispatcher.

A RequestDispatcher object can be used to forward a request to the resource or to include output from the resource in a response. The specification allows the resource to be dynamic or static. If it is dynamic, such as a servlet, the container will invoke that servlet, and then take the output and include it. If it is static, such as a text file, then the container will include the text as is. Listing 4.17 demonstrates how one servlet can transfer the request to another servlet.

Listing 4.17 Using a RequestDispatcher to Forward a Request to Another Servlet

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class ServletToServlet extends HttpServlet 
{
  public void doGet (HttpServletRequest request,
           HttpServletResponse response) 
      throws ServletException, IOException 
  {
    try {
      getServletConfig()
         .getServletContext()
         .getRequestDispatcher("/HelloWorldExample")
         .forward(request, response);
    } catch (Exception ex) {
      ex.printStackTrace ();
    }
  }
}

You can also include content from a static page or another servlet. You would use a snippet like so:

RequestDispatcher dispatcher =
   getServletContext().getRequestDispatcher(path);
if (dispatcher == null) 
{
  out.println(path + " not available");
  return;
} else
{
  dispatcher.include(request, response);
}

There are a few characteristics about the forward and include methods. The ServletRequest's path elements and parameters remain unchanged from the caller's. The included servlet cannot set headers. This is a good candidate for a trick question. The servlet cannot change the response status code either (if you try, it will be ignored). The best way to send along information from the calling servlet to the called servlet is using a query string or, even better, using the setAttribute() method to set request object attributes where they are easy to access.

There is a matter of timing to consider. You can call an include anytime, but the forward has to be called before the response is committed. Otherwise you'll throw an IllegalStateException exception.


Request Dispatcher Paths

ServletContext.getRequestDispatcher()—This method uses absolute paths.

ServletRequest.getRequestDispatcher(String path)—The path may be relative, but cannot extend outside current servlet context.

ServletRequest.getNamedDispatcher(String name)—This name is the name of the servlet for which a dispatcher is requested, and is in the web.xml file (see Chapter 10, "Web Applications," for more about web.xml).


Regarding the forward method, one reason you may want to use it is so you can dedicate a servlet as the controller. In this way, the controller can filter, preprocess requests, and manage the transaction. The gotcha here is once a servlet forwards a request, it loses control. The controller has no capability to regain access directly. You can create an architecture where requests are returned (forwarded back by a slave servlet), but the native functionality isn't helpful for this. There is another problem. When you run Listing 4.17, you'll notice something missing—the URL in the address bar doesn't change. The client loses path information when it receives a forwarded request. That means all relative URLs in the HTML become invalid. Your browser will consider the links broken. Sometimes this doesn't matter, but when it does, use sendRedirect() instead.

Web Application Context

3.1 Identify the uses for and the interfaces (or classes) and methods to achieve the following features:

  • Servlet context initialization parameters.
  • Servlet context listener.
  • Servlet context attribute listener.
  • Session attribute listeners.

Please see the section "Interfacing with HTML Requests," earlier in this chapter, where the related objective 1.3 "Retrieve a servlet initialization parameter" is discussed. Listing 4.8 is especially helpful here because it demonstrates how to enumerate the context initialization parameter list.

Regarding listeners, you can monitor and react to servlet events by defining listener objects. These objects have methods that the container invokes when life-cycle events occur. To make this happen, you define a listener class by implementing a listener interface. The container will invoke the listener method and pass it information (methods in the HttpSessionListener interface are passed an HttpSessionEvent) about that event.

Listing 4.18 demonstrates how you could use the initialization and destruction events.

Listing 4.18 Listening for a Context Initialization and Destruction

import java.util.Date;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public final class ContextListener
  implements ServletContextListener 
{
  public void contextInitialized(
                ServletContextEvent event) 
  {
   ServletContext context = event.getServletContext();
   context.setAttribute("StartDate", Date);
  }
   
  public void contextDestroyed(ServletContextEvent event) 
  {
   ServletContext context = event.getServletContext();
   Date startDate = context.getAttribute("StartDate");
   customLog(startDate);
   context.removeAttribute("StartDate");
  }
}

The attribute StartDate is set when the container initializes the application. Then when the application quits, the same attribute is logged and then deleted. For an excellent article that provides an overview of application life-cycle events, please see Servlet App Event Listeners by Stephanie Fesler (04/12/2001, http://www.onjava.com/pub/a/onjava/2001/04/12/listeners.html). The four interfaces that you can expect to see on the exam are these:

  • When a servlet is initialized or destroyed:

    • javax.servlet.ServletContextListener.

    • contextDestroyed(ServletContextEvent sce) Notification that the servlet context is about to be shut down.

    • contextInitialized(ServletContextEvent sce) Notification that the Web application is ready to process requests.

  • When a context attribute is added, removed, or replaced:

    • javax.servlet.ServletContextAttributeListener.

    • attributeAdded(ServletContextAttributeEvent scab) Notification that a new attribute was added to the servlet context.

    • attributeRemoved(ServletContextAttributeEvent scab) Notification that an existing attribute has been removed from the servlet context.

    • attributeReplaced(ServletContextAttributeEvent scab) Notification that an attribute on the servlet context has been replaced.

  • When a session is initialized or destroyed:

    • javax.servlet.http.HttpSessionListener.

    • sessionCreated(HttpSessionEvent se) Notification that a session was created.

    • sessionDestroyed(HttpSessionEvent se) Notification that a session became invalid or timed out.

  • When a session attribute is added, removed, or replaced:

    • HttpSessionAttributeListener.

    • attributeAdded(HttpSessionBindingEvent se) Notification that an attribute has been added to a session.

    • attributeRemoved(HttpSessionBindingEvent se) Notification that an attribute has been removed from a session.

    • attributeReplaced(HttpSessionBindingEvent se) Notification that an attribute has been replaced in a session.

Context Within a Distributable Web Application

3.3 Distinguish the behavior of the following in a distributable:

  • Servlet context init. parameters.
  • Servlet context listener.
  • Servlet context attribute listener.
  • Session attribute listeners.

The behavior of these listeners in a distributable is exactly the same as those discussed in the previous section, with one notable exception: Event notification of addition, removal, or replacement will affect the listener for only that context. No other context, such as other JVMs on the same or other machine, will know about the listener events.





Page 4 of 5



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel