对于每个用户,都会有一个特定的会话,在这里我们要验证用户的详细信息并在会话中设置用户,最后使用户无效。
在这里,我们从用户那里获取输入,并在提交表单时触发validate.jsp操作。
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Session In JSP</title>
<style>
h1{
background-color:PINK;
width:50%;
}
h2{
text-decoration: underline;
}
td{
padding:15px;
width:50%;
}
legend{
background-color:green;
}
fieldset{
width:30%;
height:70%;
text-align:center;
}
</style>
</head>
<body>
<h1>Session in JSP</h1>
<h2>USER LOGIN SESSION</h2>
<fieldset>
<legend><font color="white"><b>Login Here:</b> </font></legend>
<form action="validate.jsp" method="post"> <!-- Here we are taking the values from user and triggering the validate.jsp file -->
<table>
<tr><td>USER NAME</td><td><input type="text" name="username"></td></tr>
<tr><td>PASS WORD</td><td><input type="password" name="password"></td></tr>
<tr><td></td><td><button type="submit">LOGIN</button></td></tr>
</table>
</form>
</fieldset>
</body>
</html>在这里,我们通过获取输入值并将其发送到相应的页面来验证特定用户。
<%@ 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>Validate</title>
</head>
<body><!-- values given at login page are taken here and checks if the valid details are not -->
<%
String username=request.getParameter("username");
String password=request.getParameter("password");
if(username.equals("includehelp") && password.equals("includehelp")){
//如果用户有效,则执行此块,并在一个会话中保留用户
session.setAttribute("user", username);//这是如何在会话中声明用户
response.sendRedirect("logged.jsp"); //并且我们已重定向到登录页面
}else{
//如果再没有授权用户,则将其重新定向到相同的登录页面
response.sendRedirect("index.jsp");
}
%>
</body>
</html>