利用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进行登录判断了

相关推荐
斯普信专业组几秒前
构建基于MCP的MySQL智能运维平台:从开源服务端到交互式AI助手
运维·mysql·开源·mcp
hedley(●'◡'●)23 分钟前
基于cesium和vue的大疆司空模仿程序
前端·javascript·vue.js·python·typescript·无人机
qq5_81151751525 分钟前
web城乡居民基本医疗信息管理系统信息管理系统源码-SpringBoot后端+Vue前端+MySQL【可直接运行】
前端·vue.js·spring boot
百思可瑞教育26 分钟前
构建自己的Vue UI组件库:从设计到发布
前端·javascript·vue.js·ui·百思可瑞教育·北京百思教育
百锦再26 分钟前
Vue高阶知识:利用 defineModel 特性开发搜索组件组合
前端·vue.js·学习·flutter·typescript·前端框架
Exquisite.1 小时前
Mysql
数据库·mysql
CappuccinoRose1 小时前
JavaScript 学习文档(二)
前端·javascript·学习·数据类型·运算符·箭头函数·变量声明
这儿有一堆花1 小时前
Vue 是什么:一套为「真实业务」而生的前端框架
前端·vue.js·前端框架
全栈前端老曹1 小时前
【MongoDB】深入研究副本集与高可用性——Replica Set 架构、故障转移、读写分离
前端·javascript·数据库·mongodb·架构·nosql·副本集
NCDS程序员2 小时前
v-model: /v-model/ :(v-bind)三者核心区别
前端·javascript·vue.js