Common JSP commands

Source: Internet
Author: User
Tags rowcount

I. Directive Elements
1. Page command
Import
Session
Contenttype
Buffer
Istreadsafe
Info
Errorpage
Iserrorpage
2. include command
3. taglib command
Ii. script elements
1. Declare Elements
2. Expression Element
3. script elements
4. annotation Elements
3. Standard action elements
1. <JSP: param>
2. <JSP: Include>
3. <JSP: Forward>
4. <JSP: plugin>
5. <JSP: usebean>
6. <JSP: setproperty>
7. <JSP: getproperty>
4. built-in objects
1. Request
2. Response
3. Out
4. Session
5. pagecontext
6. Application
7. config
8. Page
9. Exception
V. Use of JavaBeans
1. Basic Format of JavaBeans in JSP
2. Specific Scope settings
3. Application of session events
4. Saving and reading beans
Vi. File Operations in JSP
VII. Analysis of JSP operating principles
-------------------------------------------------

In the early days, the development of network database applications mainly adopted CGI (Common Gateway Interface) technology. You can write CGI programs in different programming languages, such as Perl, Visual Basic, Delphi, and C/C ++. Although CGI technology is mature and powerful, it has been replaced by new technologies due to its difficult programming, low efficiency, and complicated modification.
In this context, new technologies are available, such as ASP (Active Server Page), PHP (personal home page), and JSP (Java Server Page. Among them, JSP is regarded by many people as the most promising dynamic website technology in the future.
JSP pages are generally composed of HTML tags and JSP elements, the JSP elements are composed of four parts: "command element", "script element", "Standard action element", and "built-in object. Next, let's explore the mysteries of JSP ......

I. Directive Elements

JSP can be understood as a message used to notify the JSP Engine. JSP does not directly generate visible output. use JSP commands to set the JSP Engine's mechanism for processing JSP pages.
Generally, JSP commands use labels <% @... %> Indicates that JSP Commands include page, include, and taglib. The page command is a command for the current page, and the include command is used to specify how to include another file. The taglib command is used to define and access the custom tag library. These three commands usually have default values, so developers do not need to explicitly use each command for confirmation.
1. Page command
The syntax format of the page command is: <% @ page attribute1 = "value1" attribute2 = "value2 "... %>
The following describes several common attributes in the instruction and briefly introduces them.
L import
The import command is the only command that can be set multiple times in all page commands, and each setting is accumulated. It is used to specify the classes used in JSP web pages. For example:
<% @ Page import = "Java. Io. *, java. util. Date" %>
L session
Determines whether the current page participates in an HTTP session. If it is set to "true", an object with the implicit name session can be obtained. If it is "false", no. The default value is "true ".
L contenttype
The character compression mode and character set used when the JSP webpage outputs data. When compiling a Chinese webpage, the settings are as follows:
<% @ Page contenttype = "text/html; charset = gb2312" %>
The default value for this property is "text/html; charset = ISO-8859-1 ".
L Buffer
Set the buffer size of the JSP webpage. The default value is "8 K". If it is set to "NONE", no buffer is used. All response outputs will be directly written to servletresponse by printwriter.
L istreadsafe
Defines whether the current page supports thread security. If it is "true", the page may receive multiple requests from the JSP engine at the same time. Otherwise, the JSP Engine queues the received requests, the current page can only process one request at a time. The default value is "true ".
L info
Set the text information of the page. You can use servlet. getservletinfo () to obtain the string.
L errorpage
Defines the URL pointing to another JSP page. When an uncaptured exception occurs on the page, the error message is throw in the throw statement, and the JSP page of the webpage is set as the error message. The exception implicit object is used to obtain the error message.
No error handling page by default.
L iserrorpage
Set whether the JSP page is an error handling page. The default value is "false ". When it is set to "true", the JSP page can access the hidden exception object and obtain the error message from the webpage with the error. Syntax for getting error information:
<% = Exception. getmessage () %>
2. Example of page error handling
The error page file is makeerror. jsp, and the error page file is errorpage. jsp. Their source code is as follows:
Makeerror. jsp

<% @ Page errorpage = "errorpage. jsp" %>
<HTML>
<Head>
<Title> error page </title>
</Head>
<Body>
<%
Int I = 8, j = 0;
Out. println (ij );
%>
</Body>
</Html>
Errorpage. jsp
<% @ Page iserrorpage = "true" %>
<HTML>
<Head>
<Title> error handling page </title>
</Head>
<Body>
<Font color = Red>
Error cause: <% = Exception. getmessage () %>
</Font>
</Body>
</Html>

The result of running program makeerror. jsp is as follows:

2. include command
You can use the include command to add other text files to the current JSP page. The format is as follows:
<% @ Include file = "header. Inc" %>
In this case, add the header. inc source code to the current page and then compile the entire file.
You can use the include command to divide a page into different parts, and finally synthesize a complete file. Using the include command of JSP helps to modularize the JSP page.
3. taglib command
(Omitted)

Ii. script elements

The JSP specification provides four types of script elements, including:
L statement
L Expression
L script
L Annotation
They are described in detail below.
1. Declare Elements
Declarations are used to define variables and functions on the JSP page. These Defined variables and functions will become attributes and methods of the servlet class (for details about servlet, refer to the following article ). Declaration does not generate any data output. You can set the initial value for other declarations, expressions, or scripts.
The declared syntax format is:

<%!
// Statement
%>
Example:
<%!
// The variables defined here will be the global variables on this JSP page
Int I = 0;
Static Int J = 100;
String S = "NOTE ";
%>
<%!
// The function defined here will be a public function on this JSP page
Public int square (int I)
{
Return (I * I );
}
%>

2. jspinit and jspdestroy Functions
To initialize some data at the beginning of execution on the JSP page, use the jspinit function. This function will be called when the JSP page is executed and will not be executed again when the JSP page is reorganized. When the server is shut down, the jspdestroy function will be executed. This function can be used to handle the aftermath of the data. The following is a simple example. The initdes. jsp file code is as follows:

<% @ Page contenttype = "text/html; charset = gb2312" %>
<%!
Public void jspinit ()
{
System. Out. println ("jspinit is called! ");
}

Public void jspdestroy ()
{
System. Out. println ("jspdestroy is called! ");
}
%>
<HTML>
<Head> <title> Use of jspinit and jspdestroy functions </title> <Body>
<Center>
<Font size = 5 color = blue> Use of jspinit and jspdestroy functions </font>
</Center>
<HR> <br>
</Body>
</Html>

When executing this page for the first time, the Resin server output the following:
Resin 1.2.2 -- Tue Jan 16 09:53:18 PST 2001
HTTP listening to *: 8080
Srun listening to 127.0.0.1: 6802
Jspinit is called!
After the page is refreshed several times, the output of the Resin server is still as above.
If the server is disabled, the output is as follows:
Resin 1.2.2 -- Tue Jan 16 09:53:18 PST 2001
HTTP listening to *: 8080
Srun listening to 127.0.0.1: 6802
Jspinit is called!
Closing Server
Jspdestroy is called!
As a result, we were inspired to use the jspinit function to connect to the database during database development, and use the jspdestroy function to complete database operations. The following uses a program that displays database content by page as an example to help readers further understand the functions and benefits of jspinit and jspdestroy.
In the paging program pages. jsp, we write the database connection action in the jspinit function, so that every time the page is reorganized, we can avoid re-executing the database connection action. As follows:

<% @ Page contenttype = "text/html; charset = gb2312"
Import = "Java. SQL. *" %>
<%!
Int pagesize = 2; // you can specify two records for each webpage.
Int showpage = 1; // you can specify the number of pages to display.
Int rowcount = 0; // number of records in the resultset
Int pagecount = 0; // the total number of pages after the resultset page
Connection con = NULL;
Statement stmt = NULL;
Resultset rs = NULL;

Public void jspinit () // initializes the database and related data.
{
Try
{
Class. forname ("Sun. JDBC. ODBC. jdbcodbcdriver ");
// Load the driver category

Con = drivermanager. getconnection ("JDBC: ODBC: test ");
// Create a database link

Stmt = con. createstatement (resultset. type_scroll_insensitive,
Resultset. concur_read_only );
// Create a statement object and set the record indicator type to move before and after

Rs = stmt.exe cutequery ("select * from products ");
// Create a resultset object and execute an SQL statement

Rs. Last (); // move the indicator to the last record

Rowcount = Rs. getrow (); // gets the number of records in the resultset.

Pagecount = (rowcount % pagesize) = 0?
(Rowcountpagesize): (rowcountpagesize) + 1 );
// Calculate the number of displayed pages
}
Catch (exception ex)
{
System. Out. println (ex. tostring ());
}
}

