jialisoftw

基于JavaBean,JSP实现登录并显示分页信息的小系统

不说废话,首先,建立要连接的数据库,我的数据库名称是collage,要显示的数据表是students,有六列,要学习并测试的可以自己建立数据库。
id:int

name:varchar

grade:int

batch:int

password:int

gxqm:varchar

 JSP页面,有三个,login.jsp,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>
密&nbsp;&nbsp;码: <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();
    }
   }
  }

 }

}

最终现实的简单分页效果如图

好了,基本效果就这样,当然怎么改善就随你自己的意愿了!

posted on 2012-10-13 11:54 飞猪一号 阅读(2278) 评论(1)  编辑  收藏

评论

# re: 基于JavaBean,JSP实现登录并显示分页信息的小系统 2015-03-26 17:34 kad

十分感谢啊!太有用了!  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 

导航

<2012年10月>
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

统计

常用链接

留言簿

随笔档案

友情链接

搜索

最新评论

阅读排行榜

评论排行榜