Java筆試面試

aoxiangguanjun發表於2013-04-25

1、棧與堆都是Java用來在Ram中存放資料的地方。與C++不同,Java自動管理棧和堆,程式設計師不能直接地設定棧或堆。

Java的堆是一個執行時資料區,類的物件從中分配空間。這些物件通過new、newarray、anewarray和multianewarray等指令建立,它們不需要程式程式碼來顯式的釋放。堆是由垃圾回收來負責的,堆的優勢是可以動態地分配記憶體大小,生存期也不必事先告訴編譯器,因為它是在執行時動態分配記憶體的,Java的垃圾收集器會自動收走這些不再使用的資料。但缺點是,由於要在執行時動態分配記憶體,存取速度較慢。

棧的優勢是,存取速度比堆要快,僅次於暫存器,棧資料可以共享。但缺點是,存在棧中的資料大小與生存期必須是確定的,缺乏靈活性。棧中主要存放一些基本型別的變數(,int, short, long, byte, float, double, boolean, char)和物件控制程式碼。

棧有一個很重要的特殊性,就是存在棧中的資料可以共享。假設我們同時定義:

int a = 3;
int b = 3; 

編譯器先處理int a = 3;首先它會在棧中建立一個變數為a的引用,然後查詢棧中是否有3這個值,如果沒找到,就將3存放進來,然後將a指向3。接著處理int b = 3;在建立完b的引用變數後,因為在棧中已經有3這個值,便將b直接指向3。這樣,就出現了a與b同時均指向3的情況。

這時,如果再令a=4;那麼編譯器會重新搜尋棧中是否有4值,如果沒有,則將4存放進來,並令a指向4;如果已經有了,則直接將a指向這個地址。因此a值的改變不會影響到b的值。

要注意這種資料的共享與兩個物件的引用同時指向一個物件的這種共享是不同的,因為這種情況a的修改並不會影響到b, 它是由編譯器完成的,它有利於節省空間。而一個物件引用變數修改了這個物件的內部狀態,會影響到另一個物件引用變數。

String是一個特殊的包裝類資料。可以用:

String str = new String("abc");
String str = "abc"; 

兩種的形式來建立,第一種是用new()來新建物件的,它會在存放於堆中。每呼叫一次就會建立一個新的物件。而第二種是先在棧中建立一個對String類的物件引用變數str,然後查詢棧中有沒有存放"abc",如果沒有,則將"abc"存放進棧,並令str指向”abc”,如果已經有”abc” 則直接令str指向“abc”。
比較類裡面的數值是否相等時,用equals()方法;當測試兩個包裝類的引用是否指向同一個物件時,用==,下面用例子說明上面的理論。

String str1 = "abc";
String str2 = "abc";
System.out.println(str1==str2); //true 

可以看出str1和str2是指向同一個物件的。

String str1 =new String ("abc");
String str2 =new String ("abc");
System.out.println(str1==str2); // false 

用new的方式是生成不同的物件。每一次生成一個。

因此用第二種方式建立多個”abc”字串,在記憶體中其實只存在一個物件而已. 這種寫法有利與節省記憶體空間. 同時它可以在一定程度上提高程式的執行速度,因為JVM會自動根據棧中資料的實際情況來決定是否有必要建立新物件。而對於String str = new String("abc");的程式碼,則一概在堆中建立新物件,而不管其字串值是否相等,是否有必要建立新物件,從而加重了程式的負擔。

另一方面, 要注意: 我們在使用諸如String str = "abc";的格式定義類時,總是想當然地認為,建立了String類的物件str。擔心陷阱!物件可能並沒有被建立!而可能只是指向一個先前已經建立的物件。只有通過new()方法才能保證每次都建立一個新的物件。

由於String類的immutable性質,當String變數需要經常變換其值時,應該考慮使用StringBuffer類,以提高程式效率。

 

 

2、JAVA中StringBuffer與StringBuilder、String的區別是什麼?

String 是字串類 最常用於簡單的字串操作

