在JSP開發(fā)中我們常常會(huì)碰到以下的一些問題,其實(shí)都很有代表性. <html> ??? <h1>Login Checking Page</h1>
在不同的頁面或者用戶之間共享數(shù)據(jù)
在JSP中共享數(shù)據(jù),大體上可以分為兩種情況,第一種是在同一個(gè)用戶的不同也面之間共享數(shù)據(jù),另一種是在不同用戶之間共享數(shù)據(jù).
對于同一個(gè)用戶的會(huì)話,要想在不同的頁面之間共享數(shù)據(jù),可以有以下幾種選擇:
.把數(shù)據(jù)保存在Session中(最常見的方法)
.通過Cookie
.通過隱含表單提交到下一個(gè)頁面
.通過ServletContext對象
.通過Application對象
.通過文件系統(tǒng)或者數(shù)據(jù)庫
要在不同的用戶之間共享數(shù)據(jù),通常的方法是:
.通過ServletContext對象
.通過Application對象
.通過文件系統(tǒng)或者數(shù)據(jù)庫
可見,對于不同用戶之間共享數(shù)據(jù)的實(shí)現(xiàn)方法在同一個(gè)用戶的不同也面之間也能實(shí)現(xiàn)數(shù)據(jù)共享.
a.在同一個(gè)用戶的不同也面之間共享數(shù)據(jù)
1.使用session共享數(shù)據(jù)
用戶在瀏覽網(wǎng)頁時(shí),由于HTTP協(xié)議是一種無狀態(tài)協(xié)議,往往在不同的頁面之間存在數(shù)據(jù)交換的問題,這就需要在這些不同的頁面之間共享數(shù)據(jù).在編程實(shí)現(xiàn)中我們常看到的方法是把共享數(shù)據(jù)保存在session中.這些共享數(shù)據(jù)可以是字符串或者與Java的原始數(shù)據(jù)類型相關(guān)的對象,也可以是一個(gè)Java對象.
exampl: 用戶登錄時(shí),如果驗(yàn)證成功,就把信息保存到一個(gè)userSession的類中,在其他的頁面可以讀取這個(gè)值.
userSession.java
package dory;
import java.util.Date;
/**
?*
?* @author Dory Doo
?*/
public class userSession {
??? private boolean isLogin=false;
??? private String userId;
??? private Date lastLoginTime;
??? private int logCount;
??? /** Creates a new instance of userSession */
??? public userSession() {
??? }
??? public void setIsLogin(boolean l)
??? {
??????? this.isLogin=l;
??? }
??? public void setUserId(String userId)
??? {
??????? this.userId=userId;
??? }
??? public void setLastLoginTime(Date l)
??? {
??????? this.lastLoginTime=l;
??? }
??? public void setLogCount(int logCount)
??? {
??????? this.logCount=logCount;
??? }
??? public boolean isLogin()
??? {
??????? return this.isLogin;
??? }
??? public String getUserId()
??? {
??????? return this.userId;
??? }
??? public Date getLastLoginTime()
??? {
??????? return this.lastLoginTime;
??? }
??? public int getLogCount()
??? {
??????? return this.logCount;
??? }
}
當(dāng)然這個(gè)就比較簡單的了,要的是整個(gè)思路.我們怎么來使用這個(gè)類,我們需要一個(gè)驗(yàn)證登陸的頁login.jsp
<%@page contentType="text/html;charset=gb2312" language="java"
?import="java.sql.*,dory.*" errorPage=""%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
?? "
??? <head>
??????? <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
??????? <title>JSP Page</title>
??? </head>
??? <body>
<%
?? 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>
整個(gè)登陸頁面的過程是這樣的:
(1)獲得用戶的登陸信息
(2)連接數(shù)據(jù)庫進(jìn)行權(quán)限驗(yàn)證
(3)如果通過驗(yàn)證,那么讀取用戶的注冊信息
(4)把用戶的注冊信息保存到一個(gè)userSession對象中
(5)把userSession對象保存到Session內(nèi)建對象中
(6)把視圖派發(fā)到下一個(gè)顯示頁面
注意:session.setAttribute("userSession",user)把userSession的一個(gè)對象設(shè)置到Session中,Session只能保存對象,不能保存原始的數(shù)據(jù)類型,比如:
session.setAttribute("count",10)
是非法的語句,如果要把值為10的整數(shù)保存到Session中,需要使用以下的方法:
session.setAttribute("count",new Integer(10));
然后在另一個(gè)頁面使用
(Integer)session.getAttribute("count");
把這個(gè)整數(shù)讀出來.
我們用如下方法在另一個(gè)頁面中把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");代碼來讀取在前一個(gè)頁面中設(shè)置的對象,然后再從這個(gè)對象讀取一些相關(guān)值.當(dāng)然我們也可以用JavaBean的形式來讀取.
2.使用隱含菜單
這種方式通過隱含菜單的形式把數(shù)據(jù)傳遞到下一個(gè)頁面,它有兩個(gè)局限性:
.只能在相鄰的兩個(gè)頁面之間傳遞數(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>
在另外一個(gè)頁面中,通過這樣來獲得數(shù)據(jù):
String test=request.getParameter("test");
String test2=request.getParameter("test2");
3.使用Cookie
和Session不同,Cookie是放在客戶端的,由于客戶考慮到安全應(yīng)素可能會(huì)禁用cookie,這樣在使用cookie就會(huì)遇到麻煩了.
b.在不同的用戶之間共享數(shù)據(jù)
在不同的在不同的用戶之間共享數(shù)據(jù)最常見的方法是使用ServletContext和application對象,通過在一個(gè)用戶那里設(shè)置屬性在另一個(gè)用戶那里獲得這個(gè)屬性.
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)于每個(gè)web應(yīng)用來說只有一個(gè),它使用和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ī)制基本上一致.