2013年3月13日 星期三

提升JSP頁面響應速度的七大技巧

0


提升JSP頁面響應速度的七大技巧


方法一:在servlet的init()方法中緩存數據
當應用服務器初始化servlet實例之後,为客戶端請求提供服務之前,他會調用這個servlet的init()方法。在一個servlet的生命周期中,init()方法只會被調用一次。通過在init()方法中緩存一些靜態的數據或完成一些只需要執行一次的、耗時的操作,就可大大地提高系統性能。
例如,通過在init()方法中建立一個JDBC連接池是個最好例子,假設我們是用jdbc2.0的DataSource接口來取得數據庫連接,在通常的情況下,我們需要通過JNDI來取得具體的數據源。我們能夠想象在一個具體的應用中,假如每次SQL請求都要執行一次JNDI查詢的話,那系統性能將會急剧下降。解决方法是如下代碼,他通過緩存DataSource,使得下一次SQL調用時仍然能夠繼續利用他:
public class ControllerServlet extends HttpServlet{
private javax.sql.DataSource testDS = null;  
public void init(ServletConfig config) throws ServletException {
super.init(config);
Context ctx = null;
try{  
ctx = new InitialContext(); 
testDS = (javax.sql.DataSource)ctx.lookup("jdbc/testDS");
}catch(NamingException ne){ne.printStackTrace();}
}catch(Exception e){e.printStackTrace();}
} 
public javax.sql.DataSource getTestDS(){ 
 return testDS; 
} 
...
...
}

方法 2:禁止servlet和JSP 自動重載(auto-reloading)
Servlet/JSP提供了一個實用的技術,即自動重載技術,他为研發人員提供了一個好的研發環境,當您改變servlet和JSP頁面後而不必重启應用服務器。然而,這種技術在產品運行階段對系統的資源是個極大的損耗,因为他會给JSP引擎的類裝載器(classloader)帶來極大的負擔。因此關閉自動重載功能對系統性能的提升是個極大的幫助。


<Context path="/ROOT" docBase="ROOT" debug="0"  crosscontext="true" reloadable="false" />

<Context path="/ROOT" docBase="ROOT" debug="0"  crosscontext="true" reloadable="true" />



方法 3: 不要濫用HttpSession
在很多應用中,我們的程式需要保持客戶端的狀態,以便頁面之間能夠相互聯系。但不幸的是由於HTTP具備天生無狀態性,從而無法保存客戶端的狀態。因此一般的應用服務器都提供了session來保存客戶的狀態。在JSP應用服務器中,是通過HttpSession對像來實現session的功能的,但在方便的同時,他也给系統帶來了不小的負擔。因为每當您獲得或更新session時,系統者要對他進行費時的序列化操作。您能夠通過對HttpSession的以下幾種處理方式來提升系統的性能。
假如沒有必要,就應該關閉JSP頁面中對HttpSession的缺省配置。 假如您沒有明確指定的話,每個JSP頁面都會缺省地創建一個HttpSession。假如您的JSP中無需使用session的話,那能夠通過如下的JSP頁面指示符來禁止他:
<%@ page session="false"%>

不要在HttpSession中存放大的數據對像:假如您在HttpSession中存放大的數據對像的話,每當對他進行讀寫時,應用服務器都將對其進行序列化,從而增加了系統的額外負擔。您在HttpSession中存放的數據對像越大,那系統的性能就下降得越快。
當您無需HttpSession時,盡快地釋放他:當您不再需要session時,您能夠通過調用HttpSession.invalidate()方法來釋放他。盡量將session的超時時間設得短一點:在JSP應用服務器中,有一個缺省的session的超時時間。當客戶在這個時間之後沒有進行任何操作的話,系統會將相關的session自動從內存中釋放。超時時間設得越大,系統的性能就會越低,因此最好的方法就是盡量使得他的值保持在一個較低的水平。



<%= session="false" %>
HttpSession.invalidate()


