利用session在html和MySQL实现登录

首先先创建一个登录页面(login.html)

html 复制代码
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="js/jquery-3.7.1.min.js"></script>
<script src="js/jquery.cookie.min.js"></script>
<script src="js/login.js" defer></script>
</head>
<body>

	<div>
		账号:<input type='text' class='account'><br>
		密码:<input type='text' class='password'><br>
		<input type='button' value='登录' class='btn'> 
	</div>
</body>
</html>

并且在相对应的位置上创建js文件

javascript 复制代码
$(".btn").on("click",function(){
	var account = $(".account").val().trim()
	var password = $(".password").val().trim()
	$.ajax({
		url:"Studentlogin",
		type:"get",
		data:{
			account,
			password,
			/*captcha*/
		},
		success:function(value){
			alert(value)
			if(value=="登录成功"){
				location.href="student.html"
			}
		}
	})
})

if(value=="登录成功"){

location.href="student.html"

}判断登录成功之后跳转的页面

在创建一个相应的servlet文件(Studentlogin.java)

java 复制代码
package qcby.servlet;

import java.io.IOException;
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 qcby.db.MysqlUtil;

/**
 * Servlet implementation class Login
 */
@WebServlet("/Studentlogin")
public class Studentlogin extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Studentlogin() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String account = request.getParameter("account");
		String password = request.getParameter("password");
		String captcha = request.getParameter("captcha");
		HttpSession session = request.getSession();
		String captchaVal = (String)session.getAttribute("captchaVal");
		String res = "验证码错误";
		if(captcha.equals(captchaVal)) {
			String sql = "select count(*) from admin where account=\""+account+"\" and password=\""+password+"\"";
			int num = MysqlUtil.getCount(sql);
			res = "登录失败";
			if(num>0) {
				res = "登录成功";
				//设置登录状态
				session.setAttribute("account", account);
			}
		}
		request.setCharacterEncoding("utf-8");
	    response.setCharacterEncoding("utf-8");
	    response.getWriter().write(res);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

这样就能够利用session进行登录判断了

相关推荐
NutShell Wang1 分钟前
Vite 8.1 深度拆解:Rolldown 统一打包器如何终结前端构建的「双引擎时代」
前端·rust·开源·vite·开发者工具·vibe coding
朱容zr3331332 分钟前
请解释“回表”的概念。
java·前端·数据库
软件技术NINI9 分钟前
盗墓笔记html+css+js 2页
css·笔记·html
leslie11823 分钟前
webpack笔记
前端·webpack
宁风NF28 分钟前
JavaScript:内存、垃圾回收、性能优化
开发语言·前端·javascript·学习·性能优化·es6
勾勾圈圈蛋蛋30 分钟前
DOM、BOM、DOM操作详解 + 结合Vue响应式关联梳理
前端
brave_zhao1 小时前
mysql的安装
数据库·mysql
ShuiShenHuoLe1 小时前
Go html/template 使用入门
开发语言·golang·html
打妖妖灵滴哪吒1 小时前
IOS模态窗口的作用与设计原则
前端·ui
冷凝娇1 小时前
【MySQL】2026总结(一)
数据库·mysql