应用session对象实现登陆

xiaoxiao2021-02-27  349

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <form name="form1" method="post" action=""> 用户名:<input name="name" type="text" id="name" style="width: 120px"><br> 密码:<input name="pwd" type="password" id="pwd" style="width: 120px"> <br> <input type="submit" name="Submit" value="提交"> </body> <% String[][] userList={{"bjh",123},{"zsf",234},{"thd",345}};//创建二维数组 boolean flag=false; //登陆状态 request.setCharacterEncoding("UTF-8"); //设置编码 String username=request.getParameter("username"); //获取用户名 String password=request.getParameter("pwd") //获取密码 for(int i=0;i<userList.length;i++){ if(userList[i][0].equals(username)){ //遍历二维数组 if(userList[i][1].equals(pwd)){ flag=true; break; // 跳出循环 } } } if(flag){ //如果值为true ,表示登陆成功 session.setAttribute("username",username); //保存用户到session中 response.sendRedirect("main.jsp"); //跳转到main.jsp }else{ response.sendRedirect("index.jsp") //跳转到登陆也买页面 } %>
转载请注明原文地址: https://www.6miu.com/read-5041.html

最新回复(0)