StringBuffer 是字串緩衝。 適用於複雜的字串增刪改操作。
StringBuilder 是與 StringBuffer 相容的 API 簡化。 該類被設計用作 StringBuffer 的一個簡易替換,用在字串緩衝區被單個執行緒使用的時候(這種情況很普遍)。
String每個新的變數都會分配新的空間,拼接字串的時候不建議使用。
StringBuffer 用於拼接字串,支援append、insert方法,記憶體空間會自己擴充套件,不需要額外分配,效率較高,執行緒安全。
StringBuilder 和StringBuffer類似,區別是他執行緒不安全,所以單執行緒情況下它效率更高。若在多執行緒環境下,請使用StringBuffer。

 

3、GC是java的垃圾回收

幾種實現的方法

 <wbr><wbr>1、 引用計數法(Reference Counting Collector)<wbr><wbr><br><br>   引用計數法是唯一沒有使用根集的垃圾回收的法,該演算法使用引用計數器來區分存活物件和不再使用的物件。一般來說,堆中的每個物件對應一個引用計數器。當每一次建立一個物件並賦給一個變數時,引用計數器置為1。當物件被賦給任意變數時,引用計數器每次加1當物件出了作用域後(該物件丟棄不再使用),引用計數器減1,一旦引用計數器為0,物件就滿足了垃圾收集的條件。<wbr><wbr><br><br>   基於引用計數器的垃圾收集器執行較快,不會長時間中斷程式執行,適宜地必須實時執行的程式。但引用計數器增加了程式執行的開銷,因為每次物件賦給新的變數,計數器加1,而每次現有物件出了作用域生,計數器減1。<wbr><wbr><br><br>   2、tracing演算法(Tracing Collector)<wbr><wbr><br><br>   tracing演算法是為了解決引用計數法的問題而提出,它使用了根集的概念。基於tracing演算法的垃圾收集器從根集開始掃描,識別出哪些物件可達,哪些物件不可達,並用某種方式標記可達物件,例如對每個可達物件設定一個或多個位。在掃描識別過程中,基於tracing演算法的垃圾收集也稱為標記和清除(mark-and-sweep)垃圾收集器.<wbr><wbr><br><br>   3、compacting演算法(Compacting Collector)<wbr><wbr><br><br>   為了解決堆碎片問題,基於tracing的垃圾回收吸收了Compacting演算法的思想,在清除的過程中,演算法將所有的物件移到堆的一端,堆的另一端就變成了一個相鄰的空閒記憶體區,收集器會對它移動的所有物件的所有引用進行更新,使得這些引用在新的位置能識別原來的物件。在基於Compacting演算法的收集器的實現中,一般增加控制程式碼和控制程式碼表。<wbr><wbr><br><br>   4、copying演算法(Coping Collector)<wbr><wbr><br><br>   該演算法的提出是為了克服控制程式碼的開銷和解決堆碎片的垃圾回收。它開始時把堆分成 一個物件面和多個空閒面,程式從物件面為物件分配空間,當物件滿了,基於coping演算法的垃圾收集就從根集中掃描活動物件,並將每個活動物件複製到空閒面(使得活動物件所佔的記憶體之間沒有空閒洞),這樣空閒面變成了物件面,原來的物件面變成了空閒面,程式會在新的物件面中分配記憶體。<wbr><wbr><br><br>   一種典型的基於coping演算法的垃圾回收是stop-and-copy演算法,它將堆分成物件面和空閒區域面,在物件面與空閒區域面的切換過程中,程式暫停執行。<wbr><wbr><br><br>   5、generation演算法(Generational Collector)<wbr><wbr><br><br>   stop-and-copy垃圾收集器的一個缺陷是收集器必須複製所有的活動物件,這增加了程式等待時間,這是coping演算法低效的原因。在程式設計中有這樣的規律:多數物件存在的時間比較短,少數的存在時間比較長。因此,generation演算法將堆分成兩個或多個,每個子堆作為物件的一代(generation)。由於多數物件存在的時間比較短,隨著程式丟棄不使用的物件,垃圾收集器將從最年輕的子堆中收集這些物件。在分代式的垃圾收集器執行後,上次執行存活下來的物件移到下一最高代的子堆中,由於老一代的子堆不會經常被回收,因而節省了時間。<wbr><wbr><br><br>   6、adaptive演算法(Adaptive Collector)<wbr><wbr><br><br>   在特定的情況下,一些垃圾收集演算法會優於其它演算法。基於Adaptive演算法的垃圾收集器就是監控當前堆的使用情況,並將選擇適當演算法的垃圾收集器。<wbr><wbr><br> 透視Java垃圾回收<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

 

