以下是项目结构:
jdbc_demo包是管理mysql数据库的加载连接和查询;
Serclet下的Myservle项目是后台管理用户请求数据;
以下是Myservlet项目代码:
package servlet;
import jdbc_demo.jdbc;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import static jdbc_demo.jdbc.compare;
@WebServlet("/Myservlet") //注解配置servlet-patten:/Myservlet web.xml无需再配置;
public class Myservlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置编码格式
response.setCharacterEncoding("utf-8");
//获取请求的数据
String username = request.getParameter("username");
String password = request.getParameter("password");
/*
* state=1,则输入的用户名与密码与数据库中的数据匹配
* 登陆成功后的操作
* */
if (jdbc.compare(username,password)==1){
// PrintWriter out = response.getWriter();
// out.write("<html>");
// out.write("<head>");
// out.write("<meta charset='UTF-8'>");
// out.write("<title>成功</title>");
// out.write("<script>alert('登录成功')</script>");
// out.write("<style> .body{" +
// " background-image: url('1.jpg');n" +
// " background-position: center,center;" +
// " background-repeat: no-repeat;" +
// " background-attachment: fixed;" +
// " background-size: cover;" +
// "}</style>");
// out.write("</head>");
// out.write("<body class='body'>");
// out.write("成功");
// out.write("</body>");
// out.write("</html>");
// 也可以重定向到Suee.jsp的网页
request.getRequestDispatcher("Suee.jsp").forward(request,response);
}else{
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
out.write("<html>");
out.write("<head>");
out.write("<meta charset='UTF-8'>");
out.write("<title>失败</title>");
out.write("<script>alert('登录失败');window.location.assign('http://localhost/login_demo5/')</script>");
out.write("</head>");
out.write("<body>");
out.write("</body>");
out.write("</html>");
//request.getRequestDispatcher("index.jsp").forward(request,response);
}
}
}
jdbc类:连接数据库与判断客户端的信息与数据库的信息是否匹配
package jdbc_demo;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class jdbc { //jdbc工具类
public static int compare(String account,String password){
int state=0; //定义一个state变量来判断用户名与密码是否匹配
my m=new my(); //封装my类
Connection con=m.getCon(); //获取my类下的getCon()方法
Statement stmt= null;
try {
stmt = con.createStatement();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
ResultSet resultSet= null;
try {
resultSet = stmt.executeQuery("select*from student");
while(resultSet.next()){
if (account.equals(resultSet.getString("username"))&&password.equals(resultSet.getString("password"))){
state=1;
}
System.out.println(state);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return state;
}
}
首页:index.jsp--------css+javascript写的
<%--
Created by IntelliJ IDEA.
User: 赖波波
Date: 2021/6/4
Time: 16:25
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录界面</title>
<script type="text/javascript">
function registe() {
window.location.href="registe.jsp";
}
</script>
<style>
.user{
/*background:none transparent scroll repeat 0% 0%;*/
width: 200px;
height: 35px;
margin-left: 531px;
margin-bottom: 30px;
background:transparent;
border: white solid 1px;
border-radius: 5px;
color: white;
}
.password{
color: white;
width: 200px;
height: 35px;
margin-left: 531px;
margin-bottom: 30px;
background:transparent;
border: white solid 1px;
border-radius: 5px;
}
.button{
padding-bottom: 22px;
border: 1px solid;
border-radius: 5px;
background-color: rgba(255, 255, 255, 0.5);
color: white;
width: 80px;
height: 20px;
margin-left: 526px;
margin-bottom: 10px;
font-size: 16px;
}
.h1{
margin-bottom: 50px;
color: white;
margin-top: 110px;
}
.body{
background-image: url("1.png");
background-position: center,center;
background-repeat: no-repeat;
background-attachment: fixed;
background-size: cover;
}
.button1{
padding-bottom: 22px;
border: 1px solid;
border-radius: 5px;
background-color: rgba(255, 255, 255, 0.5);
color: white;
width: 80px;
height: 20px;
margin-left: 50px;
font-size: 16px;
}
</style>
</head>
<body class="body">
<h1 align="center" class="h1">登录</h1>
<form action="/login_demo5/Myservlet" method="post">
<input type="text" name="username" placeholder="请输入用户名" class="user"> <br>
<input type="password" name="password" placeholder="请输入密码" class="password">
<br>
<input type="submit" vlaue="登录" align="center" class="button">
<input type="button" value="注册" class="button1" onclick="registe()">
</form>
</body>
</html>
登录成功界面:
<%--
Created by IntelliJ IDEA.
User: 赖波波
Date: 2021/6/4
Time: 18:52
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>成功</title>
<style>
.body{
background-image: url("1.jpg");
background-position: center,center;
background-repeat: no-repeat;
background-attachment: fixed;
background-size: cover;
}
.h1{
color: rgb(246, 255, 16);
}
</style>
</head>
<body class="body">
<%
String user = request.getParameter("username");
%>
<h1 align="center" class="h1">欢迎<%=user%>登录</h1>
</body>
</html>
注册界面:
<%--
Created by IntelliJ IDEA.
User: 赖波波
Date: 2021/6/5
Time: 21:36
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>注册</title>
<style>
.user{
/*background:none transparent scroll repeat 0% 0%;*/
width: 200px;
height: 35px;
margin-left: 531px;
margin-bottom: 30px;
background:transparent;
border: white solid 1px;
border-radius: 5px;
color: white;
}
.password{
color: white;
width: 200px;
height: 35px;
margin-left: 531px;
margin-bottom: 30px;
background:transparent;
border: white solid 1px;
border-radius: 5px;
}
.button{
border: 1px solid;
border-radius: 5px;
background-color: rgba(255, 255, 255, 0.25);
color: white;
width: 210px;
height: 40px;
margin-left: 526px;
margin-bottom: 10px;
font-size: 16px;
}
.h1{
margin-bottom: 50px;
color: white;
margin-top: 110px;
}
.body{
background-image: url("2.png");
background-position: center,center;
background-repeat: no-repeat;
background-attachment: fixed;
background-size: cover;
}
</style>
</head>
<body class="body">
<h1 align="center" class="h1">注册</h1>
<form action="" method="post">
<input type="text" name="username" placeholder="请输入注册名" class="user"> <br>
<input type="password" name="password" placeholder="请输入密码" class="password">
<br>
<input type="submit" vlaue="提交" align="center" class="button">
</form>
</body>
</html>