login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
</head>
<body bgcolor = "pink">
<!-- 居中对齐 -->
<center>
用户登录<br>
<hr>
<%
String flag = request.getParameter("errNo");
try{
if(flag.equals("1")){
out.println("密码错误");
}
if(flag.equals("2")){
out.println("用户名不存在");
}
}catch(Exception e){
e.printStackTrace();
}
%>
<form action = "loginCl.jsp" method="post">
用户名:<input type="text" name="username"><br>
密 码:<input type="password" name ="passwd"><br>
<input type="submit" value="提交">
<input type="reset" value="重置">
</form>
</center>
</body>
</html>
loginCl.jsp
<%@ page language="java" import="java.util.*,java.sql.* , com.yyy.model.*" pageEncoding="gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'loginCl.jsp' starting page</title>
</head>
<body>
<%
//接收用户名和密码
String u = request.getParameter("username");
String p = request.getParameter("passwd");
//调用userbean处理的方法
UserBeanCl ubc = new UserBeanCl();
if(ubc.checkUser(u, p)){
response.sendRedirect("wel.jsp?user="+u);
}else{
response.sendRedirect("login.jsp");
}
%>
</body>
</html>
wel.jsp
<%@ page language="java" import="java.util.*, java.sql.*, com.yyy.model.*" pageEncoding="gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'wel.jsp' starting page</title>
</head>
<body>
登陆成功!<%= request.getParameter("user") %><br>
<a href="login.jsp">重新登陆</a>
<h1>用户列表</h1>
<%
//定义分页用到的4个变量
int pageNow = 1; //默认第一页
int pageCount = new UserBeanCl().getPageCount();
//获取页面传来的pageNow信息
String pp = request.getParameter("pageTo");
if(pp != null){
try{
pageNow = Integer.parseInt(pp);
}catch(Exception e){
out.println("pagenow=" + pageNow);
e.printStackTrace();
}
}
//查询所有的记录数
%>
<table border = "1">
<tr><td>用户ID</td><td>名字</td><td>密码</td><td>Email</td><td>级别</td></tr>
<%
UserBeanCl ubc = new UserBeanCl();
UserBean ub = new UserBean();
ArrayList al = new ArrayList();
al = ubc.getUsersByPage(pageNow);
for(int i=0; i<al.size(); i++){
ub =(UserBean) al.get(i);
%>
<!-- 注意:这里使用的是字符串的形式得到值 rs.getInt("userid") rs.getInt(1) 会出异常!!待答复-->
<tr><td><%=ub.getUserId()%></td><td><%=ub.getUsername() %></td><td><%=ub.getPasswd() %></td><td><%=ub.getEmail() %></td><td><%=ub.getGrade() %></td></tr>
<%
}
%>
</table>
<%
//显示超链接
//首页
out.println("<a href=wel.jsp?pageTo="+1+">首页</a>");
//上一页
if(pageNow != 1){
out.println("<a href=wel.jsp?pageTo="+(pageNow - 1)+">上一页</a>");
}
// 判断页数的
if(pageNow == 1){
for(int i = pageNow; i <= pageNow + 5; i++){
out.println("<a href= wel.jsp?pageTo="+i+"> ["+i+"] </a>");
}
}else if(pageNow == 2){
for(int i = pageNow-1; i < pageNow + 5; i++){
out.println("<a href= wel.jsp?pageTo="+i+"> ["+i+"] </a>");
}
}else if(pageNow == pageCount){
for(int i = pageCount-4; i <= pageCount; i++){
out.println("<a href= wel.jsp?pageTo="+i+"> ["+i+"] </a>");
}
}else if(pageNow == pageCount-1){
for(int i = pageCount-3; i <= pageCount+1; i++){
out.println("<a href= wel.jsp?pageTo="+i+"> ["+i+"] </a>");
}
}else{
for(int i=pageNow - 2; i <= pageNow + 2; i++){
out.println("<a href= wel.jsp?pageTo="+i+"> ["+i+"] </a>");
}
}
//显示写一页
if(pageNow != pageCount){
out.println("<a href=wel.jsp?pageTo="+(pageNow + 1)+">下一页</a>");
}
//末页
out.println("<a href=wel.jsp?pageTo="+pageCount+">末页</a>");
%>
</body>
</html>
下面是java片段
UserBean.java
package com.yyy.model;
//userbean
public class UserBean {
private int userId;
private String username;
private String passwd;
private String email;
private int grade;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPasswd() {
return passwd;
}
public void setPasswd(String passwd) {
this.passwd = passwd;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
}
UserBeanCl.java
//也叫做bo 主要是封装对users表的各种操作
package com.yyy.model;
import java.sql.*;
import java.util.ArrayList;
public class UserBeanCl {
private Statement smt = null;
private ResultSet rs = null;
private Connection conn = null;
private int rowCount = 0;//所有记录数
private int pageCount=0;// rowCount/pageSize
private int pageSize = 3; //每页显示3个
//返回总页数
public int getPageCount(){
try{
//连接数据库
conn = new ConnDB().getConn();
smt = conn.createStatement();
rs = smt.executeQuery(" select count(*) from yyytestusers");
if(rs.next()){
// rowCount = Integer.parseInt(rs.getString(1));
rowCount = rs.getInt(1);
}
}catch(Exception e){
e.printStackTrace();
}finally{
this.close();
}
if(rowCount%pageSize == 0){
pageCount = rowCount / pageSize;
}else{
pageCount = rowCount / pageSize + 1;
}
return pageCount;
}
//对用户的信息分页
public ArrayList getUsersByPage(int pageNow){
ArrayList al = new ArrayList();
try{
//创建Statement
conn = new ConnDB().getConn();
smt = conn.createStatement();
//查询
// StringBuffer sb = "select passwd from yyytestusers where username = ';
// rs = smt.executeQuery("select count(*) from yyytestusers ");
String select_by_pageNow = "select * from (select rownum rn, a.* from yyytestusers a) where rn < "+ (pageNow*pageSize+1)
+"and rn not in (select rn from (select rownum rn, a.* from yyytestusers a) where rn < "+((pageNow-1)*pageSize+1) +
") order by userid";
rs = smt.executeQuery(select_by_pageNow);
//一定要rs.next() rs才指向要取得值的地方
while(rs.next()){
UserBean ub = new UserBean();
ub.setUserId(rs.getInt(2));
ub.setUsername(rs.getString(3));
ub.setPasswd(rs.getString(4));
ub.setEmail(rs.getString(5));
ub.setGrade(rs.getInt(6));//这个也是可以的。但是在数据库中。他是第二个整数。所以取2
//ub.setGrade(rs.getInt("grade"));
al.add(ub);
}
}catch(Exception e){
e.printStackTrace();
}finally{
this.close();
}
return al;
}
//验证用户是否合法
public boolean checkUser(String u, String p){
boolean b = false;
//创建statement
try{
conn = new ConnDB().getConn();
smt = conn.createStatement();
rs = smt.executeQuery("select passwd from yyytestusers where username = '"+ u +"'");
//
if(rs.next()){
//c存在用户名
//判断密码
if(rs.getString(1).equals(p)){
//response.sendRedirect("wel.jsp?user="+u);
b = true;
}
}
}catch(Exception e){
e.printStackTrace();
}finally{
this.close();
}
return b;
}
//关闭资源
public void close(){
//关闭数据库资源
try{
if(rs != null)
rs.close();
rs = null;
if(smt != null)
smt.close();
smt = null;
if(conn != null)
conn.close();
conn = null;
}catch(Exception e){
e.printStackTrace();
}
}
}
连接数据库ConnDB.java
//与数据库连接
package com.yyy.model;
import java.sql.*;
public class ConnDB {
private Connection conn = null;
public Connection getConn(){
try{
//在数据库中验证用户
String className="oracle.jdbc.driver.OracleDriver";
// String url = "jdbc:oracle:thin:@192.168.1.3:1521:unionuat";
String url = "jdbc:oracle:thin:@192.168.1.3:1521:unionuat";
String userName = "sales_jacdcs";
String userPass ="sales_jacdcs";
//加载驱动
Class.forName(className);
//得到连接
//注意这里饭的错误:url userName uerPass已经是字符串的格式。下面的语句不需要加""双引号。害了我10分钟
conn = DriverManager.getConnection(url,userName,userPass);
}catch(Exception e){
e.printStackTrace();
}
return conn;
}
}
数据库表users
创建数据表语句