Jsp+javabean step-by-Step tutorial (vi)

Source: Internet
Author: User
Tags count generator microsoft frontpage tomcat server oracle database
js| Tutorial This section covers two pages, one donewuser.jsp file to implement record add operations, another file listuser.jsp file
Used to display all of the registered user information. Both of these pages involve a specific call to the JavaBean, or look at the file,
Comments are added to the key code in the file for easy understanding.

donewuser.jsp file

Description: The user registers the Operation page, and according to the user registration success whether displays the corresponding feedback information, this page main characteristic is uses the
Lyf.adduser this JavaBean addnewuser () method to add records.

<%@ page contenttype= "text/html;charset=gb2312"%>
<% Response.setheader ("Expires", "0"); %>
<!--generates an JavaBean:lyf.adduser instance with ID AddUser, page--> Survival Range
< Jsp:usebean id= "AddUser" class= "Lyf.adduser" scope= "page"/>

<!--set the values of each property in the JavaBean, which calls the set method of each property in JavaBean so that JavaBean gets
The correct property value, "*" represents a match for all properties-->

< 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> user Add </title>
< body bgcolor= "#FFEBBD" >
< div align= "center" >< center>
<%

Call the Lyf.adduser checkuser () method to check for duplicate user names

Display the corresponding information if there are duplicates

if (!adduser.checkuser ())
{

Page text output information, using the JSP built-in object out of the Println method, equivalent to the ASP Response.Write method

Out.println ("Sorry, this username" +adduser.getusername () + "has been applied, please choose again!" ");

Return representative returns, run the time to hit back will not do the following processing, function equivalent to Response.End in ASP

Return
}
%>
<%
If there is no problem with user name duplication, call the Lyf.adduser Addnewuser () method to add user data to the database and
Display the corresponding information based on whether the data was added successfully

if (Adduser.addnewuser ()) {
%>
< h2> Add User success!

< h2> add user failed, please contact admin!

</body>

listuser.jsp file

Description: User Information list page, used to display all the registered user information, the data is paginated.

For the convenience of everyone, using a common paging code, if it is JDBC2.0 above or other support
The database driver for the type_scroll_insensitive cursor can have a more concise paging method.

And the previous JSP page similar to the statement is not explained, the representative
This page to import Java.sql.ResultSet class library, because the JSP page in the middle to declare resultset;
The Oracle.jdbc.driver.* class Library is an Oracle-specific JDBC driver that allows 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; Total Records
int intpagecount; Total pages
int intpage; Page number to display
Java.lang.String strpage;
int i,j,k;
Set the number of records to display on a page
Intpagesize = 15;
Get the number to display
Strpage = request.getparameter ("page");
if (strpage==null) {//indicates that there is no page this parameter in QueryString, the first page data is now displayed
Intpage = 1;
}
else{//converts a string to an integral type
Intpage = Java.lang.Integer.parseInt (strpage);
if (intpage<1) intpage = 1;
}
Get Total Records
strSQL = "SELECT count (*) from user";
ResultSet result = Db.executequery (strSQL); Execute the SQL statement and get the result set
Result.next (); When the recordset is just opened, the pointer is before the first record
Introwcount = Result.getint (1);
Result.close (); Close result set
Count Total Pages
Intpagecount = (introwcount+intpagesize-1)/intpagesize;
Adjust the number of pages to be displayed
if (intpage>intpagecount) intpage = Intpagecount;
Strsql= "SELECT * from user ORDER by id DESC";
Execute the SQL statement and get the result set
result = Db.executequery (strSQL);
Position the record pointer over the first record of the page you want to display
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>
< 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 Total Page
< a href= "listuser.jsp?page=0" > Home
< a href= "listuser.jsp?page=" > previous page
Previous page
< a href= "listuser.jsp?page=" > next page
Next page
< a href= "listuser.jsp?page=" > Last
< 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" > username

< td >
< p align= "center" >email

< td >
< p align= "Center" > Homepage

< 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>
  
Run the program newuser.jsp file first, do the user registration operation, and then run the listuser.jsp file to see if it has
Added to the database. For specific JSP files and class files in what directory of the problem, please see the specific JSP Server software reference,
One of the easiest ways to do this is to run it directly with Jbuilder4.0 because it comes with Tomcat server software.

Well, here Jsp+javabean's introduction is basically over, through the above study, we should javabean how
Application in the JSP program has a basic understanding, the rest is in the specific procedures to apply and play.




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.