JSP Elements
(1) Directive Elements
Occurrence time: compiled and executed during compilation
(2) script elements
(3) Action elements
Occurrence time: each time a client request is sent, it is executed once.
• An action is a special identifier. You can use an action tag to implement multiple lines of Java code. You can dynamically Insert Files, reuse the JavaBean Component, and direct to another page.
• Different from the command element, the action element is dynamically executed during client requests and may be executed once each time a client request is sent, the command element is compiled and executed only once during compilation.
Action Element category:
1. <JSP: usebean> ---- JSP is the prefix and usebean is the name
Function: Creates or returns the object of an entity bean component.
Two conditions required by JavaBean:
(1) the class is public and has a default construction method without parameters.
(2) several private attributes can be defined in the class, but these attributes must be assigned and valued through the public getter/setter method, and the getter/setter method cannot be overloaded.
Note:
• In programming, the JavaBean class must use the package statement;
• Public, no parameter constructor;
• The getter/setter method must be public and start with set and get respectively. The method naming rules follow the Java Naming rules.
• When the attribute type is Boolean, The get of the getter method can be replaced by is.
Some other methods contained in JavaBean:
(1) <JSP: usebean>
-Usebean actions are used to create references and embed existing Bean components into JSP.
-Usebean Syntax:
<JSP: usebean id = "beanname" class = "beanclass" Scope = "Page | request | session | application"/>
Example:
<! -- Function:
Book = (book) Session. getattribute ("book ");
If (book! = NULL ){
Book = New Book ();
Session. setattribute ("book", book );
}
Return book;
The getattribute method is used to obtain the book object. If the book is empty, this object is created and
It is set to the scope of the session. If it is not null, the reference is obtained from the session. All return -->
<JSP: usebean id ="Book"Class =CN. csdn. Beans. Book"Scope ="Session"> </Jsp: usebean>
(2) <JSP: setproperty>
-The setproperty action is used to set the attribute value of the bean specified in usebean. The setproperty Action specifies the name, attribute name, attribute value, and parameter of the JavaBean, which is used to assign the bean attributes.
-Setproperty Syntax:
<JSP: setproperty name = "beanalias"
Property = "propertyname" value = "value"
Param = "parameter"
<JSP: setproperty property ="Bean object property name"Name ="Bean object name"Value ="Bean object Value"/>
Instance:
<JSP: setproperty name = "book" property = "name" value = "Java SE programming skills"/>
Equivalent to <% book. setname ("Java SE programming lecture"); %>
(3) <JSP: getproperty>
-The getproperty action is used to obtain the value of the attribute specified in the bean. The system first converts the received value to a string and then sends it as the output result.
-Getproperty Syntax:
<JSP: getproperty name = "beanalias" property = "propertyname"/>
<JSP: getproperty property ="Bean object property name"Name ="Bean object name"/>
Out. println (bean object. getxxx (); // has the output function
For example, <JSP: getproperty name = "book" property = "name"/>
Equivalent to: <% = book. getname () %>
Benefits of using JavaBean
-The JavaBean technology is more conducive to the reuse of web development code. Because Java code snippets written on JSP pages are less reusable, and JavaBean is a Java class that facilitates reuse in other web applications.
-The JavaBean technology is also conducive to the division of roles of development projects, making it easier for page designers and programmers to modify the content on their own pages without interfering with each other.
2. <JSP: Include>
-The include action is used to merge content from other HTML pages or JSP pages to the current page, or insert files into the current page.
-Include action without parameters:
<JSP: Include page = "weburl" Flush = "true"/>
-Include action with parameters:
<JSP: Include page = "weburl" Flush = "true">
<JSP: Param name = "paramname" value = "paramvalue"/>
[Use the JSP page of The include action to obtain the parameter value passed by the included file through request. getparameter ("name .]
<JSP: Include>
Differences between include commands and <JSP: Include> actions
-The include command reads the content of the specified page and combines the content with the original page. This process occurs when JSP is converted into servlet, and the final JSP file is converted into servlet by the servlet container. If a JSP page uses the include command to contain another JSP page, only one servlet Java source file and one class file are generated. It is equivalent to the integration of troops.
-When you use the <JSP: Include> action to include other JSP pages, the jspservice () method of the servlet object converted by the contained file is essentially called. That is, the included JSP file should also generate the Servlet Source file and class file. If a JSP page uses the include action to include another JSP page, two Servlet Source files and two class files are generated.
Both can contain JSP pages and HTML pages.
Action elements include:
<JSP: Include page = "url address of the included file"> the contained action is executed multiple times. When each client submits a request, two servlets generate
Command elements include:
<% @ Include file = "url address of the included file"> In the conversion phase, only one servlet is generated.
3,<JSP: Forward>
-The forward action is used to redirect the current JSP page to another page (HTML files, JSP pages, and servlets in the same web application ).
-The address is the address of the current page. The content is the content of another page.
-Forward action without parameters: <JSP: Forward page = "url"/>
-Include action with parameters:
<JSP: Forward page = "url">
<JSP: Param name = "paramname" value = "paramvalue"/>
</Jsp: Forward>
The redirection using <JSP: Forward> is exactly the same as the redirection code in servlet.
4,<JSP: param>
-Param provides the name/value information for other JSP actions.
-Param Syntax:
<JSP: Param name = "paramname" value = "paramvalue"/>
5,<JSP: plugin>
-The Applet and bean plug-in used to connect to the client. When processing this action, JSP will output two different HTML elements, namely object and embed, based on the browser on the client.
JSP has three annotation Methods
1. html comments (output comments): the comments can be seen when the client views the source code. For example,
<! -- This is an HTML comment. It will show up int the response. -->
2. jsp page comment (hidden comment): The comment is written in the JSP program but not sent to the customer. Therefore, the Comment cannot be seen when the client views the source code. Such annotations are ignored during JSP compilation.
<% -- This is a JSP comment. It will only be seen in JSP code -- %>
3,Java Note: it can only appear in the Java code area and cannot appear directly on the page.// Single line comment/* multi-line comment */