相关代码页面的存储位置:(相关数据库为drimgs)
===================================================
多条件模糊分页查询的跳转过程及实现步骤的相关代码如下解析:
MainController.java的相关代码实现如下:
package cn.controller;
import java.io.IOException;
import java.lang.reflect.Method;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import cn.pojo.Student;
import cn.pojo.User;
import cn.services.CenterServices;
import cn.services.CoachServices;
import cn.services.GradeServices;
import cn.services.StudentServices;
import cn.services.StudentServicesImp;
import cn.services.UserServicesImp;
import cn.utils.PackObject;
import cn.utils.Page;
@WebServlet("*.html")
public class MainController extends HttpServlet {
private static final long serialVersionUID = 1L;
private StudentServices studentServices = new StudentServicesImp();
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 设置post和get请求执行的一致
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 1:设置字符集
request.setCharacterEncoding("utf-8");
// 2:设置url请求路径
String url = request.getServletPath();
System.out.println("url=" + url);
// 3:分解url字符串
String path = url.substring(1, url.indexOf("."));
System.out.println("path=" + path);
try {
// 4:获得MainController类
Class cl = Class.forName("cn.controller.MainController");
// 5:获得MainController中的指定方法
Method md = cl.getDeclaredMethod(path, HttpServletRequest.class, HttpServletResponse.class);
// 6:调用方法
md.invoke(this, request, response);
} catch (Exception e) {
// 7: 出现异常说明path方法有问题或不存在path方法
e.printStackTrace();
}
}
public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
User u = PackObject.getObject(request, User.class);
User user = new UserServicesImp().LoginUser(u.getUserName(), u.getUserPassWord());
System.out.println(user);
HttpSession session = request.getSession();
if (user != null) {
session.setAttribute("USER_LOGIN", user);
Cookie ck1 = new Cookie("userName", user.getUserName());
Cookie ck2 = new Cookie("passWord", user.getUserPassWord());
ck1.setMaxAge(60 * 60 * 24);
ck2.setMaxAge(60 * 60 * 24);
response.addCookie(ck1);
response.addCookie(ck2);
System.out.println(u);
request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request, response);
} else {
session.setAttribute("ERROR_MSG", "错误的用户名或密码!");
response.sendRedirect("login.jsp");
}
}
public void register(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
User user = PackObject.getObject(request, User.class);
int i = new UserServicesImp().addUser(user);
response.sendRedirect("login.jsp");
}
// 获得所有学生
public void studentlimit(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String str = request.getParameter("pageNum");
Integer pageNum = null;
if (str == null || str.equals("")) {
pageNum = 1;
} else {
pageNum = Integer.parseInt(str);
}
String studentName = request.getParameter("studentName");
String coachName = request.getParameter("coachName");
String gradeName = request.getParameter("gradeName");
Page<Student> page = studentServices.getStudentLike(studentName, gradeName, coachName, pageNum);
for (Student s : page.getList()) {
System.out.println(s);
}
request.setAttribute("page_st", page);
request.getRequestDispatcher("/WEB-INF/jsp/studentlimit.jsp").forward(request, response);
}
//学生表的添加
public void toAddStudent(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
if (session.getAttribute("USER_LOGIN") != null) {
request.setAttribute("Center_List", CenterServices.getAllCenter());
request.setAttribute("Coach_List", CoachServices.getAllCoach());
request.setAttribute("Grade_List", GradeServices.getAllGrade());
request.getRequestDispatcher("/WEB-INF/jsp/addStudent.jsp").forward(request, response);
} else {
response.sendRedirect("login.jsp");
}
}
// 5、执行添加操作
public void doStudentAdd(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
Student st = PackObject.getObject(request, Student.class);
System.out.println(st);
int i = studentServices.addStudent(st);
if (i > 0) {
response.sendRedirect("studentList.html");
} else {
request.getRequestDispatcher("/WEB-INF/jsp/addStudent.jsp").forward(request, response);
}
}
}
==============================================================
相关longin.jsp(登录界面)的相关代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!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">
<style>
* {
margin: 0px;
padding: 0px;
}
#box {
border: 1px solid black;
border-radius: 10px;
margin: 100px auto;
width: 30%;
padding: 30px;
background: gray;
}
#userName, #userPassWord, #info {
width: 250px;
height: 30px;
}
#sub, #but {
width: 100px;
height: 35px;
line-height: 35px;
}
#login, #rem {
width: 20px;
height: 20px;
vertical-align: middle;
}
#login {
margin-left: 10px;
}
#but, #rem {
margin-left: 49px;
}
</style>
<script src="js/jquery-1.8.3.js"></script>
<script>
//注册跳转
$(function() {
$("#but").click(function() {
window.location = "register.jsp";
})
});
//是否记住密码
$(function() {
var ck = document.cookie;//拿到cookie=userName=fy11020; passWord=123456;
//alert(ck);
var userName;//用来保存用户名
var passWord;//用来保存密码
var check;//是否选中
var login;//是否自动登录
//首位去空格
ck = ck.trim();
//使用;将字符串分割成数组
var cks = ck.split(";");
//遍历数组
for (var i = 0; i < cks.length; i++) {
var item = cks[i];
//对元素去空格
item = item.trim();
//拿到=号的下标
var index = item.indexOf("=");
//拿到=号前半部分内容
var name = item.substring(0, index);
//拿到=号后半部分内容
var value = item.substring(index + 1);
//是否记住密码
if (name == "check" && (value == "on"||value=="true")) {
$("#rem").attr("checked", true)
//check = value;
for (var i = 0; i < cks.length; i++) {
var item1 = cks[i];
//对元素去空格
item1 = item1.trim();
//拿到=号的下标
var index1 = item1.indexOf("=");
//拿到=号前半部分内容
var name1 = item1.substring(0, index1);
//拿到=号后半部分内容
var value1 = item1.substring(index1 + 1);
if (name1 == "userName") {
$("#userName").val(value1);
userName = value1;
}
if (name1 == "passWord") {
$("#userPassWord").val(value1);
passWord = value1;
}
//自动登录
if (name1 == "login" && (value1 == "on"||value1=="true")) {
$("#login").attr("checked", true)
//login = value1;
setTimeout(dologin, 1000);
}
}
}
}
function dologin() {
if(($("#login").is(":checked"))&&($("#rem").is(":checked"))){
location = "LoginServlet?userName=" + $("#userName").val()
+ "&userPassWord=" + $("#userPassWord").val() + "&login=on"+
"&rem=on";
}
}
});
</script>
<title>登录</title>
</head>
<body>
<div id="box">
<form action="login.html" method="post">
<table cellpadding="5px" cellspacing="20px">
<tr>
<td>用户名:</td>
<td><input type="text" name="userName" id="userName" /></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="userPassWord" id="userPassWord" /></td>
</tr>
<tr>
<td></td>
<td><input type="checkbox" name="login" id="login" />自动登录 <input
type="checkbox" name="rem" id="rem" />记住密码</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="登录" id="sub" /> <input
type="button" value="注册" id="but" /></td>
</tr>
<tr>
<td></td>
<td>
<%
String msg = (String) session.getAttribute("ERROR_MSG");
if (msg != null) {
%> <input type="text" value="${msg}" id="info" /> <%
session.removeAttribute("ERROR_MSG");
}
%>
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
=============================================================
相关register.jsp(注册页面)实现代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style>
* {
margin: 0px;
padding: 0px;
}
#box {
border: 1px solid black;
margin: 50px auto;
width: 30%;
padding: 20px;
}
input {
margin: 10px;
}
#code, #checkCode {
width: 55px;
}
</style>
<script src="laydate/laydate.js"></script>
<script src="js/jquery-1.8.3.js"></script>
<script src="js/register.js"></script>
</head>
<body>
<div id="box">
<form class="myForm" action="register.html" method="post" οnsubmit="return checkForm()">
<h2>欢迎注册</h2>
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="userName" placeholder="输入用户名"
id="userName" /> <span></span></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="userPassWord"
placeholder="输入密码" id="password" /> <span></span></td>
</tr>
<tr>
<td></td>
<td><input type="password" name="password2"
placeholder="再次输入密码" id="password2" /> <span></span></td>
</tr>
<tr>
<td>电话:</td>
<td><input type="text" name="phone" placeholder="输入手机号"
id="phone" /> <span></span></td>
</tr>
<tr>
<td>地址:</td>
<td><input type="text" name="address" id="address" /></td>
</tr>
<tr>
<td>生日:</td>
<td><input type="text" name="bornDate" id="bornDate" /></td>
</tr>
<tr>
<td>验证码:</td>
<td><input type="text" name="code" id="code" readonly/> <input
type="text" name="checkCode" id="checkCode" />
<button id="btn">看不清</button> <span></span></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="提交" /> <input type="reset"
value="重置" id="reset" /></td>
</tr>
</table>
</form>
</div>
</body>
</html>
======================================================================
src/WEB-INF/jsp下的相关代码实现:
main.jsp:
<%@ page import="cn.pojo.User" %>
<%@ page import="cn.utils.Page" %>
<%@ page import="cn.services.UserServicesImp" %>
<%@ page import="java.util.List" %><%--
Created by IntelliJ IDEA.
User: 31568
Date: 2018/3/30 0030
Time: PM 11:12
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<base href="<%=basePath%>">
<title>用户名</title>
<script src="js/jquery-1.8.3.js"></script>
<script src="layui/layui.js"></script>
<script src="js/click.js"></script>
<script src="js/main.js"></script>
<link rel="stylesheet" href="layui/css/layui.css">
<link rel="stylesheet" href="css/main.css">
</head>
<body>
<div class="layui-header">
<!-- 头部区域(可配合layui已有的水平导航) -->
<ul class="layui-nav layui-layout-right">
<p id="logo">驾校管理·主界面</p>
<li class="layui-nav-item">
<a href="javascript:;" id="name">
<img src="" class="layui-nav-img">
<%User u=(User)session.getAttribute("USER_LOGIN");
out.write(u.getUserName());
%>
</a>
<dl class="layui-nav-child">
<%
User user=(User) session.getAttribute("USER_LOGIN");
%>
<dd><a href="toUserUpdate.jsp?userId=<%=u.getUserId()%>">修改资料</a></dd>
</dl>
</li>
<li class="layui-nav-item"><a href="login.jsp">退出登录</a></li>
</ul>
</div>
<div class="layui-collapse" lay-accordion id="daohang">
<div class="layui-colla-item">
<p class="layui-colla-title">学校管理</p>
<div class="layui-colla-content layui-show" id="showCenter">显示所有</div>
<div class="layui-colla-content layui-show" id="addCenter">添加学校</div>
</div>
<div class="layui-colla-item">
<p class="layui-colla-title">班级管理</p>
<div class="layui-colla-content" id="showGrade">显示所有</div>
<div class="layui-colla-content" id="addGrade">添加班级</div>
</div>
<div class="layui-colla-item">
<p class="layui-colla-title">教练管理</p>
<div class="layui-colla-content" id="showCoach">教练展示</div>
<div class="layui-colla-content" id="addCoach">添加教练</div>
</div>
<div class="layui-colla-item">
<p class="layui-colla-title">学生管理</p>
<div class="layui-colla-content" ><a href="studentlimit.html"> 学生展示</a></div>
<!-- <div class="layui-colla-content" id="addStudent">学生添加</div> -->
</div>
<div class="layui-colla-item">
<p class="layui-colla-title">驾照类型</p>
<div class="layui-colla-content" id="showLiType">类型展示</div>
<div class="layui-colla-content" id="addLiType">添加类型</div>
</div>
<div class="layui-colla-item">
<p class="layui-colla-title">科目管理</p>
<div class="layui-colla-content" id="showSubject">显示科目</div>
<div class="layui-colla-content" id="addSubject">添加科目</div>
</div>
<div class="layui-colla-item">
<p class="layui-colla-title">模拟考试</p>
<div class="layui-colla-content">科目一</div>
<div class="layui-colla-content">科目四</div>
</div>
</div>
<script>
//注意:折叠面板 依赖 element 模块,否则无法进行功能性操作
layui.use('element', function(){
var element = layui.element;
//…
});
</script>
<div id="lastDiv">
<iframe src="http://www.baidu.com" width="100%" height="100%"
scrolling="yes" frameborder="0" id="show"> </iframe>
</div>
</body>
</html>
---------------
reg.jsp:
<%@ page import="cn.services.UserServices" %>
<%@ page import="cn.services.UserServicesImp" %>
<%@ page import="cn.pojo.User" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
if (path.equals("/")) {
path = "";
}
%>
<!DOCTYPE html>
<html>
<head lang="en">
<base href="<%=basePath%>">
<meta charset="UTF-8">
<title>注册</title>
<link rel="stylesheet" href=css/reg.css>
<script src="js/jquery-1.8.3.js"></script>
<script src="js/laydate/laydate.js"></script>
<script src="js/reg1.js"></script>
<script src="js/demo.js"></script>
</head>
<body>
<div class="wrap">
<div class="container">
<h1>学生注册</h1>
<form action="doReg.jsp" method="post">
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td><span class="sp">*</span>真实姓名:
</td>
<td><input type="text" placeholder="请姓名" name="studentName" id="userName"/>
<span></span></td>
</tr>
<tr>
<td><span class="sp">*</span>密码:
</td>
<td><input type="password" placeholder="请输入密码" name="studentPassWord" id="pass"/>
<span></span></td>
</tr>
<tr>
<td><span class="sp">*</span>确认密码:
</td>
<td><input type="password" placeholder="请再次输入密码" name="passWord_again" id="passAgain"/>
<span></span></td>
</tr>
<%--<tr>
<td><span class="sp">*</span>真实姓名:
</td>
<td><input type="text" placeholder="输入姓名" name="bornDate" id="name">
<span></span></td>
</tr>--%>
<tr>
<td>性别:
</td>
<td id="sex"><input type="radio" name="gender" value="男"/>
<span>男</span>
<input type="radio" name="gender" value="女" checked/>
<span>女</span></td>
</tr>
<input type="hidden" value=""/>
<tr>
<td> 家庭住址:
</td>
<td><input type="text" placeholder="请输入家庭住址" name="address" />
<span></span></td>
</tr>
<tr>
<td><span class="sp">*</span>电话:
</td>
<td><input type="text" placeholder="请输入电话号码" name="phone" id="tel"/>
<span></span></td>
</tr>
<tr>
<td><span class="sp">*</span>身份证号:
</td>
<td><input type="text" placeholder="请输入身份证号" name="identityCard" id="identityCard"/>
<span></span></td>
</tr>
<tr>
<td>头像:
</td>
<td><input type="file" name="studentPicture" id="pic"/>
<span></span></td>
</tr>
<tr>
<td><span class="sp">*</span>验证码:
</td>
<td><input type="text" id="vd" readonly/>
<input type="text" name="vd1" id="vd1" placeholder="输入验证码"/>
<p id="changCode">看不清</p>
<span id="prompt"></span>
</td>
</tr>
</table>
<input type="submit" value="注册" id="sub"/>
<input type="reset" value="重置">
</form>
</div>
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</div>
</body>
</html>
-------------
studentlimit.jsp:(重点实现步骤)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style>
table {
width: 90%;
margin: 10px auto;
table-layout: fixedo; /* 必须添加后表格才起作用 */
}
th:nth-of-type(9) {
width: 15%;
}
th:nth-of-type(8) {
width: 15%;
}
th:nth-of-type(7) {
width: 12%;
}
th:nth-of-type(3) {
width: 15%;
}
th:nth-of-type(5) {
width: 12%;
}
td {
text-align: center;
white-space: nowrap; /* 不准换行 */
overflow: hidden; /* 溢出隐藏 */
text-overflow: ellipsis; /* 使用...代替溢出部分 */
}
#lt {
float: left;
}
#ri {
float: right;
}
#pageForm {
display: inline-block;
}
.clearFix:after {
content: "";
display: block;
clear: both;
}
.box {
width: 90%;
margin: 0 auto;
}
#pageInput {
width: 45px;
text-align: center;
}
#searchForm {
width: 90%;
margin: 10px auto;
text-align: right;
}
</style>
</head>
<body>
<form id="searchForm" method="post" action="studentlimit.html">
<input type="text" name="studentName" value="" placeholder="输入学生名查询" />
<input type="text" name="coachName" value="" placeholder="输入教练名查询" />
<input type="text" name="gradeName" value="" placeholder="输入班级名查询" />
<input type="submit" value="查询" />
</form>
<table border="1" cellspacing="0" cellpadding="0">
<tr>
<th>学号</th>
<th>姓名</th>
<th>密码</th>
<th>性别</th>
<th>地址</th>
<th>身份证号</th>
<th>中心编号</th>
<th>班级编号</th>
<th>教练编号</th>
<th colspan="3">操作</th>
</tr>
<c:forEach var="student" items="${page_st.list}" varStatus="index">
<tr>
<td>${index.count}</td>
<td>${student.studentName}</td>
<td>${student.studentPassWord}</td>
<td>${student.gender}</td>
<td>${student.address}</td>
<td>${student.identityCard}</td>
<td>${student.centerId}</td>
<td>${student.gradeId}</td>
<td>${student.coachId}</td>
<td><a href="showInfo.html">查看</a></td>
<td><a href="deleteStudent.html">删除</a></td>
<td><a href="updateStudent.html">修改</a></td>
</tr>
</c:forEach>
</table>
<div class="box clearFix">
<div id="lt">
<span> 共有${page_st.totalCount } 条记录,当前第${page_st.pageNum }/${page_st.totalPage }页
</span>
</div>
<div id="ri">
<a href="studentlimit.html?pageNum=1">首页</a> <a
href="studentlimit.html?pageNum=${page_st.pageNum -1}">上一页</a>
<a
href="studentlimit.html?pageNum=${page_st.pageNum +1}">下一页</a>
<a
href="studentlimit.html?pageNum=${page_st.totalCount }">尾页</a>
<form action="studentlimit.html" method="post" id="pageForm">
<span>转到第 </span> <input type="number" name="pageNum" id="pageInput"
value="${page_st.pageNum}>" min="0" max="${page_st.totalPage }"/>
<%-- <input type="hidden" name="studentName" value="<%=studentName%>" />
<input type="hidden" name="coachName" value="<%=coachName%>" />
<input type="hidden" name="gradeName" value="<%=gradeName%>" /> --%>
<span>页</span>
<input type="submit" value="跳转" id="sub" />
</form>
</div>
</div>
</body>
</html>
=====================
web.xml手动配置servlet相关代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>Demo_0416</display-name>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>cn.controller.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>RegisterServlet</servlet-name>
<servlet-class>cn.controller.RegisterServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RegisterServlet</servlet-name>
<url-pattern>/RegisterServlet</url-pattern>
</servlet-mapping>
</web-app>