项目概览
文件目录:
实现登录功能:
- index.jsp
验证登录用户是否存在数据库:
- validate.jsp
- UserTable.java
- Check.java
检查编码方式:
- Filter2.java
安全功能,没登录则不能跳至其他页面:
- islogin.jsp
创建主页面框架:
- main.jsp
框架顶部,左部,右部:
- top.jsp
- left.jsp
- right.jsp
查询功能:
- select.jsp
- selectlogin.jsp
录入功能:
- insert.jsp
- insertlogin.jsp
删除功能:
- delete.jsp
- deletelogin.jsp
登录功能
登陆页面代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录</title>
<style type="text/css">
input[type="submit"]{
width:80px;
border:0px;
color:#ffffff;
background-color: #00bbff;
}
input[type="text"]{
color:#00bbff;
outline:0;
}
input[type="password"]{
color:#00bbff;
outline:0;
}
#i{
background-origin:border-box;
background-image:url(image/index1.jpg);
background-size: 500px 500px;
background-repeat: no-repeat;
background-position: center;
}
#p{
position: absolute;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
-moz-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
-o-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
</style>
</head>
<body>
<div id="p">
<form style="height: 400px;width: 400px;" action="<%= request.getContextPath() %>/validate.jsp" method="post">
<div id="i">
<table id="table" cellspacing="0" cellpadding="4" align="center" >
<tr>
<td align="center" colspan="2" ><font color="#ffffff">学生管理系统</font></td><br>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td><font color="#ffffff">账号</font></td>
<td><input style="background:transparent;border:1px solid #ffffff" type="text" name="username"></td>
</tr>
<tr>
<td><font color="#ffffff">密码</font></td>
<td><input style="background:transparent;border:1px solid #ffffff;color: #808080" type="password" name="password"></td>
</tr>
<tr>
<td></td>
<tr>
<td colspan="2" align="center">
<input type="submit" value="登录">
</td>
</tr>
<tr>
<td></td>
</tr>
</table>
</div>
</form>
</div>
</body>
</html>
验证登录用户是否存在数据库
代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="user" class="com.wudonda.UserTable"></jsp:useBean>
<jsp:useBean id="check" class="com.wudonda.Check"></jsp:useBean>
<jsp:setProperty property="*" name="user"/>
<%
if(check.UserCheck(user)){
session.setAttribute("user", user);
%>
<jsp:forward page="main.jsp"></jsp:forward>
<%}else{ %>
<jsp:forward page="index.jsp"></jsp:forward>
<%}%>
</body>
</html>
实现逻辑:
调用check的方法UserCheck,连接数据库,查询数据库是否存在该用户,存在跳至主页面,不存在重新登录。
知识点:
jsp的usebean实例化class中的类,实例化的对象名就是id名。
jsp的setproperty为name属性中的对象赋值。
安全功能,没登录则不能跳至其他页面
代码:
<%@page import="com.wudonda.UserTable"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
UserTable user = (UserTable)session.getAttribute("user");
if(user==null){
%>
<jsp:forward page="index.jsp"></jsp:forward>
<%}%
实现逻辑:
通过jsp的include指令将该jsp文件插入其他jsp文件,则访问其他页面时会检查session的user值是否为空,为空则会直接跳至登陆页面。
创建主页面框架
代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<jsp:include page="islogin.jsp"></jsp:include>
<frameset rows="20%,*">
<frame src="top.jsp">
<frameset cols="20%,*">
<frame src="left.jsp" marginwidth="10" marginheight="10">
<frame src="right.jsp" name="right.jsp">
</frameset>
</frameset>
<body>
</body>
</html>
框架顶部,左部,右部
顶部代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
</style>
</head>
<jsp:include page="islogin.jsp"></jsp:include>
<body>
<h3><marquee style="color: #808080">厚德 笃学 求是 创新</marquee></h3>
<h2 align="center" style="color: #808080">学生管理系统</h2>
</body>
</html>
查询功能
代码:
<%@page import="com.wudonda.studentinfo"%>
<%@page import="java.util.Iterator"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="check" class="com.wudonda.Check"></jsp:useBean>
<form action="">
<table cellspacing="0" cellpadding="4" >
<tr>
<td>姓名</td>
<td>性别</td>
<td>入学时间</td>
<td>专业</td>
</tr>
<%
if(check.select()!=null){
Iterator it= check.select().iterator();
while(it.hasNext()){
studentinfo student = (studentinfo)it.next();
%>
<tr>
<td><%=student.getName() %></td>
<td><%=student.getSex() %></td>
<td><%=student.getGrade() %></td>
<td><%=student.getZhuanye()%></td>
</tr>
<%}} %>
</table>
</form>
</body>
</html>
录入功能
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
#p{
position: absolute;
top: 45%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
-moz-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
-o-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
</style>
</head>
<body>
<div id="p">
<form action="insert.jsp" method="post">
<table style="height: 300px;width: 300px" bgcolor="#33ffdd" border="0" cellspacing="0" cellpadding="4" align="center">
<tr>
<td colspan="2" align="center">录入学生信息</td>
</tr>
<tr>
<td align="center">姓名</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td align="center">性别</td>
<td>
<input type="radio" name="sex" checked="checked" value="男">男
<input type="radio" name="sex" value="女">女
</td>
</tr>
<tr>
<td align="center">入学时间</td>
<td><input type="text" name="grade"></td>
</tr>
<tr>
<td align="center">专业</td>
<td>
<select name="zhuanye">
<option>计算机科学技术</option>
<option>软件工程</option>
<option>计算机应用技术</option>
<option>物联网工程</option>
<option>网络安全</option>
<option>大数据</option>
</select>
</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="提交"></td>
</tr>
</table>
</form>
</div>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="student" class="com.wudonda.studentinfo"></jsp:useBean>
<jsp:useBean id="check" class="com.wudonda.Check"></jsp:useBean>
<jsp:setProperty property="*" name="student"/>
<%
if(check.insert(student)){
out.println("success");
}else{
out.println("error");
}
%>
</body>
</html>
删除功能
代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
#p{
position: absolute;
top: 45%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
-moz-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
-o-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
</style>
</head>
<body>
<div id="p">
<form action="delete.jsp" method="post">
<table style="height: 300px;width: 300px" bgcolor="#33ffdd" border="0" cellspacing="0" cellpadding="4" align="center">
<tr>
<td colspan="2" align="center">删除学生信息</td>
</tr>
<tr>
<td align="center">姓名</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td align="center">性别</td>
<td>
<input type="radio" name="sex" checked="checked" value="男">男
<input type="radio" name="sex" value="女">女
</td>
</tr>
<tr>
<td align="center">年级</td>
<td><input type="text" name="grade"></td>
</tr>
<tr>
<td align="center">专业</td>
<td>
<select name="zhuanye">
<option>计算机科学技术</option>
<option>软件工程</option>
<option>计算机应用技术</option>
<option>物联网工程</option>
<option>网络安全</option>
<option>大数据</option>
</select>
</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="提交"></td>
</tr>
</table>
</form>
</div>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="student" class="com.wudonda.studentinfo"></jsp:useBean>
<jsp:useBean id="check" class="com.wudonda.Check"></jsp:useBean>
<jsp:setProperty property="*" name="student"/>
<%
if(check.delete(student)){
out.println("sucess");
}else{
out.println("error");
}
%>
</body>
</html>