Public void jspdestroy () // execute the operation to close various objects
{
Try
{
Rs. Close (); // close the resultset object
Stmt. Close (); // close the statement object
Con. Close (); // closes the database link object
}
Catch (exception ex)
{
System. Out. println (ex. tostring ());
}
}
%>
<HTML>
<Head>
<Title> display records by page </title>
</Head>
<Body>
<Center>
<Font size = 5 color = blue> pagination of records </font>
</Center>
<HR>
<P> </P>
<Center>
<%
String topage = request. getparameter ("topage ");

// Determine whether the topage parameter can be obtained correctly,
// The statement indicating that the JSP page should display a specific page record
If (topage! = NULL)
{
Showpage = integer. parseint (topage); // obtain the specified page number

// The following if statement determines whether the number of pages entered by the user is correct
If (showpage> pagecount)
{// Determines whether the specified page number is greater than the total page number. If yes, the last page is displayed.
Showpage = pagecount;
}
Else if (showpage <= 0)
{// If the specified page number is smaller than 0, the first page of records is displayed.
Showpage = 1;
}
}

Rs. Absolute (showpage-1) * pagesize + 1 );
// Calculate the position of the first record on the page to be displayed
%>
<H3> currently in the <font size = 4 color = Red>
<% = Showpage %> </font> page, total
<Font size = 4 color = Red>
<% = Pagecount %> </font> page <P> </P>
<%
// Use the for loop with the pagesize attribute to output records on the page
For (INT I = 1; I <= pagesize; I ++)
{
%>
<Table border = 1 bordercolor = royalblue bgcolor = lightblue>
<Tr> <TD bgcolor = lightyellow width = 100>
<B> product name </B> </TD>
<TD width = 100> <B> <% = Rs. getstring ("product_name") %>
</B> </TD>
<TD bgcolor = lightyellow width = 100>
<B> price </B> </TD>
<TD width = 100> <B> <% = Rs. getint ("price") %>
</B> </TD>
<TD bgcolor = lightyellow width = 100>
<B> description </B> </TD>
<TD width = 100> <B> <% = Rs. getstring ("Description") %>
</B> </TD>
</Tr>
</Table> <br>
<%
// The following if judgment statement is used to prevent the output of the last page record,
// Move the record indicator after the last record
If (! Rs. Next () // determines whether the last record is reached
Break; // jump out of the For Loop
}
%>
<Table>
<Tr valign = baseline align = center>
<%
// Determine whether the current page is the first page,
// If not, the hyperlink between the first page and the previous page is displayed.
If (showpage! = 1)
{
// The hyperlinks created below will be linked to you,
// Pass the page to be displayed with the topage parameter to itself
%>
<TD width = 150>
<A href = pages. jsp? Topage = <% = 1%> to the first page </a>
</TD>
<TD width = 150>
<A href = pages. jsp? Topage = <% = showpage-1%> to the previous page </a>
</TD>
<%
}

// Determine whether the current page is the last page,
// No. The hyperlink between the last page and the next page is displayed.
If (showpage! = Pagecount)
{
// The hyperlinks created below will be linked to you,
// Pass the page to be displayed as the topage Parameter
%>
<TD width = 150>
<A href = pages. jsp? Topage = <% = showpage + 1%> to the next page </a>
</TD>
<TD width = 150>
<A href = pages. jsp? Topage = <% = pagecount %> to the last page </a>
</TD>
<%
}
%>
<TD width = 150>
<Form action = pages. jsp method = post>
To
<! --
The text box for users to enter the number of pages to view. The default value is the current page,
After the user completes the data input in this text box, press enter to send the data,
It is equivalent to pressing the submit button, so the submit button will be omitted in this form
-->
<Input type = "text" name = topage style = "height: 25px; width: 40px"
Value = <% = showpage %> page
</Form> </TD> </tr>
</Table>
</Center>
</Body>
</Html>

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.