4.簡述 Java Server Page 和 Servlet 的聯絡和區別。

Servlet是一種在伺服器端執行的Java程式,從某種意義上說,它就是伺服器端的Applet。所以Servlet可以像Applet一樣作為一種 外掛(Plugin)嵌入到Web Server中去,提供諸如HTTP、FTP等協議服務甚至使用者自已定製的協議服務。而JSP是繼Servlet後Sun公司推出的新技術,它是以 Servlet為基礎開發的,Servlet與JSP區別:

(1)程式設計方式不同 JSP是為了解決Servlet中相對困難的程式設計技術而開發的技術,因此,JSP在程式的編寫方面比Servlet要容易的多,Servlet嚴格遵循Java語言的程式設計標準,而JSP則遵循指令碼語言的編制標準。

(2)Servlet必須在編譯以後才能執行 JSP並不需要另外進行編譯,JSP Container會自動完成這一工作,而Servlet在每次修改程式碼之後都需要編譯完才能執行。

(3)執行速度不同

由於JSP Container將JSP程式編譯成Servlet的時候需要一些時間,所以JSP的執行速度比Servlet要慢一些,不過,如果JSP檔案能毫無變 化的重複使用,它在第一次以後的呼叫中執行速度就會和Servlet一樣了,這是因為JSP Container接到請求以後會確認傳遞過來的JSP是否有改動,如果沒有改動的話,將直接呼叫JSP編譯過的Servlet類,並提供給客戶端解釋執 行,如果JSP檔案有所改變,JSP Container將重新將它編譯成Servlet,然後再提交給客戶端

 

5、Collection 和 Collections的區別。

 

Collections是個java.util下的類,它包含有各種有關集合操作的靜態方法。 
Collection是個java.util下的介面,它是各種集合結構的父介面。 
List, Set, Map是否繼承自Collection介面? List,Set是  Map不是
ArrayList和Vector的區別。 
一.同步性:Vector是執行緒安全的,也就是說是同步的,而ArrayList是執行緒序不安全的,不是同步的 
二.資料增長:當需要增長時,Vector預設增長為原來一培,而ArrayList卻是原來的一半
HashMap和Hashtable的區別 
一.歷史原因:Hashtable是基於陳舊的Dictionary類的,HashMap是Java 1.2引進的Map介面的一個實現 
二.同步性:Hashtable是執行緒安全的,也就是說是同步的,而HashMap是執行緒序不安全的,不是同步的 
三.值:只有HashMap可以讓你將空值作為一個表的條目的key或value 

 

6、abstract class和interface有什麼區別?

 

宣告方法的存在而不去實現它的類被叫做抽象類(abstract class),它用於要建立一個體現某些基本行為的類,併為該類宣告方法,但不能在該類中實現該類的情況。不能建立abstract 類的例項。然而可以建立一個變數,其型別是一個抽象類,並讓它指向具體子類的一個例項。不能有抽象建構函式或抽象靜態方法。Abstract 類的子類為它們父類中的所有抽象方法提供實現,否則它們也是抽象類為。取而代之,在子類中實現該方法。知道其行為的其它類可以在類中實現這些方法。
介面(interface)是抽象類的變體。在介面中,所有方法都是抽象的。多繼承性可通過實現這樣的介面而獲得。介面中的所有方法都是抽象的,沒有一個有程式體。介面只可以定義static final成員變數。介面的實現與子類相似,除了該實現類不能從介面定義中繼承行為。當類實現特殊介面時,它定義(即將程式體給予)所有這種介面的方法。然後,它可以在實現了該介面的類的任何物件上呼叫介面的方法。由於有抽象類,它允許使用介面名作為引用變數的型別。通常的動態聯編將生效。引用可以轉換到介面型別或從介面型別轉換,instanceof 運算子可以用來決定某物件的類是否實現了介面。
抽象類:
 |-由抽象方法和常量、變數、全域性常量、構造方法、普通方法組成
 |-使用abstract宣告
 |-子類要通過extends繼承抽象類,子類如果不是抽象類,則必須覆寫抽象類的全部抽象方法
 |-存在單繼承的侷限
 |-抽象類可以實現若干個介面
