805 OCPJP 6 Questions 788 SCJP 5 questions 650 OCPJWCD 5 questions 600 OCAJP 7 questions 610 OCPJP 7 questions 510 Upgrade to OCPJP 7 questions 900 Java J2EE Qns

Tutorials

SCWCD : The Web Container Model

For the ServletContext initialization parameters: write servlet code to access initialization parameters; and create the deployment descriptor elements for declaring initialization parameters.

The following methods of the ServletContext interface allow the servlet access to context initialization parameters associated with a Web application as specified by the Application Developer in the deployment descriptor:

  • getInitParameter

    Returns a String containing the value of the named context-wide initialization parameter, or null if the parameter does not exist. This method can make available configuration information useful to an entire "web application". For example, it can provide a webmaster’s email address or the name of a system that holds critical data.

  • 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.

Initialization parameters are used by an Application Developer to convey setup information. Typical examples are a Webmaster’s e-mail address, or the name of a system that holds critical data.

public interface ServletContext {

	public java.lang.String getInitParameter(java.lang.String name);
	public java.util.Enumeration getInitParameterNames();
	
}
					

Context initialization parameters that define shared String constants used within your application, which can be customized by the system administrator who is installing your application. The values actually assigned to these parameters can be retrieved in a servlet or JSP page by calling:

javax.servlet.ServletContext context = getServletContext(); 
String value = context.getInitParameter("webmaster");
					
where "webmaster" matches the param-name element of one of these initialization parameters.

You can define any number of context initialization parameters, including zero:


<web-app>
	...
	<context-param>
		<param-name>webmaster</param-name>
		<param-value>myaddress@mycompany.com</param-value>
		<description>
			The EMAIL address of the administrator to whom questions
			and comments about this application should be addressed.
		</description>
	</context-param>
	...
</web-app>

					

			
<!--
The context-param element contains the declaration of a web
application’s servlet context initialization parameters.
-->

<!ELEMENT context-param (param-name, param-value, description?)>

					

For the fundamental servlet attribute scopes (request, session, and context): write servlet code to add, retrieve, and remove attributes; given a usage scenario, identify the proper scope for an attribute; and identify multi-threading issues associated with each scope.

Request Attributes.

Attributes are objects associated with a request. Attributes may be set by the container to express information that otherwise could not be expressed via the API, or may be set by a servlet to communicate information to another servlet (via the RequestDispatcher). Attributes are accessed with the following methods of the ServletRequest interface:

  • getAttribute

    Returns the value of the named attribute as an Object, or null if no attribute of the given name exists. Attributes can be set two ways. The servlet container may set attributes to make available custom information about a request. Attributes can also be set programatically using setAttribute(String, Object). This allows information to be embedded into a request before a RequestDispatcher call. Attribute names should follow the same conventions as package names. This specification reserves names matching java.*, javax.*, and sun.*.

  • getAttributeNames

    Returns an Enumeration containing the names of the attributes available to this request. This method returns an EMPTY Enumeration if the request has no attributes available to it.

  • setAttribute

    Stores an attribute in this request. Attributes are reset between requests. This method is most often used in conjunction with RequestDispatcher. Attribute names should follow the same conventions as package names. Names beginning with java.*, javax.*, and com.sun.*, are reserved for use by Sun Microsystems. If the object passed in is null, the effect is the same as calling removeAttribute(String).

  • removeAttribute

    Removes an attribute from this request. This method is not generally needed as attributes only persist as long as the request is being handled.

Only ONE attribute value may be associated with an attribute name.
package javax.servlet;
					
public interface ServletRequest {

	public java.lang.Object getAttribute(java.lang.String name);
	public java.util.Enumeration getAttributeNames();
	public void setAttribute(java.lang.String name, java.lang.Object o);
	public void removeAttribute(java.lang.String name);

}
					

Attribute names beginning with the prefixes of "java." and "javax." are RESERVED for definition by this specification. Similarly, attribute names beginning with the prefixes of "sun.", and "com.sun." are reserved for definition by Sun Microsystems. It is suggested that all attributes placed in the attribute set be named in accordance with the reverse domain name convention suggested by the Java Programming Language Specification for package naming.

Session Attributes.

