Eclipse開workspace後,記得把檔案編碼設為UTF-8,這樣比較不會出錯
以for Java EE的版本來說,要設的地方共有以下幾個
編輯.gsp
檔(讓.gsp
也有程式碼上色)
*.gsp
,接著在點選剛加好的*.gsp ,再於Associated editors處加入JSP Editor*.gsp
try{
// code that will throw exception(s)
} catch (SomeException e1){
// code for handle exceptions which is an instance of class SomeException
} catch (AnotherException e2){
// code for handle exceptions which is an instance of class AnotherException
}
自1.7後,用|
把例外的class name串起來,就可以用同一個catch block處理多種例外
catch (Exception1 | Exception2 ex){
// code for handle exceptions which is either an instance of class Exception1 or Exception2
// (hence the xor `|` symbol)
}
詳細參考:http://docs.oracle.com/javase/7/docs/technotes/guides/language/catch-multiple.html#multiple
//在servelet的method內部
Scanner in = new Scanner(getServletContext().getResourceAsStream("path/file"););
注意檔案要置於WebContent
資料夾下,若要限制只能為servlet所用,則置於WebContent/WEB-INF
下
若使用Eclipse的Dynamic Web Project
則開project時,content directory
要設為webapps/ROOT
source的output folder
要設為webapps/ROOT/WEB-INF/classes
參考:http://stackoverflow.com/questions/33951613/azure-tomcat-eclipse-deployment
JSON官方source code及documentation:http://www.json.org/java/index.html
Java官方教學:https://docs.oracle.com/javase/tutorial/jaxp/index.html
相關package
import javax.xml.datatype.DatatypeFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
準備parse XML
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
// 取得XML檔的DOM
Document dom = db.parse("filename.xml");
// 取得root
Element root = dom.getDocumentElement();
Node、Element、NodeList
element
為一個Element物件//取得所有位在element底下,標籤名稱是tagName(在檔案中就是<tagName>)的node
NodeList entries = element.getElementsByTagName("tagName");
//取得element的children
NodeList entryChild = element.getChildNodes();
//取得element的內容(即<tagName></tagName>之間的東西)
String text = element.getTextContent();
//取得element的名稱(即在<>之中的名稱)
String nodeName = element.getNodeName();
//以attribute的名稱來取得attribute的值
//這個method要Element才有,如果是從NodeList中抽出來會是Node,要先強制轉型成Element
String attrVal = element.getAttribute("attrName");
預設是min heap,若想用max heap,在1.8之前只能用以下的constructor
// http://docs.oracle.com/javase/8/docs/api/java/util/PriorityQueue.html#PriorityQueue-int-java.util.Comparator-
public PriorityQueue(int initialCapacity, Comparator<? super E> comparator)
1.8之後則可以只指定comparator
// http://docs.oracle.com/javase/8/docs/api/java/util/PriorityQueue.html#PriorityQueue-java.util.Comparator-
public PriorityQueue(Comparator<? super E> comparator)
範例
PriorityQueue<Integer> pq1 = new PriorityQueue<Integer>(10, Collections.reverseOrder());
PriorityQueue<Integer> pq2 = new PriorityQueue<Integer>(Collections.reverseOrder());