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

相关推荐
CharlesYu0113 小时前
前端性能优化的第一性原理,是不断缩短“用户发起意图 → 获得可用结果”之间的时间
前端
平头哥技术团队13 小时前
Day 21 _ 页内锚点_给每段起个 id,目录写 href=_#id_,点一下页面就滚到那一段
前端·html·html5
子兮曰14 小时前
Bun v1.4.1 深度解析:从 Zig 到 Rust,一场 11 天、64 个 AI 代理的语言迁徙
前端·后端·bun
人民广场吃泡面14 小时前
什么是AI Agent?它又能给前端带来哪些效率提升?
前端·人工智能
中科三方15 小时前
两家域名注册商资质被ICANN终止:企业域名资产安全再受关注
前端·网络·安全·域名
bug总结15 小时前
uniapp vue3全局方法注册使用
前端·javascript·uni-app
服务端相声演员15 小时前
in子查询的执行可能被优化为连接查询
mysql
华无丽言16 小时前
如何在宜搭中实现获取子表中的字段值赋值到父表中?
前端·javascript·低代码
IT_陈寒16 小时前
Vue的computed属性竟然坑了我一把
前端·人工智能·后端
威斯软科的老司机16 小时前
通俗讲解 CNN 图像识别、向量 Embedding、Softmax 概率计算这三块的简化原理
前端·人工智能·ui·数字孪生