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

相关推荐
cui_ruicheng26 分钟前
MySQL(四):数据类型与字段设计
数据库·mysql
Arvin.Angela3 小时前
HTML5语义化标签深度解析:div、section与article的底层实现原理
html
原则猫3 小时前
HOOKS 背后机制
前端
码语智行3 小时前
首页导航跳转功能深度解析-系统内和系统外
前端
DIY源码阁3 小时前
JavaSwing航班订票管理系统 - MySQL版
数据库·mysql
阿猫的故乡4 小时前
Vue过渡动画从入门到装X:淡入淡出、滑动、列表动画、第三方库全搞定
前端·javascript·vue.js
IManiy4 小时前
总结之Vibe Coding前端骨架
前端
JS菌4 小时前
AI Agent 沙箱双层防护体系:从权限过滤到内核隔离的完整实现
前端·人工智能·后端
Aphasia3114 小时前
从输入URL到页面展示全流程
前端·面试
我叫黑大帅5 小时前
前端如何竖屏固定视口背景
前端·javascript·面试