方法 4: 將頁面輸出進行壓縮
壓縮是解决數據冗餘的一個好的方法,特別是在網络帶寬不夠發達的今天。有的瀏覽器支持gzip(GNU zip)進行來對HTML文檔進行壓縮,這種方法能夠戲剧性地減少HTML文檔的下載時間。因此,假如您將servlet或JSP頁面生成的HTML頁面進行壓縮的話,那用戶就會覺得頁面瀏覽速度會很快。但不幸的是,不是任何的瀏覽器都支持gzip壓縮,但您能夠通過在您的程式中檢查客戶的瀏覽器是否支持他。下面就是關於這種方法實現的一個代碼片段:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
OutputStream out = null;
String encoding = request.getHeader("Accept-Encoding");  
if (encoding != null && encoding.indexOf("gzip") != -1){
response.setHeader("Content-Encoding" , "gzip");
out = new GZIPOutputStream(response.getOutputStream());
} 
else if (encoding != null && encoding.indexOf("comdivss") != -1){
response.setHeader("Content-Encoding" , "comdivss");
out = new ZIPOutputStream(response.getOutputStream());
}else{
out = response.getOutputStream();
} 
... 
...
}


response.setContentType("text/html");
String encodings = request.getHeader("Accept-Encoding");
PrintWriter out = null;
if ((encodings != null) && (encodings.indexOf("gzip") != -1)) {
OutputStream outA = response.getOutputStream();
out = new PrintWriter(new GZIPOutputStream(outA), false);
response.setHeader("Content-Encoding", "gzip");
} else {
out = response.getWriter();
}
out.println("");
for(int i=0; i<10000 br="" i=""> out.println("blah blah blah
");
}
out.println("");
out.close();
方法 5: 使用線程池
應用服務器缺省地为每個不同的客戶端請求創建一個線程進行處理,並为他們分派service()方法,當service()方法調用完成後,和之相應的線程也隨之撤消。由於創建和撤消線程會耗費一定的系統資源,這種缺省模式降低了系統的性能。但所幸的是我們能夠通過創建一個線程池來改變這種狀況。
另外,我們還要为這個線程池配置一個最小線程數和一個最大線程數。在應用服務器启動時,他會創建數量等於最小線程數的一個線程池,當客戶有請求時,相應地從池從取出一個線程來進行處理,當處理完成後,再將線程重新放入到池中。假如池中的線程不夠地話,系統會自動地增加池中線程的數量,但總量不能超過最大線程數。通過使用線程池,當客戶端請求急剧增加時,系統的負載就會呈現的平滑的上升曲線,從而提高的系統的可伸縮性。

方法 6: 選擇正確的頁面包含機制
在JSP中有兩種方法能夠用來包含另一個頁面:
1、使用include指示符
<%@ includee file=”test.jsp” %>
2、使用jsp指示符
<jsp:includee page=”test.jsp” flush=”true”/>

在實際中發現,假如使用第一種方法的話,能夠使得系統性能更高。
方法 7:正確地確定javabean的生命周期
JSP的一個強大的地方就是對javabean的支持。通過在JSP頁面中使用jsp:useBean標簽,能夠將javabean直接插入到一個JSP頁面中。他的使用方法如下:
<jsp:useBean id="name" scope="page|request|session|application"
class="package.className" type="typeName">
</jsp:useBean>

其中scope屬性指出了這個bean的生命周期。缺省的生命周期为page。假如您沒有正確地選擇bean的生命周期的話,他將影響系統的性能。
舉例來說,假如您只想在一次請求中使用某個bean,但您卻將這個bean的生命周期配置成了session,那當這次請求結束後,這個bean將仍然保留在內存中,除非session超時或用戶關閉瀏覽器。這样會耗費一定的內存,並無謂的增加了JVM垃圾收集器的工作量。因此为bean配置正確的生命周期,並在bean的使命結束後盡快地清理他們,會使用系統性能有一個提高。
其他一些有用的方法

1、在字符串連接操作中盡量不使用“ ”操作符:在java編程中,我們常常使用“ ”操作符來將幾個字符串連接起來,但您或許從來沒有想到過他居然會對系統性能造成影響吧?由於字符串是常量,因此JVM會產生一些臨時的對像。您使用的“ ”越多,生成的臨時對像就越多,這样也會给系統性能帶來一些影響。解决的方法是用StringBuffer對像來代替“ ”操作符。

2、避免使用System.out.println()方法:由於System.out.println()是一種同步調用,即在調用他時,磁盤I/O操作必須等待他的完成,因此我們要盡量避免對他的調用。但我們在調試程式時他又是個必不可少的方便工具,为了解决這個矛盾,我建議您最好使用Log4j工具(http://Jakarta.apache.org ),他既能夠方便調試,而不會產生System.out.println()這样的方法。

3、ServletOutputStream 和 PrintWriter的權衡:使用PrintWriter可能會帶來一些小的開銷,因为他將任何的原始輸出都轉換为字符流來輸出,因此假如使用他來作为頁面輸出的話,系統要負擔一個轉換過程。而使用ServletOutputStream作为頁面輸出的話就不存在一個問題,但他是以二進制進行輸出的。因此在實際應用中要權衡兩者的利弊。

總結
本文的目的是通過對servlet和JSP的一些調優技術來極大地提高您的應用程式的性能,並因此提升整個J2EE應用的性能。通過這些調優技術,您能夠發現其實並不是某種技術平台(比如J2EE和.NET之爭)决定了您的應用程式的性能,重要是您要對這種平台有一個較为深入的了解,這样您才能從根本上對自己的應用程式做一個優化。


References Site

http://www.coderanch.com/t/348028/Servlets/java/send-JSP-page-compressed-GZIP


http://rritw.com/a/JAVAbiancheng/JSPjishu/20120906/218648.html

2013年3月11日 星期一

HTML 5 Form 的功能太強大了!!!

0

HTML 5 Form 的功能太強大了!!! http://www.w3schools.com/html/html5_form_attributes.asp
但IE的支持太差勁了
http://beta.html5test.com/

http://html5test.com/results/desktop.html





http://nowills.blogspot.hk/2012/01/html5ie9iehtml5-html5shiv.html
各位老傢伙們,該學 HTML5 了
http://www.pigo.idv.tw/archives/952
First name: Last name: E-mail: Enter a date before 1980-01-01: Enter a date after 2000-01-01: Quantity (between 1 and 5): Select images: Country code:
First name:
Last name:
E-mail:






Enter a date before 1980-01-01:
Enter a date after 2000-01-01:
Quantity (between 1 and 5):
Select images:
Country code:



JSP - Life Cycle ( Init & Destroy)

2

JSP - Life Cycle


A JSP life cycle can be defined as the entire process from its creation till the destruction which is similar to a servlet life cycle with an additional step which is required to compile a JSP into servlet.
The following are the paths followed by a JSP
  • Compilation
  • Initialization
  • Execution
  • Cleanup
The four major phases of JSP life cycle are very similar to Servlet Life Cycle and they are as follows:


JSP Compilation:


When a browser asks for a JSP, the JSP engine first checks to see whether it needs to compile the page. If the page has never been compiled, or if the JSP has been modified since it was last compiled, the JSP engine compiles the page.
The compilation process involves three steps:
Parsing the JSP.
Turning the JSP into a servlet.
Compiling the servlet.
JSP Initialization:
When a container loads a JSP it invokes the jspInit() method before servicing any requests. If you need to perform JSP-specific initialization, override the jspInit() method:
public void jspInit(){
  // Initialization code...
}
Typically initialization is performed only once and as with the servlet init method, you generally initialize database connections, open files, and create lookup tables in the jspInit method.

JSP Execution:


This phase of the JSP life cycle represents all interactions with requests until the JSP is destroyed.
Whenever a browser requests a JSP and the page has been loaded and initialized, the JSP engine invokes the _jspService() method in the JSP.
The _jspService() method takes an HttpServletRequest and an HttpServletResponse as its parameters as follows:
void _jspService(HttpServletRequest request, 
                 HttpServletResponse response)
{
   // Service handling code...
}
The _jspService() method of a JSP is invoked once per a request and is responsible for generating the response for that request and this method is also responsible for generating responses to all seven of the HTTP methods ie. GET, POST, DELETE etc.

JSP Cleanup:


The destruction phase of the JSP life cycle represents when a JSP is being removed from use by a container.
The jspDestroy() method is the JSP equivalent of the destroy method for servlets. Override jspDestroy when you need to perform any cleanup, such as releasing database connections or closing open files.
The jspDestroy() method has the following form:
public void jspDestroy()
{
   // Your cleanup code goes here.
}

2013年2月28日 星期四

用Java替中文網址轉碼:URLEncoder

0


用Java替中文網址轉碼:URLEncoder


在程式或者網頁的應用中我們常常需要把中文轉換為其他編碼

下面介紹這些也不僅僅限於轉換網址而已


在我目前的應用中只用過了JAVA
使用的函數是 URLEncoder.encode(String 字串, String 編碼)
(編碼為:UTF-8, UTF-16等等)
以下是節錄自用javascript轉UTF-8編碼的編碼解碼介紹

#Java#
會處理#字元為%23,空白字元轉換為+,中文字拆開每BYTE處理為ASCII

第二個String 為Locale

java.net.URLEncoder.encode(String args,String args)

java.net.URLDecoder.decode(String args,String args)




URLEncoder.encode("random word £500 bank $", "ISO-8859-1");
String s = "許功蓋";
URLEncoder.encode(s, "UTF-8")

Java API URLEncoder
http://docs.oracle.com/javase/1.4.2/docs/api/java/net/URLEncoder.html






URL Encoding Reference

ASCII CharacterURL-encoding
space%20
!%21
"%22
#%23
$%24
%%25
&%26
'%27
(%28
)%29
*%2A
+%2B
,%2C
-%2D
.%2E
/%2F
0%30
1%31
2%32
3%33
4%34
5%35
6%36
7%37
8%38
9%39
:%3A
;%3B
<%3C
=%3D
>%3E
?%3F
@%40
A%41
B%42
C%43
D%44
E%45
F%46
G%47
H%48
I%49
J%4A
K%4B
L%4C
M%4D
N%4E
O%4F
P%50
Q%51
R%52
S%53
T%54
U%55
V%56
W%57
X%58
Y%59
Z%5A
[%5B
\%5C
]%5D
^%5E
_%5F
`%60
a%61
b%62
c%63
d%64
e%65
f%66
g%67
h%68
i%69
j%6A
k%6B
l%6C
m%6D
n%6E
o%6F
p%70
q%71
r%72
s%73
t%74
u%75
v%76
w%77
x%78
y%79
z%7A
{%7B
|%7C
}%7D
~%7E
 %7F
`%80
%81
%82
ƒ%83
%84
%85
%86
%87
ˆ%88
%89
Š%8A
%8B
Œ%8C
%8D
Ž%8E
%8F
%90
%91
%92
%93
%94
%95
%96
%97
˜%98
%99
š%9A
%9B
œ%9C
%9D
ž%9E
Ÿ%9F
 %A0
¡%A1
¢%A2
£%A3
¤%A4
¥%A5
¦%A6
§%A7
¨%A8
©%A9
ª%AA
«%AB
¬%AC
%AD
®%AE
¯%AF
°%B0
±%B1
²%B2
³%B3
´%B4
µ%B5
%B6
·%B7
¸%B8
¹%B9
º%BA
»%BB
¼%BC
½%BD
¾%BE
¿%BF
À%C0
Á%C1
Â%C2
Ã%C3
Ä%C4
Å%C5
Æ%C6
Ç%C7
È%C8
É%C9
Ê%CA
Ë%CB
Ì%CC
Í%CD
Î%CE
Ï%CF
Ð%D0
Ñ%D1
Ò%D2
Ó%D3
Ô%D4
Õ%D5
Ö%D6
×%D7
Ø%D8
Ù%D9
Ú%DA
Û%DB
Ü%DC
Ý%DD
Þ%DE
ß%DF
à%E0
á%E1
â%E2
ã%E3
ä%E4
å%E5
æ%E6
ç%E7
è%E8
é%E9
ê%EA
ë%EB
ì%EC
í%ED
î%EE
ï%EF
ð%F0
ñ%F1
ò%F2
ó%F3
ô%F4
õ%F5
ö%F6
÷%F7
ø%F8
ù%F9
ú%FA
û%FB
ü%FC
ý%FD
þ%FE
ÿ%FF