<html>
??? <head>
??????? <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
??????? <title>JSP Page</title>
??? </head>
??? <body>
??? <h1>Login Checking Page</h1>
<%
?? String name=request.getParameter("name");
?? String password=request.getParameter("password");
?? //Connection the Database,loading
?? //int logCount=resultSet.getInt("count");
?? //java.util.Date lastLoginTime=resultSet.getDate("LastLoginTime");
?? //這里簡單設(shè)置logCount和lastLoginTime的值
?? UserSession user=new UserSeesion();
?? user.setUserId(name);
?? user.setIsLogin(true);
?? user.setLastLoginTime(new java.util.Date());
?? user.setLogCount(10);
?? session.setAttribute("userSession",user)
?? response.sendRedirect("welcome.jsp");
%>
??? </body>
</html>
整個登陸頁面的過程是這樣的:
(1)獲得用戶的登陸信息
(2)連接數(shù)據(jù)庫進(jìn)行權(quán)限驗(yàn)證
(3)如果通過驗(yàn)證,那么讀取用戶的注冊信息
(4)把用戶的注冊信息保存到一個userSession對象中
(5)把userSession對象保存到Session內(nèi)建對象中
(6)把視圖派發(fā)到下一個顯示頁面
注意:session.setAttribute("userSession",user)把userSession的一個對象設(shè)置到Session中,Session只能保存對象,不能保存原始的數(shù)據(jù)類型,比如:
session.setAttribute("count",10)
是非法的語句,如果要把值為10的整數(shù)保存到Session中,需要使用以下的方法:
session.setAttribute("count",new Integer(10));
然后在另一個頁面使用
(Integer)session.getAttribute("count");
把這個整數(shù)讀出來.
我們用如下方法在另一個頁面中把userSesseion對象讀取出來:
<%@page contentType="text/html;charset=gb2312" language="java"
?import="java.sql.*,dory.*" errorPage=""%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
?? "<html>
??? <head>
??????? <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
??????? <title>JSP Page</title>
??? </head>
??? <body>??
<%
?? UserSession user=(UserSession)session.getAttribute("userSession");
?? try
?? {
?????? if(user.isLogin())
?????? {
?????????? out.print("welcome,your login id is:"+user.getUserId());
?????????? out.print("your last login time is:"+user.getLastLoginTime());
?????????? out.print("now you are the:"+user.getLogCount()+"times logging this website");
?????? }
?????? else
?????? {
?????????? response.sendRedirect("login.html");
?????? }
?? }
?? catch(Exception e)
?? {
?????? response.sendRedirect("login.html");
?? }
%>
??? </body>
</html>
可以看出,通過UserSession user=(UserSession)session.getAttribute("userSession");代碼來讀取在前一個頁面中設(shè)置的對象,然后再從這個對象讀取一些相關(guān)值.當(dāng)然我們也可以用JavaBean的形式來讀取.
2.使用隱含菜單
這種方式通過隱含菜單的形式把數(shù)據(jù)傳遞到下一個頁面,它有兩個局限性:
.只能在相鄰的兩個頁面之間傳遞數(shù)據(jù)
.客戶端可以使用查看網(wǎng)頁源代碼的方式獲得表單中的數(shù)據(jù),安全性不好
它的實(shí)現(xiàn)很簡單:
<form action="target.jsp">
<input type="hidden" name="test" value="abc">
<input type="hidden" name="test2" value="def">
</form>
在另外一個頁面中,通過這樣來獲得數(shù)據(jù):
String test=request.getParameter("test");
String test2=request.getParameter("test2");
3.使用Cookie
和Session不同,Cookie是放在客戶端的,由于客戶考慮到安全應(yīng)素可能會禁用cookie,這樣在使用cookie就會遇到麻煩了.
b.在不同的用戶之間共享數(shù)據(jù)
在不同的在不同的用戶之間共享數(shù)據(jù)最常見的方法是使用ServletContext和application對象,通過在一個用戶那里設(shè)置屬性在另一個用戶那里獲得這個屬性.
1.使用ServletContext
在JSP頁面中可以通過getServletContext()方法獲得ServletContext對象.在這種情況下不同的用戶通過它來工享數(shù)據(jù),看下面的實(shí)現(xiàn)代碼:
<%@page contentType="text/html;charset=gb2312" language="java"
import="java.sql.*,javax.servlet.*,javax.servlet.http.*,dory.*" errorPage="" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
?? "<%
?? request.setCharacterEncoding("gb2312");
%>
<html>
??? <head>
??????? <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
??????? <title>JSP Page</title>
??? </head>
??? <body>
??? a simple chatting room
??? <br><hr><font color="red">
<%
?? String content=(String)getServletContext().getAttribute(new String("chatTopic_1"));
?? out.print(content);
?? getServletContext().setAttribute("chatTopic_1",content+(String)request.getParameter("content")
?? +"<br>");
%>
??? </font>
??? <hr>
??? <form accept="Servelt Context_chat.jsp">
??????? <input type="text" name="content">
??????? <input type="submit" value="speak">
??? </form>
??? </body>
</html>
2.application對象
application對象對應(yīng)于每個web應(yīng)用來說只有一個,它使用和ServletContext差不多.如下:
<%@page contentType="text/html;charset=gb2312" language="java"
import="java.sql.*,javax.servlet.*,javax.servlet.http.*,dory.*" errorPage="" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
?? "<%
?? request.setCharacterEncoding("gb2312");
%>
<html>
??? <head>
??????? <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
??????? <title>JSP Page</title>
??? </head>
??? <body>
??? a simple chatting room
??? <br><hr><font color="red">
<%
?? String content=(String)application.getAttribute(new String("chatTopic_1"));
?? out.print(content);
?? application.setAttribute("chatTopic_1",content+(String)request.getParameter("content")
?? +"<br>");
%>
??? </font>
??? <hr>
??? <form accept="Servelt Context_chat.jsp">
??????? <input type="text" name="content">
??????? <input type="submit" value="speak">
??? </form>
??? </body>
</html>
可以得到ServletContext和application的實(shí)現(xiàn)機(jī)制基本上一致.