介面:
 |-由抽象方法和全域性常量組成
 |-使用interface關鍵字
 |-子類要通過implements實現介面,子類如果不是抽象類,則必須覆寫抽象類的全部抽象方法
 |-一個子類可以實現多個介面
 |-介面不能繼承一個抽象類,但允許繼承多個介面

 

7、sleep() 和 wait() 有什麼區別?

sleep是執行緒類(Thread)的方法,導致此執行緒暫停執行指定時間,給執行機會給其他執行緒,但是監控狀態依然保持,到時後會自動恢復。呼叫sleep不會釋放物件鎖。

wait是Object類的方法,對此物件呼叫wait方法導致本執行緒放棄物件鎖,進入等待此物件的等待鎖定池,只有針對此物件發出notify方法(或notifyAll)後本執行緒才進入物件鎖定池準備獲得物件鎖進入執行狀態。
8、Java Servlet API中forward() 與redirect()的區別
forward是伺服器請求資源,伺服器直接訪問目標地址的URL,把那個URL的響應內容讀取過來,然後把這些內容再發給瀏覽器,瀏覽器根本不知道服務 器傳送的內容是從哪兒來的,所以它的位址列中還是原來的地址。還有,轉發是在web應用程式之內進行的,可以訪問web應用程式所設定的內部目錄,像是 WEB-INF目錄,只能在Web應用程式中進行,不能指定至其它的Web應用程式的地址。
redirect就是服務端根據邏輯,傳送一個狀態碼,告訴瀏覽器重新去請求那個地址,一般來說瀏覽器會用剛才請求的所有引數重新請求,所以 session,request引數都可以獲取。web應用程式會要求客戶端瀏覽器重新發出請求地址,客戶端會重新連線至所指定的地址,因此瀏覽器的地址 會出現重新導向的資訊,重新導向後的請求由瀏覽器發出,所以不能訪問Web應用程式中的隱藏目錄,像是WEB-INF,重新是由瀏覽器重新要求一個網頁, 可以指定至其他的Web應用程式地址。
RequestDispatcher.forward()方法和HttpServletResponse.sendRedirect() 方法的區別是:前者僅是容器中控制權的轉向,在客戶端瀏覽器位址列中不會顯示出轉向後的地址,他是不會改變Request的值,如果你需要在下一個頁面中 能從中獲取新的資訊的話,你可以Request.setAttribute()來放置一些標誌,這樣從下一個頁面中獲取;後者則是完全的跳轉,瀏覽器將會 得到跳轉的地址,並重新傳送請求連結。這樣,從瀏覽器的位址列中可以看到跳轉後的連結地址。所以,前者更加高效,在前者可以滿足需要時,儘量使用 Request Dispatcher.forward()方法,並且,這樣也有助於隱藏實際的連結。在有些情況下,比如,需要跳轉到一個其它伺服器上的資源,則必須使用 HttpServletResponse.sendRequest()方法。
1、forward與include共亨Request範圍內的物件,而redirect則不行,即:如果一個javabean被宣告為request範圍的話,則被forward到的資源也可以訪問這個javabean,而redriect則不行。
2、forward與include基本上都是轉發到context內部的資源,而redirect可以重定向到外部的資源,如: req.sendRedriect

1.從位址列顯示來說
forward是伺服器請求資源,伺服器直接訪問目標地址的URL,把那個URL的響應內容讀取過來,然後把這些內容再發給瀏覽器.瀏覽器根本不知道伺服器傳送的內容從哪裡來的,所以它的位址列還是原來的地址.
redirect是服務端根據邏輯,傳送一個狀態碼,告訴瀏覽器重新去請求那個地址.所以位址列顯示的是新的URL.
2.從資料共享來說
forward:轉發頁面和轉發到的頁面可以共享request裡面的資料.
redirect:不能共享資料.
3.從運用地方來說
forward:一般用於使用者登陸的時候,根據角色轉發到相應的模組.
redirect:一般用於使用者登出登陸時返回主頁面和跳轉到其它的網站等.
4.從效率來說
forward:高.
redirect:低.

