Tutorial Home
Hibernate
StrutsJSP
Spring
|
SCWCD : Building JSP Pages Using Standard Actions
Action jsp:useBean. A jsp:useBean action associates an instance of a Java programming language object defined within a given scope and available with a given id with a newly declared scripting variable of the same id. When a jsp:useBean action is used in an scriptless page, or in an scriptless context (as in the body of an action so indicated), there are no Java scripting variables created but instead an EL variable is created. The jsp:useBean action is quite flexible; its exact semantics depends on the attributes given. The basic semantic tries to find an existing object using id and scope. If the object is not found it will attempt to create the object using the other attributes. It is also possible to use this action to give a local name to an object defined elsewhere, as in another JSP page or in a servlet. This can be done by using the type attribute and not providing class or beanName attributes. At least ONE of type and class MUST be present, and it is NOT VALID to provide both class and beanName. If type and class are present, class must be assignable to type (in the Java platform sense). For it not to be assignable is a translation time error. The attribute beanName specifies the name of a Bean, as specified in the JavaBeans specification. It is used as an argument to the instantiate method in the java.beans.Beans class. It must be of the form a.b.c, which may be either a class, or the name of a resource of the form a/b/c.ser that will be resolved in the current ClassLoader. If this is not true, a request-time exception, as indicated in the semantics of the instantiate method will be raised. The value of this attribute can be a request-time attribute expression. In the following example, a Bean with name connection of type com.myco.myapp.Connection is available after actions on this element, either because it was already created and found, or because it is newly created. <jsp:useBean id="connection" class="com.myco.myapp.Connection" />In the next example, the timeout property is set to 33 if the Bean was instantiated: <jsp:useBean id="connection" class="com.myco.myapp.Connection"> <jsp:setProperty name="connection" property="timeout" value="33"> </jsp:useBean>In the following example, the object should have been present in the session. If so, it is given the local name wombat with WombatType. A ClassCastException may be raised if the object is of the wrong class, and an InstantiationException may be raised if the object is not defined: <jsp:useBean id="wombat" type="my.WombatType" scope="session" /> Syntax: <jsp:useBean id="name" scope="page|request|session|application" typeSpec /> typeSpec ::= class="className" | class="className" type="typeName" | type="typeName" class="className" | beanName="beanName" type="typeName" | type="typeName" beanName="beanName" | type="typeName"or <jsp:useBean id="name" scope="page|request|session|application" typeSpec > [body] </jsp:useBean>In this case, the [body] will be invoked if the Bean denoted by the action is CREATED. Typically, the body will contain either scriptlets or jsp:setProperty tags that will be used to modify the newly created object, but the contents of the body are not restricted.
Table 8.1. jsp:useBean attributes
Action jsp:getProperty. The jsp:getProperty action places the value of a bean instance property, converted to a String, into the implicit out object, from which the value can be displayed as output. The bean instance must be defined as indicated in the name attribute before this point in the page (usually via a jsp:useBean action). The conversion to String is done as in the println methods, i.e. the toString() method of the object is used for Object instances, and the primitive types are converted directly. If the object is not found, a request-time exception is raised. The value of the name attribute in jsp:setProperty and jsp:getProperty will refer to an object that is obtained from the pageContext object through its findAttribute method. The object named by the name must have been "introduced" to the JSP processor using either the jsp:useBean action or a custom action with an associated VariableInfo entry for this name. If the object was not introduced in this manner, the container implementation is recommended (but not required) to raise a translation error, since the page implementation is in violation of the specification. NOTE, a consequence of the previous paragraph is that objects that are stored in, say, the session by a front component are not automatically visible to jsp:setProperty and jsp:getProperty actions in that page unless a jsp:useBean action, or some other action, makes them visible. If the JSP processor can ascertain that there is an alternate way guaranteed to access the same object, it can use that information. For example it may use a scripting variable, but it must guarantee that no intervening code has invalidated the copy held by the scripting variable. The truth is always the value held by the pageContext object. Examples: <jsp:getProperty name="user" property="name" /> Syntax: <jsp:getProperty name="name" property="propertyName" />
Table 8.2. jsp:getProperty attributes
Action jsp:setProperty. The jsp:setProperty action sets the values of properties in a bean. The name attribute that denotes the bean must be defined before this action appears. There are two variants of the jsp:setProperty action. Both variants set the values of one or more properties in the bean based on the type of the properties. The usual bean introspection is done to discover what properties are present, and, for each, its name, whether it is simple or indexed, its type, and the setter and getter methods. Properties in a Bean can be set from one or more parameters in the request object, from a String constant, or from a computed request-time expression. Simple and indexed properties can be set using jsp:setProperty. When assigning values to indexed properties the value must be an array. The following two actions set a value from the request parameter values: <jsp:setProperty name="request" property="*" /> <jsp:setProperty name="user" property="user" param="username" />The following two elemenst set a property from a value: <jsp:setProperty name="results" property="col" value="0" /> <jsp:setProperty name="results" property="row" value="<%= i/4 %>" /> Syntax: <jsp:setProperty name="beanName" prop_expr /> prop_expr ::= property="*" | property="propertyName" | property="propertyName" param="parameterName" | property="propertyName" value="propertyValue" propertyValue ::= string
Table 8.3. jsp:setProperty attributes
Given a design goal, create a code snippet using the following standard actions: jsp:include, jsp:forward, and jsp:param.Action jsp:include. A jsp:include action provides for the inclusion of static and dynamic resources in the same context as the current page. Inclusion is into the current value of out. The resource is specified using a relativeURLspec that is interpreted in the context of the web application (i.e. it is mapped). The page attribute of both the jsp:include and the jsp:forward actions are interpreted relative to the current JSP PAGE, while the file attribute in an include directive is interpreted relative to the current JSP FILE. An included page cannot change the response status code or set headers. This precludes invoking methods like setCookie. Attempts to invoke these methods will be IGNORED. The constraint is equivalent to the one imposed on the include method of the RequestDispatcher class. A jsp:include action may have jsp:param subelements that can provide values for some parameters in the request to be used for the inclusion. Request processing resumes in the calling JSP page, once the inclusion is completed. The flush attribute controls flushing. If true, then, if the page output is buffered and the flush attribute is given a true value, then the buffer is flushed prior to the inclusion, otherwise the buffer is not flushed. The default value for the flush attribute is false. Examples: <jsp:include page="/templates/copyright.html" />The above example is a simple inclusion of an object. The path is interpreted in the context of the Web Application. It is likely a static object, but it could be mapped into, for instance, a servlet via web.xml. Syntax: <jsp:include page="urlSpec" flush="true|false" />and
<jsp:include page="urlSpec" flush="true|false">
{ <jsp:param .... /> }*
</jsp:include>
The first syntax just does a request-time inclusion. In the second case, the
values in the param subelements are used to augment the request for the purposes
of the inclusion.
Table 8.4. jsp:include attributes
Action jsp:forward. A jsp:forward action allows the runtime dispatch of the current request to a static resource, a JSP page or a Java servlet class in the same context as the current page. A jsp:forward effectively terminates the execution of the current page. The request object will be adjusted according to the value of the page attribute. A jsp:forward action may have jsp:param subelements that can provide values for some parameters in the request to be used for the forwarding. If the page output is buffered, the buffer is CLEARED prior to forwarding. If the page output is buffered and the buffer was flushed, an attempt to forward the request will result in an IllegalStateException. If the page output was unbuffered and anything has been written to it, an attempt to forward the request will result in an IllegalStateException. The following action might be used to forward to a static page based on some dynamic condition: <% String whereTo = "/templates/" + someValue; %> <jsp:forward page="<%= whereTo %>" /> Syntax: <jsp:forward page="relativeURLspec" />and
<jsp:forward page="urlSpec">
{ <jsp:param .... /> }*
</jsp:forward>
Table 8.5. jsp:forward attributes
Action jsp:param. The jsp:param element is used to provide key/value information. This element is used in the jsp:include, jsp:forward, and jsp:params elements. A translation error shall occur if the element is used elsewhere. When doing jsp:include or jsp:forward, the included page or forwarded page will see the original request object, with the original parameters augmented with the new parameters, with NEW VALUES TAKING PRECEDENCE over existing values when applicable. The scope of the new parameters is the jsp:include or jsp:forward call; i.e. in the case of an jsp:include the new parameters (and values) will not apply after the include. This is the same behavior as in the ServletRequest include and forward methods. For example, if the request has a parameter A=foo and a parameter A=bar is specified for forward, the forwarded request shall have A=bar,foo. Note that the NEW PARAM HAS PRECEDENCE. Syntax: <jsp:param name="name" value="value" />This action has two mandatory attributes: name and value. name indicates the name of the parameter, and value, which may be a request-time expression, indicates its value. |
|
SCJP 6.0 Simulator 664+ Questions With Explanations
SCWCD 5.0 Simulator 556+ Questions With Explanations
SCWCD 4.0 Simulator 500+ Questions With Explanations
OCA 10g Simulator 594+ Questions With Explanations
Hiring made easy Harness social networking, employee referrals, Agency portal and Job Portals
