<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    BloveSaga

    在希臘帕爾納斯山南坡上,有一個(gè)馳名世界的戴爾波伊神托所,在它的入口處的巨石上赫然銹刻著這樣幾個(gè)大字: 認(rèn)識(shí)你自己!

      BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
      34 隨筆 :: 12 文章 :: 122 評(píng)論 :: 0 Trackbacks

    在JSP開發(fā)中我們常常會(huì)碰到以下的一些問題,其實(shí)都很有代表性.

    在不同的頁面或者用戶之間共享數(shù)據(jù)

    在JSP中共享數(shù)據(jù),大體上可以分為兩種情況,第一種是在同一個(gè)用戶的不同也面之間共享數(shù)據(jù),另一種是在不同用戶之間共享數(shù)據(jù).
    對(duì)于同一個(gè)用戶的會(huì)話,要想在不同的頁面之間共享數(shù)據(jù),可以有以下幾種選擇:
    .把數(shù)據(jù)保存在Session中(最常見的方法)
    .通過Cookie
    .通過隱含表單提交到下一個(gè)頁面
    .通過ServletContext對(duì)象
    .通過Application對(duì)象
    .通過文件系統(tǒng)或者數(shù)據(jù)庫
    要在不同的用戶之間共享數(shù)據(jù),通常的方法是:
    .通過ServletContext對(duì)象
    .通過Application對(duì)象
    .通過文件系統(tǒng)或者數(shù)據(jù)庫
    可見,對(duì)于不同用戶之間共享數(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)的對(duì)象,也可以是一個(gè)Java對(duì)象.
    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è)就比較簡(jiǎn)單的了,要的是整個(gè)思路.我們?cè)趺磥硎褂眠@個(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"
    ?? "

    <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");
    ?? //這里簡(jiǎn)單設(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)證,那么讀取用戶的注冊(cè)信息
    (4)把用戶的注冊(cè)信息保存到一個(gè)userSession對(duì)象中
    (5)把userSession對(duì)象保存到Session內(nèi)建對(duì)象中
    (6)把視圖派發(fā)到下一個(gè)顯示頁面
    注意:session.setAttribute("userSession",user)把userSession的一個(gè)對(duì)象設(shè)置到Session中,Session只能保存對(duì)象,不能保存原始的數(shù)據(jù)類型,比如:
    session.setAttribute("count",10)
    是非法的語句,如果要把值為10的整數(shù)保存到Session中,需要使用以下的方法:
    session.setAttribute("count",new Integer(10));
    然后在另一個(gè)頁面使用
    (Integer)session.getAttribute("count");
    把這個(gè)整數(shù)讀出來.
    我們用如下方法在另一個(gè)頁面中把userSesseion對(duì)象讀取出來:
    <
    %@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è)置的對(duì)象,然后再從這個(gè)對(duì)象讀取一些相關(guān)值.當(dāng)然我們也可以用JavaBean的形式來讀取.

    2.使用隱含菜單
    這種方式通過隱含菜單的形式把數(shù)據(jù)傳遞到下一個(gè)頁面,它有兩個(gè)局限性:
    .只能在相鄰的兩個(gè)頁面之間傳遞數(shù)據(jù)
    .客戶端可以使用查看網(wǎng)頁源代碼的方式獲得表單中的數(shù)據(jù),安全性不好
    它的實(shí)現(xiàn)很簡(jiǎ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對(duì)象,通過在一個(gè)用戶那里設(shè)置屬性在另一個(gè)用戶那里獲得這個(gè)屬性.

    1.使用ServletContext
    在JSP頁面中可以通過getServletContext()方法獲得ServletContext對(duì)象.在這種情況下不同的用戶通過它來工享數(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對(duì)象
    application對(duì)象對(duì)應(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ī)制基本上一致.


    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 午夜电影免费观看| 国产亚洲美女精品久久久| 极品美女一级毛片免费| 亚洲国产另类久久久精品小说| 3d成人免费动漫在线观看| 亚洲国产精品嫩草影院| 中文字幕精品亚洲无线码一区应用| 91人成网站色www免费下载| 亚洲变态另类一区二区三区| 精品亚洲综合久久中文字幕| 最近中文字幕mv免费高清视频7| 国产高清视频免费在线观看| 亚洲人成在线精品| 亚洲色成人WWW永久网站| 丁香花在线观看免费观看| 中文字幕不卡免费高清视频| 亚洲 欧洲 自拍 另类 校园| 亚洲中文字幕无码久久综合网| 免费一本色道久久一区| 97在线免费视频| 亚洲AV无码专区国产乱码不卡 | 亚洲国产成人久久笫一页| 99爱视频99爱在线观看免费| 国产AV日韩A∨亚洲AV电影| 久久久久久久亚洲Av无码 | 亚洲乱亚洲乱淫久久| 免费国产怡红院在线观看| 精品国产免费人成电影在线观看| 特色特黄a毛片高清免费观看| 亚洲人成综合在线播放| 亚洲AV综合色区无码另类小说| 国产精品视频免费一区二区三区| 最刺激黄a大片免费网站| 精品无码一级毛片免费视频观看 | 24小时免费看片| 两个人看的www免费视频中文| 国产亚洲女在线线精品| 久久亚洲国产最新网站| 亚洲精品网站在线观看你懂的| 国产中文在线亚洲精品官网| 免费一级毛片在线播放|