Skip navigation

Lu's Notes

down to bottom of page

JSP - JavaServer Pages Notes

When you use a JavaBean in your JSP you must use the <jsp:useBean> tag first, before you make any other references to this tag. After the <jsp:useBean> tag, you also have to perform any initialization steps specified by the bean developer before you can make use of the bean... by creating a "scriptlet" similar to <% beanName.setRandomIndex( );%>

<jsp:useBean  id="beanName" class="BeanClass" scope="beanScope" />
   Same as...  BeanClass beanName = new BeanClass();

This tag allows you to embed JavaBeans, which are small Java programs, in your JSP pages.
id: The id attribute is the reference name that you assign to the bean and that you use to make subsequent references to the bean with other JSP tags. Although the <jsp:useBean> tag is the only standard tag using an id attribute, you can use custom tags that also have an id attribute. The role of the id attribute in all uses is to define the name of a variable to be created by the tag. Further an id must be unique across a JSP page, regardless of the type of tag (standard or custom) in which it is used.
class: The class attribute is the class name of the JavaBean. It needs to be a fully qualified class name, which means you provide the name of the JavaBean with the package hierarchy in which it exists. (class and type attributes are interchangeable, or they may both be used... however it is best to avoid using the type attribute altogether...)
scope: The scope attribute tells the JSP engine how long to maintain a copy of this bean for you. There are four different scope choices:

page: The page scope is the narrowest of scopes. The page scope beans are like temp workers -- they do all the work and are the first to get axed when a job is done. Beans of this scope can only be used by the page in which they are declared.
request: The request scope means that the bean exists (will be available) from the time that it is declared until the time that a JSP page is sent back to the user who requests it. Beans in the request scope are visible to all JSP pages that process a particular user request, and are not visible to any other request.
session: The session scope is both a scope and a Java object. JavaBeans of the session scope are stored in the session object. Each user has it's own session object, and that object is available for the user as long as the user is actively using your Web site. The session scope JavaBeans are typically used to store global information about a particular user, such as their identity and preferences. JavaBeans remain in the session until the session expires, or until you delete them from the session.
application: The application scope beans are visible to all users of your Web site. From the time that you declare an application scope bean, all users of your Web site will have access to that bean and its contents. Because application beans are so accessible, they are typically used for very limited circumstances. One typical example is a hit counter to show how many times your Web site has been visited.

<jsp:getProperty  name="beanName" property="propertyName" />
   Same as...  beanName.getPropertyName();

This tag allows you to get a value from a JavaBean property. You use this tag to get dynamic data from the JavaBean and place it in the JSP.
name: The name attribute refers to the id that you supply to the bean in the <jsp:useBean> tag described above.
property: The property attribute refers to the name of the property that you want to get out of the JavaBean.

<jsp:setProperty  name="beanName" property="propertyName" value="propertyValue" />
   Same as...  beanName.setPropertyName("propertyValue");

This tag allows you to assign a value to a JavaBean property. A JavaBean property is basically a variable in a JavaBean that holds some information.
name: The name attribute refers to the id that you supply to the bean in the <jsp:useBean> tag described above. This identifies which JavaBean the property belongs to.
property: The property attribute identifies which property in the specified the bean that you want to set.
value: The optional value attribute identifies the value that you want to set the property to. The value attribute cannot be used if the param attribute is being used.
param: The optional param attribute identifies the name of the parameter submitted by the user that you wish to assign to this property. A parameter submitted by the user will have the same name as the HTML form element that contained the user input. The param attribute cannot be used if the value attribute is being used.

All  JSP  action tags should have a "closing."
The empty tag, has no body and "closes within the tag at end":
<jsp:useBean id="beanName" class="beanClass" scope="beanScope" />
The tag with body has "a closing tag" following the body:
<jsp:useBean id="beanName" class="beanClass" scope="beanScope"> body of bean declaration, where properties of the bean can be set and initialized before the page is granted access to the bean </jsp:useBean>
Warning: If the JSP container finds a bean with the scope and id of the <jsp:useBean> tag, the body of the <jsp:useBean tag> is ignored.

The <jsp:include> tag allows you to include the content of another JSP in the current page. When using an "include," the final output of JSP must be a properly formatted HTML document... one opening and one closing head or body tag, etc...
     Some attributes of the include tag are: page="{pageURL}" and flush="true"
The <jsp:forward> tag allows you to direct a user from one JSP to another on your Web server. This is often referred to as a redirect because you force the user along a different path than the one they originally requested.
The <jsp:plugin> tag allows you to embed an applet or a visual JavaBean into your JSP and send it to the user's browser. Applets and visual JavaBeans are little Java programs run by a host program called a plug-in, which is located in the user's browser.
The <jsp:param> tag is nested inside <jsp:include>, <jsp:forward> or <jsp:plugin> tags, and is used to send parameter values to the included JSP or plug-in programs.
The <jsp:params> tag is nested inside the <jsp:plugin> tag and contains all the <jsp:param> tags for the plug-in program. The <jsp:params> tag is only used with <jsp:plugin> tags.



Also see my Java Notes  or  Java Code samples



up to top of page   Return to Top of Page   up to top of page up to top of page