国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

JSP實現數據保存(web基礎學習筆記四)

2019-11-14 22:38:04
字體:
來源:轉載
供稿:網友
jsp實現數據保存(web基礎學習筆記四)session對象:

//服務端設置Session屬性session.setAttribute("user", username);//客戶端接收SessionObject obj = session.getAttribute("user");    //判斷是否為空,不是空就輸出    if(obj==null){        //如果為空就提示用戶登錄        %>                   <%}else{            //不為空就輸出用戶名,和歡迎信息            out.

Session清理機制

在服務端設置過期時間
//設置服務器端session過期日期(單位為:秒),如設置為10秒       session.setMaxInactiveInterval(10);
在客戶端中設置
<%  //在客戶端設置session,如點擊注銷之后,直接設置session過期  //第一種刪除session中的數據session.removeAttribute("user");//第二種 或者直接使用session過期 session.invalidate();//以2選一  //重定向主頁  response.sendRedirect("index.jsp");%>
在tomcat中直接設置,在tomact中設置時間為分鐘conf/web.
xml
<!--在最下方的</webapp>之前添加,并設置為10分種--><session-config>  <session-timeout>10</session-timeout>  </session-config></web-app>
Session過程

cookie

在服務端設置Cookie
//聲明cookie變量,并添加要保存的參數和值如:用戶名       Cookie cookie = new Cookie("user",username);       //設置cookie的有效時間以秒為單位60秒*60秒int類型的值       cookie.setMaxAge(60*60);       //將cookies對象發回客戶端       response.addCookie(cookie);

在客戶端接收cookie,

//接收cookies返回值為cookies的數組       Cookie [] cookies = request.getCookies();        //聲明字符串變量用來接收cookies的值        String user="";        for(int i=0;i<cookies.length;i++){            //獲取cookies的名字,并判斷如果是服務端的名稱            if(cookies[i].getName().equals("user")){                //將cookes的值賦為字符串變量                user=cookies[i].getValue();            }                    }
application對象

計數器的實現原理
<%  //計數器//取出application屬性中的count值為object值 Object count = application.getAttribute("count");//判斷是否為空if(count==null){    //如果是空,表示第一次訪問將值設置為1    application.setAttribute("count", new Integer(1));}else{    //不是空,則將結果+1    Integer i=(Integer)count;    application.setAttribute("count", i.intValue()+1);}Integer iCount=(Integer)application.getAttribute("count");out.println("訪問:&nbsp;"+iCount+"次");%>
<%//獲取application對象的count屬性值 Object count = application.getAttribute("count");//判斷是否為空if(count==null){    //為空就設置為1    application.setAttribute("count", new Integer(1));    }else{    //不空就取值+1    application.setAttribute("count", (Integer)count+1);} Integer icount = (Integer)application.getAttribute("count"); out.println("訪問了: "+icount+"次");%>

Request、Session和Application的區別

Request:中存儲的數據僅在一個請求中可用

Session:中存儲的數據在一個會話有效期內可用

Application:中存儲的數據在整個WEB項目中可用,直到WEB服務器停止運行


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 巴里| 沽源县| 施甸县| 廉江市| 定南县| 鄄城县| 葵青区| 屯留县| 永平县| 普安县| 称多县| 太保市| 定边县| 姜堰市| 富阳市| 漯河市| 惠州市| 长海县| 金华市| 正定县| 资阳市| 乐清市| 大英县| 法库县| 嵩明县| 缙云县| 皋兰县| 潼关县| 拉孜县| 鞍山市| 杂多县| 甘洛县| 卢湾区| 公主岭市| 曲靖市| 奉贤区| 黄龙县| 宁强县| 明溪县| 霍林郭勒市| 司法|