顯示具有 Servlet 標籤的文章。 顯示所有文章
顯示具有 Servlet 標籤的文章。 顯示所有文章

2015年4月5日 星期日

Netbean 8 上的常用快速鍵 (Keyboard Shortcuts)

在寫 Servlet/JSP 程式時,以下的快速鍵可以幫忙節省鍵盤輸入的時間(因為,程式設計師的生產力和時間有關係) 


  • 往下重覆一行(Duplicate line down): Ctrl+Shift+Down 
  • 往上重覆一行(Duplicate line up): Ctrl+Shift+Up 


 若要自動格式化程式碼(Format source code),讓你的程式縮排更美:
  • 格式化程式碼: Alt+Shift+F
要找更多的快速鍵? (M)Tool > Options > (B)Keymap 可以找到更多 Netbeans 8 所提供的快速鍵。

Complete line and create a new line: Ctrl+Shift+semicolon
Complete line: Ctrl+Semicolon


Bookmarks:
Ctrl+Shift+M: Add or Remove the bookmark
Ctrl+Shift+Period: Bookmark history popup next
Ctrl+Shift+Comma: Bookmark history popup previous


References:
8 New & Beefed Up NetBeans Keyboard Shortcuts!

2011年2月7日 星期一

檢查 browser 的 cookie 功能是否啟動

參考我的 wiki 網頁: 檢查 browser 的 cookie 功能是否啟動

網頁自動更新使用 meta refresh 指令

要讓 browser 能自動更新, 或者自動轉到另一個網頁使用指令:

<meta http-equiv="refresh" content="5" />

為什麼要用 meta refresh 而不用 HttpServletRequest.sendRedirect() 方法呢? 因為 sendRedirect() 方法在重新導向時, 無法將 cookie 傳回伺服器端.

Reference:

* Meta refresh
* p174, 精通JAVA WEB程式設計, 葉雅美 - 維科出版社 ,2004-10-01 出版

將字串編碼成 html 格式

在寫 servlets/JSPs 時, 常要要輸出符合 html 格式的字串, 例如:

"bread" & "butter"  要變成 "bread" & "butter"


有現成的套件可以做此轉換.

org.apache.commons.lang.StringEscapeUtils 這個類別提供了:

public static String escapeHtml(String str)

可以將字串轉成 html 的格式.

Reference:
* Apache Commons
* escapeHtml()
* Robin's Tech Tips

2010年4月11日 星期日

取得 HTTP post 的 body

要取得 HTTP POST 的 body 可以使用 ServletRequest interface 的兩個方法: 

BufferedReader getReader()
          Retrieves the body of the request as character data using a BufferedReader.

ServletInputStream getInputStream()
          Retrieves the body of the request as binary data using a ServletInputStream.


getReader() 方法用於字元資料, getInputStream() 方法用於 binary data

2010年4月10日 星期六

設定 tomcat 處理某個 request 所使用的 Charset

要設定 Tomcat 處理某個 request 時所使用的 Charset, 要使用 ServletRequest interface 中的方法:

void setCharacterEncoding(String env)
          Overrides the name of the character encoding used in the body of this request.

例如,要處理 UTF8 的 request:

request.setCharacterEncoding("UTF-8");

Tomcat 所預設的 Charset 為: ISO-8859-1.