JSP学生信息管理+简单分页学习记录

登录注册

 

登录

login.jsp

JSP学生信息管理+简单分页学习记录_第1张图片

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    登录界面


账号
密码

注册

注册

注册成功后跳回login

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    注册界面


请完成注册
账号
密码

register的servlet

package servlet;

import Dao.StudentDao;
import entity.Student;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet(name = "RegisterServlet", value = "/RegisterServlet")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        StudentDao studentDao=new StudentDao();
        try {
            studentDao.RegisterStudent(username,password);
            response.sendRedirect("login.jsp");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}

login的servlet

登录成功后转到TableServlet

package servlet;

import Dao.StudentDao;
import entity.Student;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        Student stu=new Student();
        stu.setUsername(username);
        stu.setPassword(password);
        //调用登录功能
        StudentDao studentDao = new StudentDao();
        try {
            Boolean result = studentDao.login(stu);
            if(result==true){
                response.getWriter().print("login success");
                request.getRequestDispatcher("TableServlet").forward(request,response);
            }else {
                response.getWriter().print("login fail");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}

查询结果页面

TableServlet

先通过数据库操作查询所有信息,再调用request域向前端把list传过去;也可以用session,但是浪费资源。

传过去的的是键值的形式。

package servlet;

import Dao.StudentDao;
import entity.Student;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "TableServlet", value = "/TableServlet")
public class TableServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            StudentDao studentDao=new StudentDao();
            response.setContentType("text/html;charset=utf-8");
            request.setCharacterEncoding("utf-8");
       /*        //一页显示3条数据
               int pageSize=3;
               //如果第一次访问servlet,页面在首页,值为1;否则传啥是啥
        String cPage = request.getParameter("begin");
        if(cPage==null){
              cPage="1";
        }
        int currentPage= Integer.parseInt(cPage);
*/
        //查找数据库列表信息,跳转向jsp
        try {
            List list = studentDao.SelectAll();
            //向前端传值
            request.setAttribute("list",list);
            //跳转
            request.getRequestDispatcher("StudentTable.jsp").forward(request,response);
            System.out.println(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}

JSP学生信息管理+简单分页学习记录_第2张图片

StudentTable.jsp

注意jsp的格式,使用herf传递值的方法,

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    StudentTable


<%--通过使用循环来输出学生信息--%> <% response.setContentType("text/html;charset=utf-8"); request.setCharacterEncoding("utf-8"); List list = (List) request.getAttribute("list"); for(int i=0;i <% }; %> add
idusernamepasswordweightheightgrade
<%=list.get(i).getId()%> <%=list.get(i).getUsername()%> <%=list.get(i).getPassword()%> <%=list.get(i).getWeight()%> <%=list.get(i).getHeight()%> <%=list.get(i).getGrade()%> delete update
index last next end

 删除学生记录

点击delete会被超连接到DeleteServlet

package servlet;

import Dao.StudentDao;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/DeleteServlet")
public class DeleteServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          request.setCharacterEncoding("utf-8");
        String id = request.getParameter("id");
        StudentDao studentDao=new StudentDao();
        try {
            studentDao.DeleteStudentById(id);
            //先挑servlet再执行一编查找,再通过servlet中的跳转到jsp页面,完成对jsp页面的刷新
            request.getRequestDispatcher("TableServlet").forward(request,response);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}

需要注意的是,在Servlet中成数据库的删除操作后,需要再执行一次查找页面,再被动跳转到jsp;否则会异常!其他操作的逻辑也如此。

分页

JSP学生信息管理+简单分页学习记录_第3张图片

 JSP学生信息管理+简单分页学习记录_第4张图片

 分页查询方法,返回一个指定大小的list

  /**
     * 分页查询
     */
  public List SelectByPage(int begin, int size) throws SQLException {
      //1.连接数据库
      DBUtil db=new DBUtil();
      Connection connection= db.getConnection();
      //2.编写sql,设置value
      String sql="select * from stu2 limit ?,?";
      PreparedStatement pstmt = connection.prepareStatement(sql);
      pstmt.setInt(1,begin);
      pstmt.setInt(2,size);
      //3.执行sql
      ResultSet rs = pstmt.executeQuery();
      Student student;
      List list=new ArrayList<>();
      while (rs.next()){
          String id2 = rs.getString("id");
          String username2 = rs.getString("username");
          String password2 = rs.getString("password");
          double height2 = rs.getDouble("height");
          double weight2 = rs.getDouble("weight");
          double grade2 = rs.getDouble("grade");
          //封装
          student=new Student();
          student.setId(id2);
          student.setUsername(username2);
          student.setPassword(password2);
          student.setHeight(height2);
          student.setGrade(grade2);
          student.setWeight(weight2);
          list.add(student);
      }

    return list;
  }

查询数据库数据总数方法,返回一个int

/**
     * 查询数据总数
     */
  public int getTotalCount() throws SQLException {
      //1.连接数据库
      DBUtil db=new DBUtil();
      Connection connection= db.getConnection();
      //2.编写sql,设置value
      String sql="select count(*) from stu2";
      PreparedStatement pstmt = connection.prepareStatement(sql);
      //3.执行sql
      ResultSet rs = pstmt.executeQuery();
      int num=-1;
    while (rs.next()){
        num = rs.getInt(1);
    }
      return num;

  }

JSP学生信息管理+简单分页学习记录_第5张图片

对四个按钮进行设置

 

index
">last
">next
">end

TableServlet

package servlet;

import Dao.StudentDao;
import entity.Student;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet(name = "TableServlet", value = "/TableServlet")
public class TableServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            StudentDao studentDao=new StudentDao();
            response.setContentType("text/html;charset=utf-8");
            request.setCharacterEncoding("utf-8");

        //设置为一页显示三条数据
        int size=3;
        //如果第一次访问servlet,页面在首页,值为0;否则传啥是啥
        String cPage = request.getParameter("begin");
        if(cPage==null){cPage="0";}
        int currentPage= Integer.parseInt(cPage);
        //数据总数
        try {
            int totalAccount=studentDao.getTotalCount();
            //页面总数
            int totalPage=totalAccount%size+1;
            int endPage=totalAccount-1;
            //next
            int next=currentPage+size;
            //last
            int last=currentPage-size;
            //数据发送去前端
            request.setAttribute("currentPage",currentPage);
            request.setAttribute("size",size);
            request.setAttribute("totalPage",totalPage);
            request.setAttribute("endPage",endPage);
            request.setAttribute("next",next);
            request.setAttribute("last",last);

        } catch (SQLException e) {
            e.printStackTrace();
        }


        try {
            List list = studentDao.SelectByPage(currentPage, size);
            //向前端传值
            request.setAttribute("list",list);
            //跳转
            request.getRequestDispatcher("StudentTable.jsp").forward(request,response);
            System.out.println(list);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        //查找数据库列表信息,跳转向jsp
        /* try {
            List list = studentDao.SelectAll();
            //向前端传值
            request.setAttribute("list",list);
            //跳转
            request.getRequestDispatcher("StudentTable.jsp").forward(request,response);
            System.out.println(list);
        } catch (Exception e) {
            e.printStackTrace();
        }*/
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}

你可能感兴趣的:(java,servlet,html)