這是我在網上找到的資料,本來我以為我知道他們的區別,認為主要是當前的請求變數是否繼續有效,但看了這個,瞭解了請求的作用範圍,才完全明白其中的原因,希望對大家有用。
不要僅僅為了把變數傳到下一個頁面而使用session作用域,那會無故增大變數的作用域,轉發也許可以幫助你解決這個問題。
重定向:以前的request中存放的變數全部失效,並進入一個新的request作用域。
轉發:以前的request中存放的變數不會失效,就像把兩個頁面拼到了一起。
正文開始:
先是看上去不同,他們的呼叫分別如下:
request.getRequestDispatcher("apage.jsp").forward(request, response);//轉發到apage.jsp
response.sendRedirect("apage.jsp");//重定向到apage.jsp
在jsp頁面中你也會看到通過下面的方式實現轉發:
<jsp:forward page="apage.jsp" />
提到轉發和重定向就不得不提到request作用域。很多初學者都知道當我們提交一個表單時,就建立了一個新的請求。實際上,當我們點選一個連結時,也建立了一個新的請求。那麼一個請求的作用域到底有多大呢?例如:
在頁面a.jsp中有一個連結<a href="b.jsp?id=1">這是指向b的一個連結,而且還帶了一個引數</a>。當我們點選這個連線的時候,就產生了一個請 求,為了明確起見,我們把它叫做requestA->B。現在,在b.jsp頁面中我們就可以從這個請求中獲取資訊了。在b.jsp中你可以寫入 out.println(request.getParameter("id"))進行測試。下面更復雜一點,我們在b.jsp頁面中增加下面的語句:
request.setAttribute("name","funcreal");
out.println(request.getAttriblute("name"));//成功顯示了name變數的值。
現在在b.jsp中再增加一個連結:<a href="c.jsp?age=23">這是指向c的一個連結,而且還帶了一個引數</a>,當我們點選這個連線的時候,將產生一個 新的請求,這時requestA-B也就安息了,新的請求叫做requestB-C。同樣的道理,在c.jsp中,我們可以訪問到的變數只有age,因為 id,name這兩個變數都屬於requestA-B,此時他已經不存在了。下面是原始碼:
a.jsp
<%@ page c %>
<html>
<body bgcolor="#ffffff">
<a href="b.jsp?id=1">指向b.jsp,而且還帶了一個引數id=1。requestA-B現在誕生了</a>
</body>
</html>
b.jsp
<%@ page c %>
<html>
<body bgcolor="#ffffff">
<%
out.println("id=" + request.getParameter("id"));
request.setAttribute("name","Func Real");
out.println("name=" + request.getAttribute("name"));
%>
<a href="c.jsp?age=23">requestA-B已經結束了。指向c.jsp,而且還帶了一個引數age=23</a>
</body>
</html>
c.jsp
<%@ page c %>
<html>
<body bgcolor="#ffffff">
<%
out.println("id=" + request.getParameter("id"));
out.println("name=" + request.getAttribute("name"));
out.println("age=" + request.getParameter("age"));
%>
</body>
</html>
那麼轉發又是怎麼回事呢?現在增加一個頁面叫做d.jsp,並且在c.jsp中</body>前面增加一句<jsp:forward page="d.jsp"/>
d.jsp
<%@ page c %>
<html>
<body bgcolor="#ffffff">
requestB-C的魔爪已經伸到了d.jsp頁面
<%
out.println("age=" + request.getParameter("age"));
%>
</body>
</html>
執行程式,你會發現c頁面中的內容沒有顯示出來,因為forward是自動執行的,位址列中雖然是c.jsp但實際上,但瀏覽器中顯示的已經是d.jsp 的內容了,而且看到了從b.jsp傳過來的引數。你可以簡單得這樣理解:轉發,就是延長了requestB-C的作用域,<jsp:forward page="d.jsp"/>,這一句話實際上是把c.jsp和d.jsp粘到了一起,他們就像是在一個頁面中。
如果你用過struts,那麼你就知道為什麼在Action中,最後一句幾乎總是mapping.findForward("xxx");了。因為我們在這個Action中設定的請求作用域的變數都將會在下一個頁面(也許是另一個Action)中用到,所以要用轉發。
總結:
用重定向和轉發不是一個習慣問題。而是什麼情況下必須用什麼的問題。
不要僅僅為了把變數傳到下一個頁面而使用session作用域,那會無故增大變數的作用域,轉發也許可以幫助你解決這個問題。
重定向:以前的request中存放的變數全部失效,並進入一個新的request作用域。
轉發:以前的request中存放的變數不會失效,就像把兩個頁面拼到了一起。
forward是伺服器請求資源,伺服器直接訪問目標地址的URL,把那個URL的響應內容讀取過來,然後把這些內容再發給瀏覽器,瀏覽器根本不知道伺服器傳送的內容是從哪兒來的,所以它的位址列中還是原來的地址。
redirect就是服務端根據邏輯,傳送一個狀態碼,告訴瀏覽器重新去請求那個地址, web應用程式會要求客戶端瀏覽器重新發出請求地址,客戶端會重新連線至所指定的地址,因此瀏覽器的地址會出現重新導向的資訊,重新導向後的請求由瀏覽器發出。
forward與include共享Request範圍內的物件,而redirect則不行,
forward與include基本上都是轉發到context內部的資源,而redirect可以重定向到外部的資源
9、Servlet的生命週期
1,初始化階段  呼叫init()方法

