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

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

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

    BloveSaga

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

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

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

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

    在JSP中共享數(shù)據(jù),大體上可以分為兩種情況,第一種是在同一個用戶的不同也面之間共享數(shù)據(jù),另一種是在不同用戶之間共享數(shù)據(jù).
    對于同一個用戶的會話,要想在不同的頁面之間共享數(shù)據(jù),可以有以下幾種選擇:
    .把數(shù)據(jù)保存在Session中(最常見的方法)
    .通過Cookie
    .通過隱含表單提交到下一個頁面
    .通過ServletContext對象
    .通過Application對象
    .通過文件系統(tǒng)或者數(shù)據(jù)庫
    要在不同的用戶之間共享數(shù)據(jù),通常的方法是:
    .通過ServletContext對象
    .通過Application對象
    .通過文件系統(tǒng)或者數(shù)據(jù)庫
    可見,對于不同用戶之間共享數(shù)據(jù)的實(shí)現(xiàn)方法在同一個用戶的不同也面之間也能實(shí)現(xiàn)數(shù)據(jù)共享.
    a.在同一個用戶的不同也面之間共享數(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)的對象,也可以是一個Java對象.
    exampl: 用戶登錄時(shí),如果驗(yàn)證成功,就把信息保存到一個userSession的類中,在其他的頁面可以讀取這個值.
    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)然這個就比較簡單的了,要的是整個思路.我們怎么來使用這個類,我們需要一個驗(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");
    ?? //這里簡單設(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ī)制基本上一致.


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


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 亚洲精品中文字幕麻豆 | 免费播放一区二区三区| 国产偷窥女洗浴在线观看亚洲| 亚洲AV无码一区二区三区网址| 成年免费大片黄在线观看岛国| 亚洲国产视频一区| 妞干网手机免费视频| 亚洲日韩国产二区无码| 精品国产免费一区二区| 亚洲av日韩精品久久久久久a| 成年人免费观看视频网站| 亚洲日日做天天做日日谢| 成人免费午夜视频| 美女视频黄频a免费| 国产成人亚洲综合| 久久免费精彩视频| 亚洲不卡1卡2卡三卡2021麻豆| 亚洲中文无码永久免费| 国产亚洲精品美女久久久久 | 国产精品观看在线亚洲人成网| 免费女人18毛片a级毛片视频| 一级全免费视频播放| 久久精品亚洲视频| 男男AV纯肉无码免费播放无码| 国产AV无码专区亚洲AV麻豆丫| 亚洲国产精品专区在线观看| 中文字幕久精品免费视频| 亚洲一区二区三区精品视频| 四虎免费永久在线播放| 99在线免费观看| 亚洲精品**中文毛片| 日韩成人免费视频播放| 国产精品黄页免费高清在线观看| 亚洲午夜在线电影| 日韩免费a级在线观看| 中文字幕乱码免费看电影| 精品亚洲成A人无码成A在线观看| 亚洲成a人片在线观看老师| 无码人妻久久一区二区三区免费 | 99精品视频在线视频免费观看| 99热亚洲色精品国产88|