A small system that implements logon and displays paging information based on JavaBean and JSP

Source: Internet
Author: User

Do not talk nonsense. First of all, set up the database to be connected. My database name is collage, and the data table to be displayed is students. There are six columns. If you want to learn and test it, you can create your own database.
Id: int

Name: varchar

Grade: int

Batch: int

Password: int

Gxqm: varchar

JSP pages, including login. jsp, loginhandle. jsp, and welcome. jsp. There are three java classes. Userbean is used to map data objects, UserbeanCl is used for business logic processing, and connDB is used to establish database connections. Run the Code directly.

Login. jsp page

<% @ Page language = "java" contentType = "text/html; charset = UTF-8"
PageEncoding = "UTF-8" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Title> Login in and then enjoy yourself! </Title>
</Head>
<Body>
<Center>
Please log on
<Form name = "form1" action = "loginhandle. jsp">
<Hr color = "cc00aa">
Username: <input type = "text" name = "user">
<Br>
Password & nbsp; Code: <input type = "password" name = "password">
<Br>
<Input type = "submit" value = "login">
<Input type = "reset" name = "reset">
</Form>
</Center>
</Body>
</Html>

Loginhandle page:

<% @ Page language = "java" import = "java. SQL. * "import =" java. util. *, com. ly. model. userbeanCl "contentType =" text/html; charsets = UTF-8"
PageEncoding = "UTF-8" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Title> here </title>

</Head>
<Body>
<Center>
<Br>
<%
String u = request. getParameter ("user ");
String p = request. getParameter ("password ");

// 1. General Verification
/* If (u. equals ("Liyong") & p. equals ("woaini ")){
Response. sendRedirect ("welcome. jsp? User = "+ u); // jump to the welcome page and pass the user name
}
Else {
Response. sendRedirect ("login. jsp ");
}
*/
// 2. directly go to the database for verification
// DriverManager. registerDriver (new com. mysql. jdbc. Driver ());
// Class. forName ("com. mysql. jdbc. Driver ");
/*
Try {
Class. forName ("com. mysql. jdbc. Driver ");
} Catch (ClassNotFoundException e ){
E. printStackTrace ();
}
Connection ct = DriverManager. getConnection (
"Jdbc: mysql: // localhost: 3306/collage", "root ","");
Statement st = ct. createStatement ();
ResultSet rs = st.exe cuteQuery ("select password from students where name = '"
+ U + "'");
If (rs. next ()){
// Indicates that the user name exists
If (rs. getString (1). equals (p )){
// The password is correct.
Response. sendRedirect ("welcome. jsp? User = "+ u); // jump to the welcome page and pass the user name
} Else {
Response. sendRedirect ("login. jsp ");

}
}
Rs. close ();
St. close ();
Ct. close ();
*/
// 3. Verify the user through userbeanCl
UserbeanCl ubc = new UserbeanCl ();
 
If (ubc. checkUser (u, p ))
{
Response. sendRedirect ("welcome. jsp? User = "+ u );

} Else {

Out. println ("<script> alert ('incorrect password or username! '); </Script> ");
Thread. sleep (1000 );
Response. sendRedirect ("login. jsp ");
// Return;

}

%>
</Center>
</Body>
</Html>

Welcome Page:

<% @ Page language = "java" import = "java. util. *, com. ly. model .*"
ContentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Title> Welcome your mother! </Title>
</Head>
<Body>
<Center>
Hello, world! <Br>
<% = Request. getParameter ("user") %>
<Br> <a href = "login. jsp"> return to log on again </a>
<Hr color = "silver">
<Br> User Information List <br>
<%
Int pageNow = 1;
String u = request. getParameter ("user ");
String s_pageNow = request. getParameter ("pageNow ");
If (s_pageNow! = Null ){
PageNow = Integer. parseInt (s_pageNow );

}
// Call the UserbeanCl method to complete pagination
UserbeanCl ubc = new UserbeanCl ();
Int pageCount = ubc. pageCount ();
ArrayList al = ubc. getUsersByPage (pageNow );

%>
<Table border = "1">
<Tr>
<Td> User ID </td>
<Td> name </td>
<Td> class </td>
<Td> batch </td>
<Td> password </td>
<Td> personalized signature </td>
</Tr>
<%
For (int I = 0; I <al. size (); I ++ ){
Userbean usb = (Userbean) al. get (I );
// System. out. println ("-----------------" + usb. getGxqm ());
%>
<Tr>
<Td> <% = usb. getId () %> </td>
<Td> <% = usb. getName () %> </td>
<Td> <% = usb. getGrade () %> </td>
<Td> <% = usb. getBatch () %> </td>
<Td> <% = usb. getPassword () %> </td>
<Td> <% = usb. getGxqm () %> </td>
</Tr>
<%

}

%>