2,響應客戶請求階段  呼叫service()方法

3,終止階段  呼叫destroy()方法
10、簡述AWT中事件處理機制
當事件源發生事件時,就會自動呼叫該事件的介面方法,而方法中就是編寫的事件處理程式程式碼。
要實現AWT中事件處理,需以下三步:
1,事件源
      能夠產生事件的物件都可以成為事件源,如文字框,按鈕等。也就是說,事件源必須是一個物件,而且這個物件必須是Java認為能夠發生時間的物件。
2,監視器
      需要一個物件對事件源進行監視,以便對發生的事件作出處理。
例如:對於文字框,這個方法為:addActionListener(監視器);
3,處理事件的介面
      監視器負責處理事件源發生的事件。為了讓監視器這個物件能對事件源發生的事件進行處理,建立該監視器物件的類必須申明實現相應的介面,即必須在類體中給出該介面中的所有方法體,那麼當事件源發生事件時,監視器就自動呼叫類實現的某個介面方法。
11、session、cookie和cache的區別是什麼?
解釋一:
session和cookies是儲存每個使用者單獨的資訊,前者儲存在伺服器。安全。後者儲存在客戶端。安全比較低。後者可以長期儲存。 <wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><br style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px">cache是快取。所有使用者都可以訪問到的物件。儲存在伺服器</span><br style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px">解釋二:</span><br style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px">session </span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px">資訊存於服務端,在互動時傳到客戶端一個sessionid,客戶端請求資料時傳送sessionid用於識別 </span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><br style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px">cookies </span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px">資訊存於客戶端,在效互時客戶端將資訊傳送至服務端,安全性較差. </span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><br style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px">cache用於緩衝資料,通常為網頁及媒體檔案,在涉及安全性的動態生成頁面上,可以設定有較時間,以便減少攻擊</span><br style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px">解釋三:</span><br style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px">一般session的預設生命週期是20分鐘,也就是說如果你在20分鐘裡沒有傳送任何請求的話,session就過期了。 </span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px">在 </span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px">web.config </span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px">檔案裡面設定: </span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><br style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px">&lt;sessionState </span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><br style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px">mode="InProc" </span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><br style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px">stateConnectionString="tcpip=127.0.0.1:42424" </span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><br style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px">sqlConnectionString="data </span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px">source=127.0.0.1;Trusted_Connection=yes" </span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><br style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px">cookieless="false" </span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><br style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px">timeout="20" </span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><br style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px">/&gt;</span><br style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px">cache不可靠,會隨時被根據系統要求而清除,但是自動管理過期時間和依賴,名副其實地是快取的作用。 </span><wbr style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><br style="color:rgb(70,70,70); font-family:simsun; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; line-height:21px">cookie會在客戶端瀏覽器和伺服器之間來回丟來丟去,是最原始的儲存使用者資訊的形式,對加密、效能等都無從考慮(當然在這個系統上擴充套件以後可以達到),並且瀏覽器對其大小限制得很緊。</span> </wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

session是存在伺服器的記憶體中 每個會話對應一個sessionId 通過sessionId開區分是那個會話的session,是以鍵值對的形式儲存

如果有幾千個session,怎麼提高效率?
幾千個session,怎麼提高效率,可以依靠Cookies幾千個session並不多,關鍵看session裡面裝了些什麼,session裡面只儲存使用者索引id就可以了。存幾萬個int也無所謂。
將全站的session內容做個統計,將所有相關的部分,規劃成物件,然後在session裡儲存例項。
這樣可以有效的減少session數量,提高使用和維護效率。

 

相關文章