JavaServer Pages (JSP) 1.0 Brief Introduction---III

Source: Internet
Author: User
Tags comments html comment html page insert integer modify java web
Js|server 8.2 The Jsp:usebean Action
This action lets your load in a JavaBean to is used in the JSP page. This is a a very useful capability because it lets you exploit the reusability of Java classes without sacrificing the con Venience that JSP adds over servlets alone. The simplest syntax for specifying this a bean should be used is:

<jsp:usebean id= "name" class= "Package.class"/>

This usually means "instantiate" is the class specified by class, and bind it to a variable with the name Specifi Ed by ID. " However, as we ' ll-shortly, you can specify a scope of this makes the bean associated with more than the CU Rrent page. In this case, it's useful to obtain references to existing beans, and the Jsp:usebean action specifies that a new object is instantiated only if there no existing one with the same ID and scope. Now, once your have a bean, can modify its properties via Jsp:setproperty, or by using a scriptlet and calling a Explicitly on the object with the variable name specified earlier via the id attribute. Recall that and beans, when you say ' this bean has a property of Typex called Foo ', you really mean ' This class has a Met Hod called Getfoo that returns something of type X, and another method called the Setfoo that takes a X as an argument. The Jsp:setproperty action is discussed into the next section, but for Now, this can either supply a explicit value, give a param attribute to say this value is derived from the N Amed request parameter, or just list the property to indicate that the value should is derived from the request parameter With the same name as the property. You read existing properties in a JSP expression or scriptlet by calling the appropriate method, or more getxxx, By using the Jsp:getproperty action.

Note This class specified for the bean must is in the server's regular class path, not the part reserved for classes T Hat get automatically reloaded when they the change. For example, in the Java Web Server, it and all the classes it uses should go in the classes directory or is in a jar file In the Lib directory, not being in the Servlets directory.

Here are a very simple example which loads a bean and sets/gets a simple String parameter.

beantest.jsp
You can also download the source or try it on-line.

<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<HTML>
<HEAD>
<title>reusing JavaBeans in Jsp</title>
<link Rel=stylesheet
href= "My-style-sheet.css"
Type= "Text/css" >
</HEAD>

<BODY>

<CENTER>
<table border=5>
<tr><th class= "TITLE" >
Reusing JavaBeans in jsp</table>
</CENTER>
<P>

<jsp:usebean id= "test" class= Hall. Simplebean "/>
<jsp:setproperty name= "Test"
property= "Message"
Value= "Hello WWW"/>

<jsp:getproperty name= "test" property= "message"/>
</I></H1>

</BODY>
</HTML>

Simplebean.java
Here's the source code for the bean used in the Beantest JSP page. You can also download the source.

Package Hall;

public class Simplebean {
Private String message = ' No message specified ';

Public String GetMessage () {
return (message);
}

public void Setmessage (String message) {
this.message = message;
}
}

Here ' s a typical result:

8.3 More Jsp:usebean Details
The simplest way to use a bean
<jsp:usebean id= "name" class= "Package.class"/>
To the load the bean, then with the Jsp:setproperty and Jsp:getproperty to modify and retrieve bean properties. However, there are two other options. A can use the container format, namely
<jsp:usebean ...>
Body
</jsp:useBean>
to indicate which the body portion should being executed only when the The bean was is a, not when a instantiated bean I s found and used. As discussed below, beans can be shared with, so don't all jsp:usebean statements to a new bean being instantiated. Second, in addition to IDs and class, there are three other attributes, can use:scope, type, and Beanname. These attributes are are summarized in the following table.

Atribute Usage
ID gives a name to the variable that would reference the bean. A previous bean object is used instead of instantiating a new one if one can being found with the same ID and scope.
Class designates the full package name of the Bean.
Scope  indicates the which the bean should be made available. There are four possible values:page, request, session, and application. The default, page, indicates that the bean was only available on the current page (stored in the PageContext of the current Page). A value of request indicates that the bean was only available for the "Current client request" (stored in the ServletRequest Object). A value of Session indicates which the object is available to all pages during the "Life of" the current HttpSession. Finally, a value of application indicates that it's available to all pages that share the same servletcontext. The reason that's scope matters is, that a Jsp:usebean entry would only be in a new object being instantiated if there is no previous object with the same ID and scope. Otherwise the previously existing object is used, and any jsp:setparameter elements or other entries the between A start and end tags would be ignored.  
Type specifies the type of the variable that would refer to the object. This is must match the classname or be a superclass or is interface that class implements. Remember that's name of the variable is designated via the id attribute.
Beanname gives the name of the bean, as you would supply it to the instantiate method of Beans. It is permissible to supply a type and a beanname, and omit the class attribute.

8.4 The Jsp:setproperty Action
Use Jsp:setproperty to give values to properties of beans that have been referenced. You can does this in two contexts. The can use Jsp:setproperty after, but outside of, a jsp:usebean element, as below:

<jsp:usebean id= "myname" .../>
...
<jsp:setproperty name= "MyName"
property= "Someproperty" .../>

In this case, the Jsp:setproperty are executed regardless of whether a new bean was instantiated or a existing bean was fo und. A second context in which Jsp:setproperty can appear are inside the body of a jsp:usebean element, as below:

