纯Sevlet登陆页面与数据分页显示

一、大致流程如下:

纯Sevlet登陆页面与数据分页显示_第1张图片

分别对应三个sevlet,Login.java LoginCL.java   Wel.java


代码如下:

Login.java

package com.example;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Login extends HttpServlet{

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		resp.setContentType("text/html;charset=utf-8");
		
		PrintWriter pw = resp.getWriter();
		pw.println("");
		pw.println("");
		pw.println("

登陆界面

"); pw.println("
"); pw.println("用户名:
"); pw.println("密码:
"); pw.println("
"); pw.println("
"); pw.println(""); pw.println(""); } public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); } }

LoginCL.java

//用户验证
package com.example;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginCL extends HttpServlet{
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		//接受用户名和密码
		String u = req.getParameter("username");
		String p = req.getParameter("passwd");
		
		//操作数据库
		Connection ct = null;
		Statement stat = null;
		ResultSet rs = null;
		
		try {
			Class.forName("com.mysql.jdbc.Driver");
			ct = DriverManager.getConnection("jdbc:mysql://localhost:3306/yuippe", 
					"root", "123456");
			stat = ct.createStatement();
			rs = stat.executeQuery("SELECT passwd FROM users WHERE username='" + u +
					"'" );
			
			if(rs.next()){
				//说明用户存在
				String dbpasswd = rs.getString(1);
				
				if(p.equals(dbpasswd)){
					//用户信息存入session
					HttpSession hs = req.getSession(true);
					hs.setMaxInactiveInterval(60);
					hs.setAttribute("name", u);
					//跳转
					resp.sendRedirect("wel");
				}else{
					resp.sendRedirect("login");
				}
			}else{
				//不合法
				//跳转
				resp.sendRedirect("login");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try 
			{
				if(rs != null){
					rs.close();
				}
				if(stat != null){
					stat.close();
				}
				if(ct != null){
					ct.close();
				}
			
			} catch (Exception e) {
					e.printStackTrace();
				
			}
		}
	}
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doGet(req, resp);
	}
}

Wel.java

//欢迎界面
package com.example;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class Wel extends HttpServlet{
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		HttpSession hs = req.getSession(true);
		String val = (String)hs.getAttribute("name");
		
		//判断
		if(val == null){
			//非法
			resp.sendRedirect("login");
		}
		
		//分页
		int pageSize = 3;//一页显示几条记录
		int pageNow = 1; //目前显示的页数
		int rowCount = 0;//共有多少条记录(查表)
		int pageCount = 0;//共有多少页(计算)
		
		//动态的接受pageNow
		String spageNow = req.getParameter("pageNow");
		if(spageNow != null)
		pageNow = Integer.parseInt(spageNow);
		
		//操作数据库
		Connection ct = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		try {
			Class.forName("com.mysql.jdbc.Driver");
			ct = DriverManager.getConnection("jdbc:mysql://localhost:3306/yuippe", 
					"root", "123456");
			ps = ct.prepareStatement("SELECT COUNT(*) FROM users");
			rs = ps.executeQuery();
			if(rs.next())
			rowCount = rs.getInt(1);
			
			//共分为多少页
			if(rowCount % pageSize == 0){
				pageCount = rowCount/pageSize;
			}else{
				pageCount = rowCount/pageSize + 1;
			}
			
			ps.close();
			rs.close();
			//每页上需要显示的结果
			ps = ct.prepareStatement("SELECT * FROM users LIMIT " + (pageNow-1)*pageSize + ","
					                 + pageSize +";");
			rs = ps.executeQuery();
			
			resp.setContentType("text/html;charset=utf-8");
			PrintWriter pw = resp.getWriter();
			pw.println("");
			pw.println("");
			pw.println("");
			//显示结果表格
			pw.println("");
			while(rs.next()){
				pw.println("");
				pw.println("");
				pw.println("");
				pw.println("");
				pw.println("");
				pw.println("");
			}
			pw.println("
userIdusernamepasswdgrade
" + rs.getInt(1) + "" + rs.getString(2) + "" + rs.getString(3) + "" + rs.getInt(4) + "
"); //显示上一页 if(pageNow != 1) pw.println(""+"上一页"+""); //显示5个页面超链接 for(int i=pageNow; i < pageNow+5; i++){ pw.println("" +i+ ""); } //显示下一页 if(pageNow != pageCount) pw.println(""+"下一页"+""); pw.println(""); pw.println(""); } catch (Exception e) { e.printStackTrace(); }finally{ try { if(rs != null){ rs.close(); } if(ps != null){ ps.close(); } if(ct != null){ ct.close(); } } catch (Exception e) { e.printStackTrace(); } } } public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); } }

web.xml配置文件如下:





  login
  com.example.Login



  login
  /login



  logincl
  com.example.LoginCL



  logincl
  /logincl



  wel
  com.example.Wel



  wel
  /wel



二、分页显示

分别定义四个变量

                int pageSize = 3;//一页显示几条记录
int pageNow = 1; //目前所在的页面
int rowCount = 0;//共有多少条记录(查表)
int pageCount = 0;//共有多少页(计算)

rowCount通过查表得到:

ps = ct.prepareStatement("SELECT COUNT(*) FROM users");
			rs = ps.executeQuery();
			if(rs.next())
			rowCount = rs.getInt(1);

pageCount通过计算可得:

if(rowCount % pageSize == 0){
				pageCount = rowCount/pageSize;
			}else{
				pageCount = rowCount/pageSize + 1;
			}

三、结果如下:

纯Sevlet登陆页面与数据分页显示_第2张图片

你可能感兴趣的:(java)