index.jsp
<body>
<form action="deal.jsp" name="form1" method="post">
username:<input name="username" type="text" id="name" style="width:120px"><br>
password:<input name="pwd" type="password" id="pwd" style="width:120px"><br>
<br>
<input type="submit" name="Submit" value="Submit"/>
</form>
</body>
deal.jsp
<body>
<%
String[][] userList={{"ly","123"},{"tz","456"}};//保存用戶列表
boolean flag=false;//登陸狀態(tài)
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
String pwd=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){
session.setAttribute("username", username);//保存用戶名到session范圍的變量中
response.sendRedirect("main.jsp");//跳轉(zhuǎn)到主頁
}else{
response.sendRedirect("index.jsp");//跳轉(zhuǎn)到登錄頁面
}
%>
</body>
main.jsp
<body>
<%String username=(String)session.getAttribute("username"); %><!-- 獲取保存在session范圍在內(nèi)的用戶名 -->
您好![<%=username %>],歡迎訪問!<br>
<a href="exit.jsp">[退出]</a>
</body>
exit.jsp
<body>
<%
session.invalidate();//銷毀session
response.sendRedirect("index.jsp");//重定向頁面
%>
</body>
21.PNG
22.PNG
23.PNG