<jsp:usebean id= "MyName" ... >
...
<jsp:setproperty name= "MyName"
property= "Someproperty" .../>
</jsp:useBean>

Here, the Jsp:setproperty are executed only if a new object was instantiated, not if a existing one was found.

There are four possible attributes of Jsp:setproperty:

Attribute Usage
Name This required attributes designates the bean whose property would be set. The Jsp:usebean element must appear before the Jsp:setproperty element.
Property This required attribute indicates for you want to set. However, there is one special case:a value of "*" means ' all request parameters whose names match bean property names Would be passed to the appropriate setter methods.
Value This optional attribute specifies the value to the property. String values are automatically converted to numbers, Boolean, Boolean, Byte, Byte, Char, and Character via the standard V Alueof method in the target or wrapper class. For example, a value of ' true ' for a Boolean or Boolean would be converted via boolean.valueof, and a value of ' 42 "For a int or Integer property'll be converted via integer.valueof." You can ' t use both value and param, but it are permissible to use neither. The discussion of param below.
param This optional attributes designates the request parameter from which to the property should to be derived. If the current request has no such parameter, the It is done:the system does not pass NULL to the setter method of the P Roperty. Thus, you can let the bean itself supply default values, overriding them the ' when ' request parameters say to do. For example, the following snippet says "set the Numberofitems property to whatever the value of the NUMITEMS request para Meter is, if there is such a request parameter. Otherwise don ' t do anything. "
<jsp:setproperty name= "Orderbean"
Property= "Numberofitems"
param= "NumItems"/>
If you omit both value and Param, it's the same as if you supplied a param name of the property name. Can take this idea of automatically using the ' request property whose name matches the ' property one step further by SUP Plying a property name of "*" and omitting both value and Param. In this case, the server iterates through available properties and request parameters, matching up ones with identical Nam Es.


Here's an example which uses a bean to create a table of prime numbers. If there is-a parameter named Numdigits in the request data, it's passed into the bean's Numdigits property. Likewise for Numprimes.

jspprimes.jsp
To download the JSP source, right click on the source code link. You can also download the "source code for" Numberedprimes Bean referenced by the Jsp:usebean element. Browse the source code directory for other Java classes used by Numberedprimes. The best way to try it out on-line are to start and the HTML page that acts as a front end to it.

<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<HTML>
<HEAD>
<title>reusing JavaBeans in Jsp</title>
<link Rel=stylesheet
href= "My-style-sheet.css"
Type= "Text/css" >
</HEAD>

<BODY>

<CENTER>
<table border=5>
<tr><th class= "TITLE" >
Reusing JavaBeans in jsp</table>
</CENTER>
<P>

<jsp:usebean id= "primetable" class= Hall. Numberedprimes "/>
<jsp:setproperty name= "primetable" property= "Numdigits"/>
<jsp:setproperty name= "primetable" property= "Numprimes"/>

Some <jsp:getproperty name= "primetable" property= "Numdigits"/>
Digit primes:
<jsp:getproperty name= "primetable" property= "Numberedlist"/>

</BODY>
</HTML>

Here ' s a typical result:

8.5 The Jsp:getproperty Action
This element retrieves the "value of" a bean property, converts it to a string, and inserts it into the output. The two required attributes are name, the name of a bean previously referenced via Jsp:usebean, and, the property Whose value should be inserted. Here's an example; For more examples, Sections 8.2 and 8.4.

<jsp:usebean id= "Itembean" .../>
...
<UL>
<li>number of items:
<jsp:getproperty name= "Itembean" property= "NumItems"/>
<li>cost of each:
<jsp:getproperty name= "Itembean" property= "UnitCost"/>
</UL>

8.6 The Jsp:forward Action
This action lets your forward the request to another page. It has a single attribute, page, which should consist of a relative URL. This could is a static value, or could is computed at request time, as in the two examples below.

<jsp:forward page= "/utils/errorreporter.jsp"/>
<jsp:forward page= "<%= somejavaexpression%>"/>

8.7 The Jsp:plugin Action
This action lets your insert the browser-specific OBJECT or EMBED element needed to specify which the browser run an applet Using the Java plugin.

9. Comments and Character quoting conventions
There are a small number of special constructs can use in various cases to insert comments or characters that would ot Herwise be treated specially. Here ' s a summary:

Syntax Purpose
<%--Comment--%>
A JSP comment. Ignored by Jsp-to-scriptlet translator. Any embedded JSP scripting elements, directives, or actions are ignored.
<!--comment-->
An HTML comment. Passed through to resultant HTML. Any embedded JSP scripting elements, directives, or actions are executed.
<\%
Used in template text (static HTML) where you really Want "<%".
%\>
Used in scripting elements where you really want "%>".
\'
A single quote In the attribute that uses single quotes. Remember, however, that's you can use either single or double quotes, and the other type of quote'll then be a regular cha Racter.
\"
A double quote in an attribute that uses double quotes. Remember, however, that's you can use either single or double quotes, and the other type of quote'll then be a regular cha Racter.
%\>
%> in an attribute.
<\%
<% in an attribute.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.