您的位置:首页 > 其它

应用session对象实现登陆

2017-05-04 16:23 141 查看
<%@ 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")					//跳转到登陆也买页面
}
%>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: