程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> JSP編程 >> 關於JSP >> 基於JavaBean,JSP實現登錄並顯示分頁信息的小系統

基於JavaBean,JSP實現登錄並顯示分頁信息的小系統

編輯:關於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();
    }
   }
  }

 }

}

最終現實的簡單分頁效果如圖

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved