介绍
一个简单的JSP项目,学校实训周写的,花了三四天时间,纯JSP代码,可分享资源应付学校实训。项目可以除了基本的增删改查之外,对安全性方面进行了一定的完善,还可以通过密保找回密码,修改资料也需要验证二次密码。
一、找回密码功能
第一步:查找是否有这个人-->返回布尔值
第二步:查找密保问题-->返回密保问题
第三步:对密保回答进行验证
第四步:密保问题正确则返回Uid值
二、部分代码
1.JSP
Login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<style>
body{
font-size: 12px;
font-family: "宋体";
}
h1{
text-align:center;
color: #4faccb;
}
form{
width:320px;
height:200px;
border-radius:20px;
padding-top:13px;
margin:15px auto 50px;
background: #f5f8fd;
border:3px solid #4faccb;
}
.a1{
background:url(../resources/images/1.jpg) no-repeat 5px center #FFF;
color:#999;
}
.a2{
background:url(../resources/images/2.jpg) no-repeat 5px center #FFF;
}
.a1,.a2{
width:152px;
height:18px;
border:1px solid #38a1bf;
padding:2px 2px 2px 22px;
}
p span{
width:60px;
display: inline-block;
text-align: right;
}
p span2{
width:40px;
display: inline-block;
padding-left: 5px;
text-align: left;
}
p{
margin-top:25px;
text-align:center;
}
.b1,.b2{
width:55px;
height:25px;
border-radius:3px;
border:1px solid #6b57ea;
}
.b1{
margin-right: 15px;
}
.b2{
margin-left:15px;
}
span1{
margin-right: 13px;
margin-left: 15px;
}
.style1{
margin-top: 5px;
}
a{
color: #38A1BF;
text-decoration: none;
}
</style>
</head>
<body>
<br>
<h1>欢迎登录</h1>
<form action="/untitled_war_exploded/pbLoginServlet" method="post">
<p><span>用户名:</span>
<input type="text" placeholder="用户名/电话号码" name="username" class="a1"></p>
<p><span>登录密码:</span>
<input type="password" name="password" class="a2"></p>
<p class="style1"><input type="radio" name="type" value="user"><span2>用户</span2>
<input type="radio" name="type" value="controller"><span2>管理员</span2></p>
<p class="style1">
<input type="submit" value="登录" class="b1">
<input type="reset" value="清空" class="b2">
</p>
<p class="style1"><a href="registered.jsp">没有账号?</a>
<span1 class="span1"></span1>
<a href="recoverPassword.jsp">忘记密码?</a></p>
</form>
</body>
</html>
UserIndex.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<style>
body{font-size:16px; font-family:"微软雅黑";}
.div{
width: 949px;
height: 687px;
margin: 0 auto;
padding: 0;
text-align: center;
background: url("../../resources/images/sy.png") no-repeat;
}
h1{
margin: 200px auto 0;
padding: 0;
text-align: center;
}
a{
color: #38A1BF;
text-decoration: none;
width:120px;
height:25px;
border:1px solid #95b871;
padding: 2px;
border-radius:10px;
}
</style>
</head>
<body>
<div class="div">
<br>
<h1>欢迎用户${sessionScope.username}进入系统</h1><br>
请选择你要进行的操作:
<a href="/untitled_war_exploded/userInformationServlet?Uid=${sessionScope.Uid}&username=${sessionScope.username}">查看个人资料</a>
<a href="/untitled_war_exploded/mailListInformationServlet">查看通讯录</a>
<!--添加备忘录-->
<a href="/untitled_war_exploded/memorandumDisplay?Uid=${sessionScope.Uid}">查看备忘录</a>
<a href="/untitled_war_exploded/phonebook/login.jsp">退出登录</a>
</div>
</body>
</html>
UserDisplay.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>页面信息</title>
<style>
body{
font-size: 14px;
font-family: "宋体";
text-align: center;
margin: 0 auto;
}
h1{
text-align:center;
color: #4faccb;
}
table{
width:630px;
height:120px;
border-radius:20px;
padding-top:8px;
margin:15px auto;
background: #f5f8fd;
border:3px solid #4faccb;
}
tr{
width: 500px;
height: 30px;
}
td{
text-align: center;
width:100px;
height:20px;
}
a{
color: #38A1BF;
text-decoration: none;
}
</style>
</head>
<body>
<br>
<h1>
用户${sessionScope.username}信息表
</h1>
<div>
<table>
<tr>
<td>Uid</td>
<td>用户名</td>
<td>登录密码</td>
<td>邮箱</td>
<td>住址</td>
<td>生日</td>
<td>操作</td>
</tr>
<c:set var="user" value="${sessionScope.pbUser}"/>
<tr>
<td>${user.uid}</td>
<td>${user.username}</td>
<td>${user.password}</td>
<td>${user.email}</td>
<td>${user.address}</td>
<td>${user.birthday}</td>
<td><a href="javascript:mod(${user.uid})">修改资料</a></td>
</tr>
</table>
<a href="/untitled_war_exploded/phonebook/user/userIndex.jsp"><--返回上一级</a>
</div>
<script>
function mod(Uid){
let password1 = prompt("请验证二级密码");
location.href="/untitled_war_exploded/userInformationServlet?Uid="+Uid+"&password1="+password1;
}
</script>
</body>
</html>
2.Servlet
pbLoginServlet.java
import pb.phonebook.pbUser;
import utils.StringUtil;
import pb.phonebook.phoneDao;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
@WebServlet(name = "pbLoginServlet",urlPatterns = "/pbLoginServlet")
public class pbLoginServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=utf-8");
if(!(StringUtil.isNotEmptyOrNull(req.getParameter("type"))
&&StringUtil.isNotEmptyOrNull(req.getParameter("username"))
&&StringUtil.isNotEmptyOrNull(req.getParameter("username")))){
resp.getWriter().println("<script>alert('存在输入为空');location.href='/untitled_war_exploded/phonebook/login.jsp';</script>");
}
if(req.getParameter("type").equals("user")){
String username = String.valueOf(req.getParameter("username"));
String password = String.valueOf(req.getParameter("password"));
int Uid = phoneDao.login(username,password);
if(Uid!=0){
//查询到Uid,进入用户登陆后界面,将Uid和username放入session作用域
HttpSession httpSession = req.getSession();
httpSession.setAttribute("Uid",Uid);
httpSession.setAttribute("username",username);
resp.sendRedirect("/untitled_war_exploded/phonebook/user/userIndex.jsp");
}else {
resp.getWriter().println("<script>alert('用户登录名或密码错误');location.href='/untitled_war_exploded/phonebook/login.jsp';</script>");
}
}
if(req.getParameter("type").equals("controller")){
if(req.getParameter("username").equals("admin")&&req.getParameter("password").equals("yxx")){
//账号密码正确,显示所有用户信息
List<pbUser> list = phoneDao.display();
req.getSession().setAttribute("pbUserList",list);
req.getRequestDispatcher("/phonebook/administrators/management.jsp").forward(req,resp);
}else {
resp.getWriter().println("<script>alert('管理员账号或者密码错误');location.href='/untitled_war_exploded/phonebook/login.jsp';</script>");
}
}
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
}
总结
用来对付学校的实训是没什么大问题的
完整文件目录: