文章目录
小练习
问题描述:
**1.**使用JSP完成登录程序
编写一个登录页面,页面中要求输入用户名和密码,点击登录按钮之后,对用户名的格式以及密码的格式进行验证,用户名的格式是手机号或者是邮箱,密码的格式是由英文字母或数字组成的4-16位密码。如果格式正确,则跳转到验证页面,在验证页面中对输入的用户名和密码,连接数据库进行验证,如果用户名和密码正确则跳转到欢迎页,在欢迎页中显示:欢迎:username,如果用户名密码不正确,则跳转会登录页面(最好给出提示).如果用户名或密码格式不正确,则给出相应的提示,并且不能完成页面的跳转。
**2.**在第一个的基础上,在欢迎页中,显示员工列表,要求做分页。每一页显示两条员工的数据。
**3.**在第二题基础上,增加模糊查询,要求分页。模糊的条件是姓名的模糊查询即可
数据库库表:
emp表:
/*
Navicat MySQL Data Transfer
Source Server : localhost_3306
Source Server Version : 50519
Source Host : localhost:3306
Source Database : test
Target Server Type : MYSQL
Target Server Version : 50519
File Encoding : 65001
Date: 2020-11-13 18:17:58
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for emp
-- ----------------------------
DROP TABLE IF EXISTS `emp`;
CREATE TABLE `emp` (
`empid` int(3) NOT NULL AUTO_INCREMENT,
`ename` varchar(32) NOT NULL,
`job` varchar(32) NOT NULL,
`sal` double(7,2) DEFAULT NULL,
`hiredate` datetime DEFAULT NULL,
PRIMARY KEY (`empid`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of emp
-- ----------------------------
INSERT INTO `emp` VALUES ('1', '李', 'manger', '100.00', '2020-11-11 16:31:33');
INSERT INTO `emp` VALUES ('2', '张三', '保安', '10000.00', '2020-11-20 16:59:53');
t_user表:
/*
Navicat MySQL Data Transfer
Source Server : localhost_3306
Source Server Version : 50519
Source Host : localhost:3306
Source Database : test
Target Server Type : MYSQL
Target Server Version : 50519
File Encoding : 65001
Date: 2020-11-13 18:17:40
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for t_user
-- ----------------------------
DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user` (
`userid` varchar(16) NOT NULL,
`password` varchar(16) NOT NULL,
`username` varchar(32) NOT NULL,
PRIMARY KEY (`userid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of t_user
-- ----------------------------
INSERT INTO `t_user` VALUES ('13878978999', '123456', '管理员');
INSERT INTO `t_user` VALUES ('13878@oracle.com', '123456', '张三');
dao数据访问对象:
BaseDao接口:
package com.oracle.dao;
import java.util.List;
public interface BaseDao<T> {
public abstract void insert(T t);
public abstract void update(T t);
public abstract void delete(T t);
public abstract T selectById(T t);
public abstract List<T> selectAll();
}
EmpDao类:
package com.oracle.dao;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.oracle.tools.DBTools;
import com.oracle.vo.Emp;
public class EmpDao implements BaseDao<Emp>{
@Override
public void insert(Emp t) {
String sql = "insert into emp(ename,job,sal,hiredate) values(?,?,?,?)";
DBTools.executeUpdate(sql, t.getEname(),t.getJob(),t.getSal(),t.getHiredate());
}
public void insertWithId(Emp t){
String sql = "insert into emp(empid,ename,job,sal,hiredate) values(?,?,?,?,?)";
DBTools.executeUpdate(sql, t.getEmpid(),t.getEname(),t.getJob(),t.getSal(),t.getHiredate());
}
@Override
public void update(Emp t) {
String sql="update emp set ename=?,job=?,sal=?,hiredate=? where empid=?";
DBTools.executeUpdate(sql, t.getEname(),t.getJob(),t.getSal(),t.getHiredate(),t.getEmpid());
}
@Override
public void delete(Emp t) {
String sql = "delete from emp where empid=?";
DBTools.executeUpdate(sql, t.getEmpid());
}
@Override
public Emp selectById(Emp t) {
Emp emp=null;
String sql = "select * from emp where empid=?";
List<Object[]> objs = DBTools.executeQuery(sql, t.getEmpid());
if(objs.size()>0){
Object[] obj = objs.get(0);
int empid = (Integer)obj[0];
String ename = (String)obj[1];
String job = (String)obj[2];
double sal = (Double)obj[3];
Timestamp time = (Timestamp)obj[4];
Date hiredate = new Date(time.getTime());
emp = new Emp(empid, ename, job, sal, hiredate);
}
return emp;
}
@Override
public List<Emp> selectAll() {
List<Emp> emps = null;
String sql = "select * from emp";
List<Object[]> objs = DBTools.executeQuery(sql, null);
if(objs.size()>0){
emps = new ArrayList<Emp>();
for(int i=0;i<objs.size();i++){
Object[] obj = objs.get(i);
int empid = (Integer)obj[0];
String ename = (String)obj[1];
String job = (String)obj[2];
double sal = (Double)obj[3];
Timestamp time = (Timestamp)obj[4];
Date hiredate = new Date(time.getTime());
Emp emp = new Emp(empid, ename, job, sal, hiredate);
emps.add(emp);
}
}
return emps;
}
public List<Emp> selectEmpsByPage(int pageNum,int num){
List<Emp> emps=null;
String sql="select * from emp limit ?,?";
List<Object[]> objs=DBTools.executeQuery(sql, (pageNum-1)*num,num);
if(objs!=null&&objs.size()>0){
emps=new ArrayList<Emp>();
for(int i=0;i<objs.size();i++){
Object[] obj=objs.get(i);
int empid=(Integer)obj[0];
String ename=(String)obj[1];
String job=(String)obj[2];
double sal=(Double)obj[3];
Timestamp time=(Timestamp)obj[4];
Date hiredate=new Date(time.getTime());
Emp emp=new Emp(empid, ename, job, sal, hiredate);
emps.add(emp);
}
}
return emps;
}
//总记录
public int getCount(){
int count=0;
String sql="select count(*) from emp";
List<Object[]> objs=DBTools.executeQuery(sql, null);
if(objs!=null&&objs.size()>0){
Object[] obj=objs.get(0);
count=((Long)obj[0]).intValue();
}
return count;
}
public List<Emp> selectEmpsByEname(int pageNum,int num,String key){
List<Emp> emps=null;
String sql="select * from emp where ename like ? limit ?,?";
List<Object[]> objs=DBTools.executeQuery(sql,"%"+key+"%" ,(pageNum-1)*num,num);
if(objs!=null&&objs.size()>0){
emps=new ArrayList<Emp>();
for(int i=0;i<objs.size();i++){
Object[] obj=objs.get(i);
int empid=(Integer)obj[0];
String ename=(String)obj[1];
String job=(String)obj[2];
double sal=(Double)obj[3];
Timestamp time=(Timestamp)obj[4];
Date hiredate=new Date(time.getTime());
Emp emp=new Emp(empid, ename, job, sal, hiredate);
emps.add(emp);
}
}
return emps;
}
public int getCountByEname(String key){
int count=0;
String sql="select count(*) from emp where ename like ?";
List<Object[]> objs=DBTools.executeQuery(sql, "%"+key+"%");
if(objs!=null&&objs.size()>0){
Object[] obj=objs.get(0);
count=((Long)obj[0]).intValue();
}
return count;
}
public List<Emp> selectEmpsByJob(int pageNum,int num,String key){
List<Emp> emps=null;
String sql="select * from emp where job like ? limit ?,?";
List<Object[]> objs=DBTools.executeQuery(sql,"%"+key+"%" ,(pageNum-1)*num,num);
if(objs!=null&&objs.size()>0){
emps=new ArrayList<Emp>();
for(int i=0;i<objs.size();i++){
Object[] obj=objs.get(i);
int empid=(Integer)obj[0];
String ename=(String)obj[1];
String job=(String)obj[2];
double sal=(Double)obj[3];
Timestamp time=(Timestamp)obj[4];
Date hiredate=new Date(time.getTime());
Emp emp=new Emp(empid, ename, job, sal, hiredate);
emps.add(emp);
}
}
return emps;
}
public int getCountByJob(String key){
int count=0;
String sql="select count(*) from emp where job like ?";
List<Object[]> objs=DBTools.executeQuery(sql, "%"+key+"%");
if(objs!=null&&objs.size()>0){
Object[] obj=objs.get(0);
count=((Long)obj[0]).intValue();
}
return count;
}
}
UserDao类:
package com.oracle.dao;
import java.util.List;
import com.oracle.tools.DBTools;
import com.oracle.vo.User;
public class UserDao implements BaseDao<User>{
@Override
public void insert(User t) {
// TODO Auto-generated method stub
}
@Override
public void update(User t) {
// TODO Auto-generated method stub
}
@Override
public void delete(User t) {
// TODO Auto-generated method stub
}
@Override
public User selectById(User t) {
User user= null;
String sql="select * from t_user where userid=?";
List<Object[]> objs=DBTools.executeQuery(sql, t.getUserid());
if(objs!=null&&objs.size()>0){
Object[] obj=objs.get(0);
String userid=(String)obj[0];
String password=(String)obj[1];
String username=(String)obj[2];
user=new User(userid,password,username);
}
return user;
}
@Override
public List<User> selectAll() {
// TODO Auto-generated method stub
return null;
}
}
vo值对象:
User类:
package com.oracle.vo;
public class User {
private String userid;
private String password;
private String username;
public User(){}
public User(String userid,String password,String username){
this.setUserid(userid);
this.setPassword(password);
this.setUsername(username);
}
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Override
public String toString() {
return "User [userid=" + userid + ", password=" + password + ", username=" + username + "]";
}
}
Emp类:
package com.oracle.vo;
import java.util.Date;
public class Emp {
private int empid;
private String ename;
private String job;
private double sal;
private Date hiredate;
public Emp(){}
public Emp(int empid,String ename,String job,double sal,Date hiredate){
this.setEmpid(empid);
this.setEname(ename);
this.setHiredate(hiredate);
this.setJob(job);
this.setSal(sal);
}
public int getEmpid() {
return empid;
}
public void setEmpid(int empid) {
this.empid = empid;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public double getSal() {
return sal;
}
public void setSal(double sal) {
this.sal = sal;
}
public Date getHiredate() {
return hiredate;
}
public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}
@Override
public String toString() {
return "Emp [empid=" + empid + ", ename=" + ename + ", job=" + job + ", sal=" + sal + ", hiredate=" + hiredate
+ "]";
}
}
DBTools工具类:
package com.oracle.tools;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class DBTools {
static{
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static Connection conn = null;
private static final String URL="jdbc:mysql://localhost:3306/test";
private static final String USER="root";
private static final String PASSWORD="root";
private static PreparedStatement pstat = null;
private static ResultSet rs = null;
public static Connection getConn(){
if(conn==null){
try {
conn = DriverManager.getConnection(URL, USER, PASSWORD);
conn.setAutoCommit(false);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return conn;
}
public static void executeUpdate(String sql,Object...objs){
conn = getConn();
try {
pstat = conn.prepareStatement(sql);
if(objs!=null&&objs.length>0){
for(int i=0;i<objs.length;i++){
pstat.setObject(i+1, objs[i]);
}
}
pstat.executeUpdate();
commit();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
rollback();
}
}
public static List<Object[]> executeQuery(String sql,Object...objs){
List<Object[]> list = new ArrayList<Object[]>();
conn = getConn();
try {
pstat = conn.prepareStatement(sql);
if(objs!=null&&objs.length>0){
for(int i=0;i<objs.length;i++){
pstat.setObject(i+1, objs[i]);
}
}
rs = pstat.executeQuery();
while(rs.next()){
Object[] obj = new Object[rs.getMetaData().getColumnCount()];
for(int i=0;i<obj.length;i++){
obj[i] = rs.getObject(i+1);
}
list.add(obj);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
public static void commit(){
if(conn!=null){
try {
conn.commit();
close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void rollback(){
if(conn!=null){
try {
conn.rollback();
close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void close(){
if(rs!=null){
try {
rs.close();
rs=null;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(pstat!=null){
try {
pstat.close();
pstat=null;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
conn=null;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
add.jsp:
<%@ page language="java" import="java.util.*" 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">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'addEmp.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
function $(id){
return document.getElementById(id);
}
window.οnlοad=function(){
let ename=$("ename");
let job=$("job");
let sal=$("sal");
let btn_add=$("btn_add");
let sp1=$("sp1");
let sp2=$("sp2");
let sp3=$("sp3");
btn_add.οnclick=function(){
let enameValue=ename.value;
let salValue=sal.value;
let jobValue=job.value;
let flag=true;
if(enameValue==null||enameValue==""){
flag=false;
sp1.innerHTML="<font color='red'>ename不能为空! </font>";
return;
}else{
flag=true;
sp1.innerHTML="";
}
if(jobValue==null||jobValue==""){
flag=false;
sp2.innerHTML="<font color='red'>job不能为空! </font>";
return;
}else{
flag=true;
sp2.innerHTML="";
}
if(salValue==null||salValue==""){
flag=false;
sp3.innerHTML="<font color='red'>sal不能为空! </font>";
return;
}else{
let salReg=/^\d\d{0,}\.{0,1}\d{1,2}$/;
if(salValue.match(salReg)==null){
flag=false;
sp3.innerHTML="<font color='red'>sal格式不正确! </font>";
return;
}else{
flag=true;
sp3.innerHTML="";
}
}
if(flag){
document.forms[0].submit();
}
}
}
</script>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
String username=(String)session.getAttribute("username");
if(username==null||username.equals("")){
%>
<jsp:forward page="index.jsp">
<jsp:param value="请登录" name="errorInfo" />
</jsp:forward>
<%
}
%>
<form action="addEmp.jsp" method="post">
Ename:<input type="text" name="ename" id="ename" /><span id="sp1"> </span><br/>
Job:<input type="text" name="job" id="job"/><span id="sp2"> </span><br/>
Sal:<input type="text" name="sal" id="sal"/><span id="sp3"> </span><br/>
<input type="button" value="添加" id="btn_add"><input type="reset" value="重置" /><br/>
</form>
</body>
</html>
addEmp.jsp:
<%@page import="com.oracle.vo.Emp"%>
<%@page import="com.oracle.dao.EmpDao"%>
<%@ page language="java" import="java.util.*" 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">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'addEmp.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
String username=(String)session.getAttribute("username");
if(username==null||username.equals("")){
%>
<jsp:forward page="index.jsp">
<jsp:param value="请登录" name="errorInfo" />
</jsp:forward>
<%
}
String ename=request.getParameter("ename");
String job=request.getParameter("job");
String sal=request.getParameter("sal");
EmpDao empDao=new EmpDao();
Emp emp=new Emp();
emp.setEname(ename);
emp.setJob(job);
emp.setSal(Double.parseDouble(sal));
emp.setHiredate(new Date());
empDao.insertWithId(emp);
%>
<jsp:forward page="welcome.jsp">
<jsp:param name="pn" value="1"/>
</jsp:forward>
<%
%>
</body>
</html>
check.jsp:
<%@page import="com.oracle.dao.UserDao"%>
<%@page import="com.oracle.vo.User"%>
<%@page language="java" import="java.util.*" 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">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'check.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
String userid=request.getParameter("userid");
String password=request.getParameter("password");
String remPass=request.getParameter("remPass");
String remTime=request.getParameter("remTime");
User user=new User();
user.setUserid(userid);
UserDao dao=new UserDao();
user=dao.selectById(user);
if(user==null){
%>
<jsp:forward page="index.jsp">
<jsp:param value="用户不存在!" name="errorInfo"/>
</jsp:forward>
<%
}else{
if(password.equals(user.getPassword())){
session.setAttribute("username", user.getUsername());
if(remPass!=null){
int second=0;
if(remTime.equals("0")){
second=0;
}else if(remTime.equals("1")){
second=60;
}else if(remTime.equals("2")){
second=5*60;
}else if(remTime.equals("3")){
second=10*60;
}else if(remTime.equals("4")){
second=30*60;
}
Cookie useridCookie=new Cookie("userid",userid);
useridCookie.setMaxAge(second);
Cookie passwordCookie=new Cookie("password",password);
passwordCookie.setMaxAge(second);
response.addCookie(useridCookie);
response.addCookie(passwordCookie);
}
%>
<jsp:forward page="welcome.jsp">
<jsp:param value="1" name="pn"/>
</jsp:forward>
<%
}else{
%>
<jsp:forward page="index.jsp">
<jsp:param value="密码不正确!" name="errorInfo"/>
</jsp:forward>
<%
}
}
%>
</body>
</html>
delete.jsp:
<%@page import="com.oracle.dao.EmpDao"%>
<%@page import="com.oracle.vo.Emp"%>
<%@ page language="java" import="java.util.*" 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">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'delete.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
String username=(String)session.getAttribute("username");
if(username==null||username.equals("")){
%>
<jsp:forward page="index.jsp">
<jsp:param value="请登录" name="errorInfo" />
</jsp:forward>
<%
}
int empid=Integer.parseInt(request.getParameter("empid"));
Emp emp=new Emp();
emp.setEmpid(empid);
EmpDao dao=new EmpDao();
dao.delete(emp);
%>
<jsp:forward page="welcome.jsp">
<jsp:param name="pn" value="1"/>
</jsp:forward>
<%
%>
</body>
</html>
index.jsp:
<%@page import="com.oracle.dao.UserDao"%>
<%@page import="com.oracle.vo.User"%>
<%@page language="java" import="java.util.*" 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">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
function $(id){
return document.getElementById(id);
}
window.οnlοad=function(){
let flag=true;
let userid=$("userid");
let password=$("password");
let btn_login=$("btn_login");
let sp1=$("sp1");
let sp2=$("sp2");
btn_login.οnclick=function(){
let userValue=userid.value;
let passValue=password.value;
if(userValue==null||userValue==""){
sp1.innerHTML="<font color='red'>用户名不能为空! </font>";
flag=false;
return;
}else{
sp1.innerHTML="";
flag=true;
}
if(passValue==null||passValue==""){
sp2.innerHTML="<font color='red'>密码不能为空! </font>";
flag=false;
return;
}
else{
sp2.innerHTML="";
flag=true;
}
let userReg=/(^1[3-9]\d{9}$)|(^[a-zA-Z0-9]\w{3,4}@[0-1a-zA-Z]{2,7}\.(cn|com|net)$)/;
let passReg=/^\d{4,16}$/;
if(userValue.match(userReg)==null){
sp1.innerHTML="<font color='red'>用户名格式不正确! </font>";
flag=false;
return;
}else{
sp1.innerHTML="";
}
if(passValue.match(passReg)==null){
sp2.innerHTML="<font color='red'>密码格式不正确! </font>";
flag=false;
return;
}else{
sp2.innerHTML="";
}
if(flag=true){
document.forms[0].submit();
}
}
}
</script>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
String info=request.getParameter("errorInfo");
if(info==null){
info="";
}
String userid="";
String password="";
Cookie[] cookies=request.getCookies();
if(cookies!=null){
for( Cookie c:cookies){
if(c.getName().equals("userid")){
userid=c.getValue();
}
if(c.getName().equals("password")){
password=c.getValue();
}
}
}
if(!userid.equals("")&&!password.equals("")){
UserDao userDao=new UserDao();
User user=new User();
user.setUserid(userid);
user=userDao.selectById(user);
if(user!=null&&password.equals(user.getPassword())){
session.setAttribute("username", user.getUsername());
%>
<jsp:forward page="welcome.jsp">
<jsp:param value="1" name="pn"/>
</jsp:forward>
<%
}
}
%>
<form action="check.jsp" method="post">
<span>用户名:</span><input type="text" name="userid" id="userid"/><span id="sp1"><%= info %></span><br>
<span>密码:</span><input type="password" name="password" id="password"/><span id="sp2"></span><br>
<input type="checkbox" name="remPass"/>记住密码
<select name="remTime">
<option value="0">请记住密码的时间</option>
<option value="1">一分钟</option>
<option value="2">五分钟</option>
<option value="3">十分钟</option>
<option value="4">半个小时</option>
</select><br/>
<input type="button" value="登录" id="btn_login"/>
</form>
</body>
</html>
logout.jsp:
<%@ page language="java" import="java.util.*" 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">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'logout.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
Cookie useridCookie=new Cookie("userid","");
Cookie passwordCookie=new Cookie("password","");
response.addCookie(useridCookie);
response.addCookie(passwordCookie);
session.invalidate();
response.sendRedirect("index.jsp");
%>
</body>
</html>
update.jsp:
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="com.oracle.vo.Emp"%>
<%@page import="com.oracle.dao.EmpDao"%>
<%@ page language="java" import="java.util.*" 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">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'update.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
function $(id){
return document.getElementById(id);
}
window.οnlοad=function(){
let ename=$("ename");
let job=$("job");
let sal=$("sal");
let btn_update=$("btn_update");
let sp1=$("sp1");
let sp2=$("sp2");
let sp3=$("sp3");
btn_update.οnclick=function(){
let enameValue=ename.value;
let salValue=sal.value;
let jobValue=job.value;
let flag=true;
if(enameValue==null||enameValue==""){
flag=false;
sp1.innerHTML="<font color='red'>ename不能为空! </font>";
return;
}else{
flag=true;
sp1.innerHTML="";
}
if(jobValue==null||jobValue==""){
flag=false;
sp2.innerHTML="<font color='red'>job不能为空! </font>";
return;
}else{
flag=true;
sp2.innerHTML="";
}
if(salValue==null||salValue==""){
flag=false;
sp3.innerHTML="<font color='red'>sal不能为空! </font>";
return;
}else{
let salReg=/^\d\d{0,}\.{0,1}\d{1,2}$/;
if(salValue.match(salReg)==null){
flag=false;
sp3.innerHTML="<font color='red'>sal格式不正确! </font>";
return;
}else{
flag=true;
sp3.innerHTML="";
}
}
if(flag){
document.forms[0].submit();
}
}
}
</script>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
String username=(String)session.getAttribute("username");
if(username==null||username.equals("")){
%>
<jsp:forward page="index.jsp">
<jsp:param value="请登录" name="errorInfo" />
</jsp:forward>
<%
}
String empid=request.getParameter("empid");
EmpDao dao=new EmpDao();
Emp emp=new Emp();
emp.setEmpid(Integer.parseInt(empid));
emp=dao.selectById(emp);
if(emp!=null){
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
%>
<form action="updateEmp.jsp" method="post">
<input type="hidden" name="empid" value="<%=emp.getEmpid() %>"/>
<input type="hidden" name="hiredate" value="<%=sdf.format(emp.getHiredate()) %>"/>
Ename:<input type="text" name="ename" value="<%=emp.getEname()%>" id="ename" /><span id="sp1"> </span><br/>
Job:<input type="text" name="job" value="<%=emp.getJob() %>" id="job"/><span id="sp2"> </span><br/>
Sal:<input type="text" name="sal" value="<%=emp.getSal() %>" id="sal"/><span id="sp3"> </span><br/>
<input type="button" value="修改" id="btn_update"><input type="reset" value="重置" /><br/>
</form>
<%
}else{
%>
<jsp:forward page="welcome.jsp">
<jsp:param name="username" value="<%=username %>"/>
<jsp:param name="pn" value="1"/>
</jsp:forward>
<%
}
%>
</body>
</html>
updateEmp.jsp:
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="com.oracle.vo.Emp"%>
<%@page import="com.oracle.dao.EmpDao"%>
<%@ page language="java" import="java.util.*" 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">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'updateEmp.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
String username=(String)session.getAttribute("username");
if(username==null||username.equals("")){
%>
<jsp:forward page="index.jsp">
<jsp:param value="请登录" name="errorInfo" />
</jsp:forward>
<%
}
String ename=request.getParameter("ename");
String empid=request.getParameter("empid");
String hiredate=request.getParameter("hiredate");
String job=request.getParameter("job");
String sal=request.getParameter("sal");
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
EmpDao empDao=new EmpDao();
Emp emp=new Emp();
emp.setEname(ename);
emp.setEmpid(Integer.parseInt(empid));
emp.setJob(job);
emp.setSal(Double.parseDouble(sal));
emp.setHiredate(sdf.parse(hiredate));
empDao.update(emp);
%>
<jsp:forward page="welcome.jsp">
<jsp:param name="pn" value="1"/>
</jsp:forward>
<%
%>
</body>
</html>
welcome.jsp:
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="com.oracle.vo.Emp"%>
<%@page import="com.oracle.dao.EmpDao"%>
<%@ page language="java" import="java.util.*" 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">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'welcome.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script>
window.οnlοad=function(){
let deleteas=document.getElementsByName("deletea");
for(let i=0;i<deleteas.length;i++){
let a1=deleteas[i];
a1.οnclick=function(){
let f=confirm("确定要删除吗?");
return f;
}
}
}
</script>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
String username=(String)session.getAttribute("username");
String pn=request.getParameter("pn");
if(username==null||username.equals("")||pn==null||pn.equals("")){
%>
<jsp:forward page="index.jsp">
<jsp:param value="请登录" name="errorInfo"/>
</jsp:forward>
<%
}else{
EmpDao dao=new EmpDao();
//当前页数
int pageNum=Integer.parseInt(pn);
//查询页数
int num=2;
int count=0;
int totalPage=1;
List<Emp> emps=null;
String keyValue=request.getParameter("keyValue");
String key=request.getParameter("key");
if(keyValue==null||keyValue.equals("")||keyValue.equals("null")){
count=dao.getCount();
if(count%num==0){
totalPage=count/num;
}else{
totalPage=(count/num)+1;
}
if(pageNum<1){
pageNum=1;
}else if(pageNum>totalPage){
pageNum=totalPage;
}
emps=dao.selectEmpsByPage(pageNum, num);
}else{
if(key.equals("ename")){
count=dao.getCountByEname(keyValue);
if(count%num==0){
totalPage=count/num;
}else{
totalPage=(count/num)+1;
}
if(pageNum<1){
pageNum=1;
}else if(pageNum>totalPage){
pageNum=totalPage;
}
emps=dao.selectEmpsByEname(pageNum, num, keyValue);
}else if(key.equals("job")){
count=dao.getCountByJob(keyValue);
if(count%num==0){
totalPage=count/num;
}else{
totalPage=(count/num)+1;
}
if(pageNum<1){
pageNum=1;
}else if(pageNum>totalPage){
pageNum=totalPage;
}
emps=dao.selectEmpsByJob(pageNum, num, keyValue);
}
}
%>
<table border="1" align="center">
<tr> <td colspan="6">欢迎 <%=username %><a href="logout.jsp">退出</a></td></tr>
<tr> <td colspan="6">
<form action="welcome.jsp" method="post">
<input type="hidden" name="username" value="<%=username %>"/>
<input type="hidden" name="pn" value="1"/>
<select name="key">
<option value="ename">按照姓名模糊查询</option>
<option value="job">按照职位模糊查询</option>
</select>
<input type="text" name="keyValue"/>
<input type="submit" value="查询 "/>
</form>
</td>
</tr>
<tr>
<td colspan="6"><a href="add.jsp?username=<%=username %>">添加员工</a></td>
</tr>
<tr>
<th>编号</th>
<th>姓名</th>
<th>职位</th>
<th>工资</th>
<th>入职时间</th>
<th>操作</th>
</tr>
<%
if(emps==null&&emps.size()==0){
%>
<%="没有数据可查!" %>
<%
}else{
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
for(int i=0;i<emps.size();i++){
Emp e=emps.get(i);
String hiredate=sdf.format(e.getHiredate());
%>
<tr>
<td><%=e.getEmpid() %></td>
<td><%=e.getEname() %></td>
<td><%=e.getJob() %></td>
<td><%=e.getSal() %></td>
<td><%=hiredate %></td>
<td><a href="update.jsp?empid=<%=e.getEmpid()%>&username=<%=username %>">修改</a>/
<a href="delete.jsp?empid=<%=e.getEmpid()%>&username=<%=username %>" name="deletea">删除</a>
</td>
</tr>
<%
}
%>
<tr align="center">
<td colspan="6" >
<a href="welcome.jsp?pn=1&username=<%=username %>&key=<%=key %>&keyValue=<%=keyValue %>">首页</a>
<a href="welcome.jsp?pn=<%=pageNum-1 %>&username=<%=username %>&key=<%=key %>&keyValue=<%=keyValue %>">上一页</a>
<a href="welcome.jsp?pn=<%=pageNum+1 %>&username=<%=username %>&key=<%=key %>&keyValue=<%=keyValue %>">下一页</a>
<a href="welcome.jsp?pn=<%=totalPage %>&username=<%=username %>&key=<%=key %>&keyValue=<%=keyValue %>">尾页</a>
</td>
</tr>
<%
}
%>
</table>
<%
}
%>
</body>
</html>
目录结构:
工资 入职时间 操作 <% if(emps==null&&emps.size()==0){ %> <%="没有数据可查!" %> <% }else{ SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); for(int i=0;i
**目录结构:**
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-sxwUzcN7-1605264223326)(../AppData/Roaming/Typora/typora-user-images/1605264187824.png)]