</Table>
<Br> <%
// Display the previous page
Out. println ("<a href = welcome. jsp? PageNow = "+ 1 +" & user = "+ u +"> homepage </a> ");
If (pageNow! = 1 ){
Out. println ("<a href = welcome. jsp? PageNow = "+ (pageNow-1) +" & user = "+ u +"> previous page </a> ");

}
// Display the page number
For (int I = pageNow; I <= (pageCount <(pageNow + 5 )? PageCount :( pageNow + 5); I ++ ){

Out. println ("<a href = welcome. jsp? PageNow = "+ I +" & user = "+ u +"> ["+ I +"] </a> ");

}
If (pageNow! = PageCount ){
Out. println ("<a href = welcome. jsp? PageNow = "+ (pageNow + 1) +" & user = "+ u +"> next page </a> ");

}
Out. println ("<a href = welcome. jsp? PageNow = "+ pageCount +" & user = "+ u +"> last page </a> ");
%>
 
</Center>
</Body>
</Html>

Userbean. java encapsulate data objects

Package com. ly. model;

Public class Userbean {
Private int id;
Private String name;
Private int grade;
Private int batch;
Private int password;
Private String gxqm;
 
Public int getId (){
Return id;
}
Public void setId (int id ){
This. id = id;
}
Public String getName (){
Return name;
}
Public void setName (String name ){
This. name = name;
}
Public int getGrade (){
Return grade;
}
Public void setGrade (int grade ){
This. grade = grade;
}
Public int getBatch (){
Return batch;
}
Public void setBatch (int batch ){
This. batch = batch;
}
Public int getPassword (){
Return password;
}
Public void setPassword (int password ){
This. password = password;
}
Public String getGxqm (){
Return gxqm;
}
Public void setGxqm (String gxqm ){
This. gxqm = gxqm;
}
 
}


ConnDB. java implements database connection // obtain database connection

// Obtain the database connection
Package com. ly. model;
Import java. SQL .*;
Import java. util .*;
Public class connDB {
Private Connection ct = null;
 
 
Public Connection getConn (){
 
 
Try {
Class. forName ("com. mysql. jdbc. Driver ");
Ct = DriverManager. getConnection ("jdbc: mysql: // localhost: 3306/collage", "root ","");
} Catch (Exception e ){

E. printStackTrace ();
}

 
Return ct;
}
}

UserbeanCl. java implements business logic processing

// This is a processing class that encapsulates various operations on the User table, including addition, deletion, modification, and query.

Package com. ly. model;

Import java. SQL .*;
Import java. util. ArrayList;

Public class UserbeanCl {
Private Statement st = null;
Private ResultSet rs = null;
Private Connection ct = null;
Private int pageSize = 10;
Private int rowCount = 0;
Private int pageCount = 0;

// Verify whether the user exists
Public int pageCount (){
Try {
// Get the connection
Ct = new connDB (). getConn ();
St = ct. createStatement ();
Rs = st.exe cuteQuery ("select count (*) from students ");
//
If (rs. next ()){

RowCount = rs. getInt (1 );
}
If (rowCount % pageSize = 0 ){
PageCount = rowCount/pageSize;

} Else {
PageCount = rowCount/pageSize + 1;
}


} Catch (Exception e ){
E. printStackTrace ();
} Finally {
Shutup ();
}
Return pageCount;

}

Public ArrayList getUsersByPage (int PageNow ){
PageCount = pageCount ();
 
Ct = new connDB (). getConn ();
Int startRow = (PageNow-1) * pageSize;
ArrayList al = new ArrayList ();
 
Try {
St = ct. createStatement ();
Rs1_st.exe cuteQuery ("SELECT id, name, grade, batch, password, gxqm FROM 'Students 'limit" + startRow + "," + pageSize );
While (rs. next ()){

Userbean ub = new Userbean ();
Ub. setId (rs. getInt (1 ));
Ub. setName (rs. getString (2 ));
Ub. setGrade (rs. getInt (3 ));
Ub. setBatch (rs. getInt (4 ));
Ub. setPassword (rs. getInt (5 ));
Ub. setGxqm (rs. getString (6 ));

// System. out. println (rs. getString (6 ));


Al. add (ub );

}

} Catch (Exception e ){
E. printStackTrace ();
} Finally {
Shutup ();
}

Return al;

}

Public boolean checkUser (String u, String p ){
Boolean B = false;
Ct = new connDB (). getConn ();
Try {
St = ct. createStatement ();
ResultSet rs = st
. ExecuteQuery ("select password from students where name = '"
+ U + "'");
If (rs. next ()){
// Indicates that the user name exists
If (rs. getString (1). equals (p )){
// The password is correct.
B = true;
} Else {
Return false;
}
}

} Catch (Exception e ){
E. printStackTrace ();
} Finally {
Shutup ();
}
Return B;

}

Public void shutup (){

Try {
If (rs! = Null)
Rs. close ();
} Catch (SQLException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Finally {
Try {
If (st! = Null)
St. close ();
} Catch (SQLException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Finally {
Try {
If (ct! = Null)
Ct. close ();
} Catch (SQLException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
}

}

}

The simple paging effect of the final reality

 

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.