SmileFace
與java一起走過的日子 |
所以:聲明為static的變量實質上就是全局變量。當聲明一個對象時,并不產生static變量的拷貝,而是該類所有的實例變量共用同一個static變量。
使用:java中只有static和非static變量,這個屬于每個類的,如果需要全局變量比如PI(3.14...),可以寫一個類Math,定義static變量PI,調用Math.PI就可以使用了,這樣就達到我們使用全局變量的目的了。
????DOM適合于當今流行的各種語言,包括Java,JavaScripte,VB,VBScript,Perl,C,C++等。它了為HTML和XML文檔提供了一個可應用于不同平臺的編程接口。W3C DOM的最新信息可從http://www.w3.org/TR2001/WD-DOM-Lever-3-Core-20010913查閱。微軟在http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk30/htm/xmconxmldomuserguide.asp上也有DOM的詳細技術信息。
???DOM的設計為了適用于不同的語言,它保留了不同語言中非常相似的API。但是它并不適合于Java編程者的習慣。而JDOM作為一種輕量級API被制定,它最核心的要求是以Java為中心,只適合于Java語言,它遵循DOM的接口主要規則,除去了DOM中為了兼容各語言而與Java習慣的不同。
二、使用JDOM的前提條件
???須要有SAX、DOM解析器的類文件,JDOM只是一種適合Java程序員來使用的Java XML解析器,目前流行的Java XML解析器還有:Apache Xerces Java、JAXP。
Xerces Java解析器是完全用Java編寫的XML解析器,最新版本是2.5,它支持以下標準和API:
(1) XML1.0規范(第二版本)
(2) XML命名空間規范
(3) DOM2核心標準規范
(4) SAX2核心擴展
(5) JAXP1.2 :是Sun提供的使用Java處理XML的接口API。
(6) XML Schema結構和數據類型標準
??????還有最好的是它開放源代碼,我們可以在http://xml.apache.org/dist/xerces-j/ 處去下載。下載文件Xerces-J-bin.2.5.0.zip。
解壓下載文件,得到四個壓縮包加到項目的路徑中(其實不要全加,但不熟的情況下考慮這么做)。
JDOM的二進制版本下載:http://www.jdom.org/downloads/index.html
把解壓后的jdom.jar文件加到項目的類路徑中,另外便于調試,還要下載它的源代碼。
三、使用JDOM解析XML
?????好了,現在該是正題了。下面通過一個簡單的例子說明一下怎么用JDOM這一適合Java程序員習慣的工具包來解析XML文檔。
為了簡單,我用了如下XML作為要解析的XML文件:
<?xml version="1.0" encoding="gb2312"?>
<books>
???<book email="zhoujunhui">
?????<name>rjzjh</name>
?????<price>60.0</price>
??</book>
</books>
夠簡單的吧,但它對于我們關心的東西都有了,子節點,屬性。
下面是用于解析這個XML文件的Java文件:
1 public class JDomParse { 2 public JDomParse(){ 3 String xmlpath="library.xml"; 4 SAXBuilder builder=new SAXBuilder(false); 5 try { 6 Document doc=builder.build(xmlpath); 7 Element books=doc.getRootElement(); 8 List booklist=books.getChildren("book"); 9 for (Iterator iter = booklist.iterator(); iter.hasNext();) { 10 Element book = (Element) iter.next(); 11 String email=book.getAttributeValue("email"); 12 System.out.println(email); 13 String name=book.getChildTextTrim("name"); 14 System.out.println(name); 15 book.getChild("name").setText("alterrjzjh"); 16 17 } 18 19 XMLOutputter outputter=new XMLOutputter(); 20 outputter.output(doc,new FileOutputStream(xmlpath)); 21 22 } catch (JDOMException e) { 23 e.printStackTrace(); 24 } catch (IOException e) { 25 e.printStackTrace(); 26 } 27 } 28 public static void main(String[] args) { 29 new JDomParse(); 30 } 31}不到30行代碼,現在我對代碼解釋一下:
首先要了解java默認的序列化行為,java將一切關于對象的信息都保存了下了,也就是說,有些時候那些不需要保存的也被保存了下來。一般情況下,我們僅僅需要保存邏輯數據就可以了。不需要保存的數據我們可以用關鍵字transient標出。
例如:
import java.io.*;
public class Serial implements Serializable {
int company_id;
String company_addr;
transient boolean company_flag;
}
其中的company_flag字段將不會參與序列化與反序列化,但同時也增加了為它初始值的責任。這也是序列化常常導致的問題之一。因為序列化相當于一個只接受數據流的public構造函數,這種對象構造方法是語言之外的。但他仍然是一種形式上的構造函數。如若你的類不能夠通過其他方面來保證初始化,則你需要額外的提供readObject方法,首先正常的反序列化,然后對transient標示的字段進行初始化。
在不適合的時候,使用java默認的序列化行為可能會帶來速度上的影響,最糟糕的情況是,可能導致溢出。在某些數據結構的實現中,經常會充斥著各種的循環引用,而java的默認序列化行為,并不了解你的對象結構,其結果就是java試圖通過一種昂貴的“圖遍歷”來保存對象狀態。可想而知,不但慢而且可能溢出。這時候你就要提供自己的readObject,來代替默認的行為。
note: http://developer.51cto.com/art/200601/20017.htm
5月份的時候,我的一篇blog里提到這個內容,那時候沒有深入寫,現在還是補補吧:2、在conf/catalina/localhost下找到與“項目名.xml”文件,程序名是webapps目錄下的項目文件夾名稱。在此文件中的 <context> 標記之間添加以下資源鏈接:
<ResourceLink name="jdbc/blue" type="javax.sql.DataSource" global="jdbc/blue"/>
注意:一定要加在這個文件中,而不是在項目文件夾里的web.xml中。至少我這樣做時結果不對。
3、 接下來就是測試了。測試程序很重要,盡量用簡單的了。以下是我的測試程序:
<%@ page contentType="text/html; charset=GBK"%>
<%@ page import="java.sql.*,javax.sql.DataSource,javax.naming.*"%>
<html>
<head><title>test.jsp</title></head>
<body bgcolor="#ffffff">
<%
Context initCtx=new InitialContext();
DataSource ds = (DataSource)initCtx.lookup("java:comp/env/jdbc/blue");
try
{
out.println("test! <br>");
Connection conn=ds.getConnection();
out.println("data from database:<br>");
Statement stmt=conn.createStatement();
ResultSet rs =stmt.executeQuery("select * from dept");
while(rs.next())
{
out.println(rs.getString(1));
out.println(rs.getString(2));
}
rs.close();
stmt.close();
}
catch(Exception e)
{
e.printStackTrace();
}
%>
</body>
</html>
Good Luck!
There are two conditons before you want to run *.jsp file in your web.
1. You have to put the *.jsp into a project which have already registered in the server.xml of conf folder in Tomcat,or the project which under the webapps folder directly
2.?In the project, you must have WEB-INF folder which have classes and lib subdirectory.
<!-- The mapping for the invoker servlet --> <!-- <servlet-mapping> <servlet-name>invoker</servlet-name> <url-pattern>/servlet/*</url-pattern> </servlet-mapping> -->
When you edit web.xml in the Tomcat conf directory, the servlet is ready to go。
例如,after saving Ch09_06.java and building the project, start Tomcat with the Tomcat plug-in's buttons and navigate to http://localhost:8080/Ch09_06/servlet/org.eclipsebook.ch09.Ch09_06.
2、就是編輯每個項目中的web.xml文件:
lternatively, if you don't want to edit web.xml in the Tomcat conf directory to enable anonymous servlets, you can create a local web.xml for each project you create with the Tomcat plug-in.
今天下載了一個Toad for mysql, 是freeware。 可是安裝它之前還要裝.Net的framework2讓我很不舒服。Anyway,? I?still need to use it, right? So, I decide not to think about it. Ok, just install everything it asked. The result is important to me. I have not too much time. Go ahead, my great plan?!!!
1 安裝JDK:
下載,解壓到想要的目錄;
修改、/etc/profile 文件,增加4行:
export JAVA_HOME=/usr/hong/tools/jdk1.5.0_08
export PATH=$JAVA_HOME/bin:$PATH
CLASSPATH=.:/usr/hong/tools/jdk1.5.0_08/lib/tools.jar:$CALSSPATH
export CLASSPATH
驗證: 輸入java -version 。如果版本信息正確,ok
2 eclipse: 下載,解壓即可
3 安裝tomcat:
下載,解壓到想要的目錄;
修改、/etc/profile 文件,增加4行:
CATALINA_BASE="/usr/hong/tools/jakarta-tomcat-5.0.28"
CATALINA_HOME="/usr/hong/tools/jakarta-tomcat-5.0.28"
PATH=$PATH:$CATALINA_HOME/bin
export CATALINA_BASE CATALINA_HOME PATH
啟動:Tomcat/bin/startup.sh
驗證:配置好環境變量以后你試試在瀏覽器中敲入: http://localhost:8080
為Tomcat新建一個管理用戶,為此需要編輯jakarta/conf/tomcat-users.xml,在里面添加類似的一行:
<user user-name="admin" password="20006600" roles="????" />
這樣在Tomcat主頁面中點擊左面的"Tomcat Administration"的鏈接,就可以對服務器進一步配置.
注冊自己的項目:假設你有一個自己的JSP項目,他應該安裝在下面的目錄下:/tomcat/webapps/myproject。你必須在tomcat/conf/server.xml中加入一行<Context path="/myproject" docBase="myproject" debug="0"/>
在瀏覽器中對應的地址為:http://localhost/myproject
Note:
when I install tomcat again, I found a wonderful document regarding how to installtomcat in linux, I am highly recommanding it for you. pls check with the url: http://linux-sxs.org/internet_serving/book1.html
| |||||||||
日 | 一 | 二 | 三 | 四 | 五 | 六 | |||
---|---|---|---|---|---|---|---|---|---|
25 | 26 | 27 | 28 | 29 | 30 | 31 | |||
1 | 2 | 3 | 4 | 5 | 6 | 7 | |||
8 | 9 | 10 | 11 | 12 | 13 | 14 | |||
15 | 16 | 17 | 18 | 19 | 20 | 21 | |||
22 | 23 | 24 | 25 | 26 | 27 | 28 | |||
29 | 30 | 1 | 2 | 3 | 4 | 5 |