A simple example of a Session object:
1, Login interface: Use a simple HTML form submission interface.
<%@ page language= "java" contenttype= "text/html; charset=gb18030 "
pageencoding= "GB18030"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; charset=gb18030 ">
<title>insert title here</title>
<body>
<form action= "do_long.jsp" method= "POST" >
User name: <input type= "text" name= "Usename"/><br><br>
Password: <input type= "password" name= "password"/><br><br>
<input type= "Submit" value= "Submission"/>
<input type= "reset" value= "reset"/>
</form>
</body>
2, Request object to obtain the user name and password, determine whether it is empty, use the session setattribute store user name information. Jump to the Welcome screen.
<%@ page language= "java" contenttype= "text/html; charset=gb18030 "
pageencoding= "GB18030"%>
<%
String usename=request.getparameter ("Usename");
String password=request.getparameter ("password");
if (usename!=null&&password!=null) {
Session.setattribute ("Usename", usename);
Response.setheader ("Refresh", "2; Url=welcome.jsp ");
}
else{
%>
User name, password cannot be empty. <a href= "long.jsp" > Login </a>
<%}%>
3. Welcome interface
<%@ page language= "java" contenttype= "text/html; charset=gb18030 "
pageencoding= "GB18030"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; charset=gb18030 ">
<title>insert title here</title>
<body>
<%if (Session.getattribute ("Usename")!=null) {%>
Welcome: <%=session.getattribute ("Usename")%>
<a href= "longout.jsp" > Logout </a><br>
<%}else {%>
Please login first
<a href= "long.jsp" > Login </a><br>
<%}%>
<%if (Session.isnew ()) {%>
Welcome New User
<%}else{%>
Welcome to the old user
<%}%>
</body>
4. Exit the interface
<%@ page language= "java" contenttype= "text/html; charset=gb18030 "
pageencoding= "GB18030"%>
<%
Session.invalidate ();
Response.setheader ("Refresh", "2; Url=welcome.jsp ");
%>
A simple instance of the Session object