Step-by-Step JSP + JavaBean tutorial (6)

Source: Internet
Author: User
Tags microsoft frontpage
This section involves two pages. One donewuser. jsp file is used to add records, and the other listuser. jsp file is used to add records.
Displays information about all registered users. The two pages involve the specific call of JavaBean. Let's take a look at the file,
Comments are added to key code in the file for your understanding.

Donewuser. jsp file

Note: The user registration operation page displays the corresponding feedback based on whether the user registration is successful. The main feature of this page is to use
The addnewuser () method of the JavaBean lyf. adduser method is used to add records.

<% @ Page contenttype = "text/html; charset = gb2312" %>
<% Response. setheader ("expires", "0"); %>
<! -- Generate a JavaBean: lyf. adduser instance. The ID is adduser and the survival range is page -->
<JSP: usebean id = "adduser" class = "lyf. adduser" Scope = "page"/>

<! -- Set the values of each attribute in the JavaBean, which calls the Set Method of each attribute in the JavaBean to obtain
The correct attribute value. "*" indicates matching all attributes -->

<JSP: setproperty name = "adduser" property = "*"/>
<HTML>
<Head>
<Meta http-equiv = "content-language" content = "ZH-CN">
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Meta name = "generator" content = "Microsoft FrontPage 3.0">
<Meta name = "progid" content = "FrontPage. Editor. Document">
<Title> Add a user </title>
</Head>
<Body bgcolor = "# ffebbd">
<Div align = "center"> <center>
<%

// Call the checkuser () method of lyf. adduser to check whether there are duplicate usernames

// If there are duplicates, the corresponding information is displayed.

If (! Adduser. checkuser ())
{

// Page text output information, using the println method of the embedded JSP object out, equivalent to the response. Write method in ASP

Out. println ("sorry, this user name" + adduser. GetUserName () + "has been applied. Please reselect it! ");

// Return indicates that the return is returned, and the following processing will not be performed when the return is run. The function is equivalent to response. End in ASP.

Return;
}
%>
<%
// If no duplicate user names exist, call the addnewuser () method of lyf. adduser to add user data to the database, and
Displays the corresponding information based on whether the data is successfully added.

If (adduser. addnewuser ()){
%>
<H2> User Added successfully!

<H2> failed to add user. Please contact the administrator!

</Body>
</Html>

Listuser. jsp file

Note: The user information list page displays all registered user information and displays data by page.

For ease of use, General paging code is adopted. If it is jdbc2.0 or later or other support
Type_scroll_insensitive cursor database driver, you can have a more concise paging method.

The statements similar to those on the preceding JSP page will not be explained.
This page imports the class library of Java. SQL. resultset, because the resultset must be declared in the middle of the JSP page;
The Oracle. JDBC. Driver. * class library is a dedicated JDBC driver for Oracle, allowing JSP pages to be used for Oracle database operations.

<% @ Page contenttype = "text/html; charset = gb2312" %>
<% Response. setheader ("expires", "0"); %>
<% @ Page import = "Java. SQL. resultset" %>
<% @ Page import = "oracle. JDBC. Driver. *" %>
<! -- Generate a JavaBean: lyf. DB instance -->
<JSP: usebean id = "DB" class = "lyf. DB" Scope = "request"/>
<JSP: setproperty name = "DB" property = "*"/>
<%

Java. Lang. String strsql; // SQL statement

Int intpagesize; // number of records displayed on one page
Int introwcount; // The total number of records.
Int intpagecount; // the total number of pages.
Int intpage; // the page number to be displayed.
Java. Lang. String strpage;
Int I, J, K;
// Set the number of records displayed on one page
Intpagesize = 15;
// Obtain the page number to be displayed
Strpage = request. getparameter ("page ");
If (strpage = NULL) {// indicates that the page parameter is not found in querystring. the first page of data is displayed.
Intpage = 1;
}
Else {// convert a string to an integer
Intpage = java. Lang. Integer. parseint (strpage );
If (intpage <1) intpage = 1;
}
// Obtain the total number of records
Strsql = "select count (*) from user ";
Resultset result = db.exe cutequery (strsql); // execute the SQL statement and obtain the result set.
Result. Next (); // when the record set is opened, the pointer is located before the first record.
Introwcount = result. getint (1 );
Result. Close (); // close the result set.
// Calculate the total number of pages
Intpagecount = (introwcount + intPageSize-1)/intpagesize;
// Adjust the page number to be displayed
If (intpage> intpagecount) intpage = intpagecount;
Strsql = "select * from user order by id desc ";
// Execute the SQL statement and obtain the result set
Result = db.exe cutequery (strsql );
// Locate the record pointer to the first record on the page to be displayed.
I = (intPage-1) * intpagesize;
For (j = 0; j
<HTML>
<Head>
<Meta http-equiv = "content-language" content = "ZH-CN">
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Meta name = "generator" content = "Microsoft FrontPage 3.0">
<Meta name = "progid" content = "FrontPage. Editor. Document">
<Title> User List </title>
</Head>
<Body bgcolor = "# ffebbd">
<Div align = "center"> <center>
<Table border = "1"
Bordercolordark = "# ffffff" bordercolorlight = "#000000" cellspacing = "0" Height = "22"
Width = "100%">
<Tr bgcolor = "# ffebad">
<TD Height = "1" width = "691" class = "Main">
Page
<A href = "listuser. jsp? Page = 0 "> Homepage
<A href = "listuser. jsp? Page = "> Previous Page
Previous Page
<A href = "listuser. jsp? Page = "> next page
Next Page
<A href = "listuser. jsp? Page = "> last page
<Input type = "text" class = "Main" name = "page" size = "3" value = ""
Tabindex = "1"> page <input type = "Submit" class = "Main" value = "go" name = "B1" tabindex = "2">

<Table border = "1" width = "100%" cellspacing = "0" bordercolorlight = "#000000"
Bordercolordark = "# ffffff" class = "Main">
<Tr bgcolor = "# ffebad">
<TD>
<Div align = "Left"> User Name

<TD>
<P align = "center"> email

<TD>
<P align = "center"> Home Page

<TD>
<P align = "center"> Registration Time

<TD>
<P align = "center"> description

<Tr bgcolor = "# ffebad">
<TD>
<Div align = "Left">
<TD> <Div align = "center">
<TD> <Div align = "center"> <font color = "# 0000cc">

<TD> <Div align = "center"> <font color = "# ff6666">

<TD> <Div align = "center"> <font color = "# 0000ff">

</Table>

</Body>
</Html>
  
Run the newuser. jsp file to register the user, and then run the listuser. jsp file to check whether
Add to database. For details about the directory where the JSP file and class file are stored, refer to the reference of the specific JSP server software,
The simplest method is to run it directly with jbuilder4.0 because it comes with the Tomcat server software.

Now, the introduction of JSP + JavaBean is basically over. Through the above learning, how should we know about JavaBean?
I have a basic understanding of JSP programs, and the rest is to apply and play in specific programs.

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.