A servlet can bind an object attribute into an HttpSession implementation by name. Any object bound into a session is available to any other servlet that belongs to the same ServletContext and handles a request identified as being a part of the same session.

  • getAttribute

    Returns the object bound with the specified name in this session, or null if no object is bound under the name.

  • getAttributeNames

    Returns an Enumeration of String objects containing the names of all the objects bound to this session.

  • setAttribute

    Binds an object to this session, using the name specified. If an object of the same name is already bound to the session, the object is replaced. After this method executes, and if the new object implements HttpSessionBindingListener, the container calls HttpSessionBindingListener.valueBound. The container then notifies any HttpSessionAttributeListeners in the web application. If an object was already bound to this session of this name that implements HttpSessionBindingListener, its HttpSessionBindingListener.valueUnbound method is called. If the value passed in is null, this has the same effect as calling removeAttribute().

  • removeAttribute

    Removes the object bound with the specified name from this session. If the session does not have an object bound with the specified name, this method does nothing. After this method executes, and if the object implements HttpSessionBindingListener, the container calls HttpSessionBindingListener.valueUnbound. The container then notifies any HttpSessionAttributeListeners in the web application.

package javax.servlet.http;				
				
public interface HttpSession {

	public java.lang.Object getAttribute(java.lang.String name);
	public java.util.Enumeration getAttributeNames();
	public void setAttribute(java.lang.String name, java.lang.Object value);
	public void removeAttribute(java.lang.String name);

}
					

Some objects may require notification when they are placed into, or removed from, a session. This information can be obtained by having the object implement the HttpSessionBindingListener interface. This interface defines the following methods that will signal an object being bound into, or being unbound from, a session

  • valueBound

  • valueUnbound

The valueBound method must be called BEFORE the object is made available via the getAttribute method of the HttpSession interface. The valueUnbound method must be called AFTER the object is no longer available via the getAttribute method of the HttpSession interface.

Multiple servlets executing request threads may have active access to a single session object at the same time. The Developer has the responsibility for synchronizing access to session resources as appropriate.

Within an application marked as distributable, all requests that are part of a session must be handled by one Java Virtual Machine (JVM) at a time. The container must be able to handle all objects placed into instances of the HttpSession class using the setAttribute or putValue methods appropriately. The following restrictions are imposed to meet these conditions:

  • The container must accept objects that implement the Serializable interface.

  • The container may choose to support storage of other designated objects in the HttpSession, such as references to Enterprise JavaBeans components and transactions.

  • Migration of sessions will be handled by container-specific facilities.

The distributed servlet container must throw an IllegalArgumentException for objects where the container cannot support the mechanism necessary for migration of the session storing them.

Containers must notify any session attributes implementing the HttpSessionActivationListener during migration of a session. They must notify listeners of passivation prior to serialization of a session, and of activation after deserialization of a session.

Application Developers writing distributed applications should be aware that since the container may run in more than one Java virtual machine, the developer cannot depend on static variables for storing an application state. They should store such states using an enterprise bean or a database.

Context Attributes.

A servlet can bind an object attribute into the context by name. Any attribute bound into a context is available to any other servlet that is part of the same Web application. The following methods of ServletContext interface allow access to this functionality:

  • setAttribute

    Binds an object to a given attribute name in this servlet context. If the name specified is already used for an attribute, this method will REPLACE the attribute with the new to the new attribute. If listeners are configured on the ServletContext the container notifies them accordingly. If a null value is passed, the effect is the same as calling removeAttribute(). Attribute names should follow the same convention as package names. The Java Servlet API specification reserves names matching java.*, javax.*, and sun.*.

  • getAttribute

    Returns the servlet container attribute with the given name, or null if there is no attribute by that name. An attribute allows a servlet container to give the servlet additional information not already provided by this interface. See your server documentation for information about its attributes. A list of supported attributes can be retrieved using getAttributeNames. The attribute is returned as a java.lang.Object or some subclass. Attribute names should follow the same convention as package names. The Java Servlet API specification reserves names matching java.*, javax.*, and sun.*.

  • getAttributeNames

    Returns an Enumeration containing the attribute names available within this servlet context. Use the getAttribute(String) method with an attribute name to get the value of an attribute.

  • removeAttribute

    Removes the attribute with the given name from the servlet context. After removal, subsequent calls to getAttribute(String) to retrieve the attribute’s value will return null. If listeners are configured on the ServletContext the container notifies them accordingly.

package	javax.servlet;

public interface ServletContext {

	public void setAttribute(java.lang.String name, java.lang.Object object);
	public java.lang.Object getAttribute(java.lang.String name);
	public java.util.Enumeration getAttributeNames();
	public void removeAttribute(java.lang.String name);
	
}	
					

scjp 1.5 | scjp 1.6 | scwcd 1.5
Java Certifications
www.javacertifications.net