How to use javabeans in jsp? How to use a defined class ?, Jspjavabeans

Source: Internet
Author: User

How to use javabeans in jsp? How to use a defined class ?, Jspjavabeans

How to use javabeans?

About JavaBean

JavaBeans are Java classes that comply with certain specifications. The advantage of using JavaBeans is to solve code duplication and reduce code redundancy. The functions are clearly differentiated, improving Code maintainability.

Design Principles of JavaBean

Public class, public constructor without parameters, private property, getter, and setter methods.

What is JSP action?

JSP action elements provide information for the request processing stage. The Action Element follows the syntax of the XML element. It has a start tag containing the element name, which can have attributes, optional content, and end tags matching the start tag.

JSP action elements include five types:

The first category is related to the access to JavaBean, including:

<Jsp: useBean> <jsp: setProperty> <jsp: getProperty>

The second type is the basic elements starting with JSP1.2, including the six brother action elements.

<Jsp: include> <jsp: forward> <jsp: param> <jsp: plugin> <jsp: params> <jsp: fallback>

The third type is the newly added elements of JSP2.0, which are mainly related to JSP Document, including six elements.

<Jsp:> <jsp: root> <jsp: declaration> <jsp: scriptlet> <jsp: expression> <jsp: text> <jsp: output>

The fourth type is the newly added Action Element of JSP2.0. It is mainly used to dynamically generate the value of the XML Element tag, including three actions.

<Jsp: attribute> <jsp: body> <jsp: element>

The fifth type is JSP2, which is a newly added Action Element of 0. It is mainly used in Tag File and has two elements.

<Jsp: invoke> <jsp: dobody>

Use JavaBeans in JSP pages

1. Create a JavaBean instance like a normal Java class.

2. JSP pages usually use JSP action labels using JavaBean.

Common JSP action labels are useBeans action, setProperty action, and getProperty action.

<Jsp: useBeans>

Purpose: instantiate a JSP page or Use JavaBean within the specified range:

Format: <jsp: useBean id = "identifier" class = "java class name" scope = "scope"/>

The id is the instance corresponding to JavaBean.

<Jsp: setProperty>

Purpose: assign values to attributes of the instantiated JavaBean object in four forms:

<Jsp: setProperty name = "JavaBean Instance name" property = "*"/> (associated with the form)

<Jsp: setProperty name = "JavaBean Instance name" property = "JavaBean attribute name"/> (associated with the form)

<Jsp: setProperty name = "JavaBean Instance name" property = "JavaBean attribute name" value = "BeanValue"/> (manually set)

<Jsp: setProperty name = "JavaBean Instance name" property = "propertyName" param = "parameter name in the request object"/> (associated with the form) (attributes can be assigned values by passing parameters through URLs)

<Jsp: getProperty>

Purpose: Get the attribute value of the specified JavaBean object.

Format: <jsp getProperty name = "JavaBean Instance name" property = "property name"/>

The four scopes of JavaBean

Note: The scope attribute of useBeans can be used to specify the scope of application of javabean:

Page // only valid on the current page

Request // you can use HttpRequest. getAttribute () to obtain the JavaBean object.

Session // you can use HttpSession. getAttribute () to obtain the JavaBean object.

Application // you can use the application. getAttribute () method to obtain the JavaBean object.

How to use a defined class?

<% @ Page import = "com. ideal. dao. comatrix ayappdao">

Among them, the class comatrix ayappdao is the dao class under com/ideal/dao in the Code src package, so that you can use the public method in the class just like writing code in the java class on the jsp page,

For example, assume that the dao class has the public method getPayAmount (String s)

<%

Comatrix ayappdao dao = new comatrix ayappdao ();

String comm_code = "018876666"; // merchant ID

Dao. getPayAmount (comm_code); // obtain the payment amount based on the merchant ID

%>

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.