纯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("<html>");
		pw.println("<body>");
		pw.println("<h1>登陆界面</h1>");
		pw.println("<form action=logincl method=post>");
		pw.println("用户名:<input type=text name=username><br>");
		pw.println("密码:<input type=password name=passwd><br>");
		pw.println("<input type=submit value=login><br>");
		pw.println("</form>");
		pw.println("</body>");
		pw.println("</html>");
		
	}
	
	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("<html>");
			pw.println("<body>");
			pw.println("<table border='1'>");
			//显示结果表格
			pw.println("<tr><th>userId</th><th>username</th><th>passwd</th><th>grade</th></tr>");
			while(rs.next()){
				pw.println("<tr>");
				pw.println("<td>" + rs.getInt(1) + "</td>");
				pw.println("<td>" + rs.getString(2) + "</td>");
				pw.println("<td>" + rs.getString(3) + "</td>");
				pw.println("<td>" + rs.getInt(4) + "</td>");
				pw.println("</tr>");
			}
			pw.println("</table>");
			//显示上一页
			if(pageNow != 1)
			pw.println("<a href=wel?pageNow=" + (pageNow-1) + ">"+"上一页"+"</a>");
			//显示5个页面超链接
			for(int i=pageNow; i < pageNow+5; i++){
				pw.println("<a href=wel?pageNow="+i+">" +i+ "</a>");
			}
			//显示下一页
			if(pageNow != pageCount)
			pw.println("<a href=wel?pageNow=" + (pageNow+1) + ">"+"下一页"+"</a>");
			
			pw.println("</body>");
			pw.println("</html>");
			
		} 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配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web_2_4.xsd"
         wersion="2.4">

<servlet>
  <servlet-name>login</servlet-name>
  <servlet-class>com.example.Login</servlet-class>
</servlet>

<servlet-mapping>
  <servlet-name>login</servlet-name>
  <url-pattern>/login</url-pattern>
</servlet-mapping>

<servlet>
  <servlet-name>logincl</servlet-name>
  <servlet-class>com.example.LoginCL</servlet-class>
</servlet>

<servlet-mapping>
  <servlet-name>logincl</servlet-name>
  <url-pattern>/logincl</url-pattern>
</servlet-mapping>

<servlet>
  <servlet-name>wel</servlet-name>
  <servlet-class>com.example.Wel</servlet-class>
</servlet>

<servlet-mapping>
  <servlet-name>wel</servlet-name>
  <url-pattern>/wel</url-pattern>
</servlet-mapping>

</web-app>

二、分页显示

分别定义四个变量

                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登陆页面与数据分页显示)