基于JavaBean,JSP实现登录并显示分页信息的小系统
不说废话,首先,建立要连接的,我的数据库名称是collage,要显示的数据表是students,有六列,要学习并测试的可以自己建立数据库。
id:int
name:varchar
grade:int
batch:int
password:int
gxqm:varchar
jsp页面,有三个,login.,loginhandle.jsp,welcome.jsp。java类有三个,userbean用于映射数据对象,userbeancl用于业务逻辑处理,conndb用于建立数据库连接。直接上代码吧
login.jsp页面
<%@ 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>
请登录
<form name="form1" action="loginhandle.jsp">
<hr color="cc00aa">
用户名: <input type="text" name="user">
<br>
密 码: <input type="password" name="password">
<br>
<input type="submit" value="登录">
<input type="reset" name="重置">
</form>
</center>
</body>
</html>
loginhandle页面:
<%@ page language="java" import="java.sql.*" import="java.util.*,com.ly.model.userbeancl" 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>here</title>
</head>
<body>
<center>
<br>
<%
string u = request.getparameter("user");
string p = request.getparameter("password");
// 一、普通验证
/* if(u.equals("liyong")&&p.equals("woaini")){
response.sendredirect("welcome.jsp?user="+u);//跳转到欢迎界面并传递出用户名user
}
else{
response.sendredirect("login.jsp");
}
*/
// 二、直接到数据库中验证
// 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.executequery("select password from students where name='"
+ u + "'");
if (rs.next()) {
//说明用户名存在
if (rs.getstring(1).equals(p)) {
//密码正确
response.sendredirect("welcome.jsp?user=" + u);//跳转到欢迎界面并传递出用户名user
} else {
response.sendredirect("login.jsp");
}
}
rs.close();
st.close();
ct.close();
*/
// 三、通过userbeancl对用户进行验证
userbeancl ubc=new userbeancl();
if(ubc.checkuser(u,p))
{
response.sendredirect("welcome.jsp?user=" + u);
}else {
out.println("<script>alert('密码或者用户名错误!');</script>");
thread.sleep(1000);
response.sendredirect("login.jsp");
// return;
}
%>
</center>
</body>
</html>
welcome页面:
<%@ 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">返回重新登录</a>
<hr color="silver">
<br> 用户信息列表 <br>
<%
int pagenow=1;
string u=request.getparameter("user");
string s_pagenow=request.getparameter("pagenow");
if(s_pagenow!=null){
pagenow=integer.parseint(s_pagenow);
}
// 调用userbeancl的方法,完成分页显示
userbeancl ubc=new userbeancl();
int pagecount=ubc.pagecount();
arraylist al=ubc.getusersbypage(pagenow);
%>
<table border="1">
<tr>
<td>用户id</td>
<td>姓名</td>
<td>班级</td>
<td>批次</td>
<td>密码</td>
<td>个性签名</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><hr color="blue"><br>
<%
//显示上一页
out.println("<a href=welcome.jsp?pagenow="+1+"&user="+u+">首页</a>");
if(pagenow!=1){
out.println("<a href=welcome.jsp?pagenow="+(pagenow-1)+"&user="+u+">上一页</a>");
}
// 显示页码
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+">下一页</a>");
}
out.println("<a href=welcome.jsp?pagenow="+pagecount+"&user="+u+">尾页</a>");
%>
</center>
</body>
</html>
userbean.java实现数据对象的封装
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实现数据库的连接//得到数据库的连接
//得到数据库的连接
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实现业务逻辑的处理
//这是一个处理类,主要封装对user表的各种操作,包括增删改查
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;
// 验证用户是否存在
public int pagecount() {
try {
// 得到连接
ct = new conndb().getconn();
st=ct.createstatement();
rs = st.executequery("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();
rs=st.executequery("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()) {
// 说明用户名存在
if (rs.getstring(1).equals(p)) {
// 密码正确
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();
}
}
}
}
}
最终现实的简单分页效果如图
上一篇: 让上班族感到温暖的便利店美食 麻辣香锅饭上榜 关东煮第一
下一篇: 不愧是二货,搞笑不吝啬