2024/02/15
登录原码:
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<html>
<head>
<title>JSP - Hello World</title>
</head>
<body background="imagine/鸡哥头.jpg" style="background-repeat:no-repeat;background-attachment:fixed;background-size:100% 100%">
<div style="text-align: center">
<h1> 登录界面</h1><br>
<form method="post" id="login" action="login-servlet">
账号:<input type="text" name="nameid" id="nameid"><br><br>
密码:<input type="password" name="pwd" id="pwd"><br><br>
<span id="msg" style="font-size: 12px;color:red">${msg}</span><br>
<a href="register.jsp">没有账号?去注册</a>
<button type="button" id="btm">登录</button> 
<br>
<a href="findback_pwd.jsp">忘记密码?点此处找回</a>
</form>
<script>
document.getElementById("btm").addEventListener("click", function(){
var a=confirm("确认登录嘛?");
if(!a)
{
document.getElementById('msg').innerHTML="您已取消登录";
return;
}
var id=document.getElementById("nameid").value;
var pwd=document.getElementById("pwd").value;
if(!is(id))
{
document.getElementById('msg').innerHTML="您的账号不能为空";
return;
}
if(!is(pwd))
{
document.getElementById('msg').innerHTML="您的密码不能为空";
return;
}
document.getElementById('login').submit();
});
function is(s)
{
if (s.length==0||s.trim()=="")
return false;
else return true;
}
</script>
</div>
</body>
</html>
package com.example.demo;
import bean.Base_InformationBean;
import bean.InfoDAO;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(value="/login-servlet")
public class LoginServlet extends HttpServlet {
private String code;
private String password;
private Base_InformationBean hhh;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
code=request.getParameter("nameid");
password=request.getParameter("pwd");
hhh=new Base_InformationBean();
hhh.setCode(code);
hhh.setPassword(password);
boolean flag= new InfoDAO().Login(hhh);
if(flag)
{
HttpSession session= request.getSession();
session.setAttribute("User",hhh);
response.sendRedirect("success.jsp");
}
else
{
request.setAttribute("msg","账号或密码错误");
try {
request.getRequestDispatcher("index.jsp").forward(request,response);
} catch (ServletException e) {
throw new RuntimeException(e);
}
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/html");
doGet(request,response);
}
}
注册:
<%--
Created by IntelliJ IDEA.
User: 龚涵彬
Date: 2024/2/2
Time: 19:52
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>账号注册</title>
</head>
<body>
<h1>注册</h1>
<div style="text-align: center">
<form method="post" id="register" action="register-servlet">
用户名:<input type="text" name="nameid" id="nameid"><br><br>
 密码:<input type="password" name="pwd1" id="pwd1"><br><br>
再输入一次密码:<input type="password" name="pwd2" id="pwd2">    <br><br>
<span id="msg" style="font-size: 12px;color:red">${msg}</span><br><br>
<button type="button" id="btm">注册</button>
</form>
<script>
document.getElementById("btm").addEventListener("click",function () {
var nameid=document.getElementById("nameid").value;
var pwd1=document.getElementById("pwd1").value;
var pwd2=document.getElementById("pwd2").value;
if(!is(nameid))
{
document.getElementById('msg').innerHTML="您要注册的用户名不能为空";
return;
}
if(nameid.length>20)
{
document.getElementById('msg').innerHTML="您的用户名长度不能超过20个字符";
return;
}
if(!is(pwd1))
{
document.getElementById('msg').innerHTML="您的密码不能为空";
return;
}
if(!is(pwd2))
{
document.getElementById('msg').innerHTML="您第二次密码输入的不能为空";
return;
}
if(pwd1.length>20)
{
document.getElementById('msg').innerHTML="您的密码长度不能超过20个字符";
return;
}
if(pwd1!==pwd2)
{
document.getElementById('msg').innerHTML="您两次密码输入不一致";
return;
}
var a=confirm("确认注册嘛?");
if(!a)
{
return;
}
else document.getElementById("register").submit();
})
function is(s)
{
if (s.length===0||s.trim()==="")
return false;
else return true;
}
</script>
</div>
</body>
</html>
package com.example.demo;
import bean.Base_InformationBean;
import bean.InfoDAO;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(value="/register-servlet")
public class FegisterServlet extends HttpServlet {
private String code;
private String password;
private Base_InformationBean hhh;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
code=request.getParameter("nameid");
password=request.getParameter("pwd1");
hhh=new Base_InformationBean();
hhh.setCode(code);
hhh.setPassword(password);
if(new InfoDAO().findCode(hhh))
{
request.setAttribute("msg","该账号已被注册请重新填写");
try {
request.getRequestDispatcher("register.jsp").forward(request,response);
} catch (ServletException e) {
throw new RuntimeException(e);
}
}
else {
new InfoDAO().register(hhh);
request.setAttribute("msg", "注册完成");
try {
request.getRequestDispatcher("index.jsp").forward(request, response);
} catch (ServletException e) {
throw new RuntimeException(e);
}
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/html");
doGet(request,response);
}
}
找回:
<%--
Created by IntelliJ IDEA.
User: 龚涵彬
Date: 2024/2/3
Time: 16:43
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>找回密码</title>
</head>
<body>
<h1>找回密码</h1>
<div style="text-align: center">
<form action="find-servlet" id="firststep" method="post">
<label for="nameid">请输入你要找回密码的账号:</label><input type="text" name="nameid" id="nameid"><br>
<span id="msg" style="font-size: 12px;color:red">${msg}</span><br>
<input type="button" id="btm" value="确定">
</form>
<script>
document.getElementById("btm").addEventListener("click",function () {
var a=document.getElementById("nameid").value;
if (a.length===0||a.trim()==="")
{
document.getElementById('msg').innerHTML="您的输入不能为空";
return;
}
var b=confirm("确认账号无误?")
if(!b)
{
document.getElementById('msg').innerHTML="您已取消";
return;
}
document.getElementById("firststep").submit();
})
</script>
</div>
</body>
</html>
package com.example.demo;
import bean.Base_InformationBean;
import bean.InfoDAO;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(value = "/find-servlet")
public class FindcodeServlet extends HttpServlet {
private String code;
private Base_InformationBean hhh;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
code=request.getParameter("nameid");
hhh=new Base_InformationBean();
hhh.setCode(code);
boolean flag=new InfoDAO().findCode(hhh);
if(!flag)
{
request.setAttribute("msg","该账号不存在,请注册");
try {
request.getRequestDispatcher("findback_pwd.jsp").forward(request,response);
}
catch (ServletException e) {
throw new RuntimeException(e);
}
}else
{
request.setAttribute("code",code);
try {
request.getRequestDispatcher("change_pwd.jsp").forward(request,response);
} catch (ServletException e) {
throw new RuntimeException(e);
}
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/html");
doGet(request,response);
}
}