2006年7月19日
#
一.HTTP請求:
HTTP請求分為:
? 1).請求行
? 2).消息頭
? 3).空行
? 4).正文
1.請求行
? [方法 URI HTTP版本信息]
? 如: GET /index.htm HTTP/1.0
2.方法(全部大寫):
? GET????? 請求URI標(biāo)識的資源
? HEAD???? 請求獲取響應(yīng)消息頭
? PUT????? 請求存儲資源,并用URI作為其標(biāo)識
? POST???? 請求服務(wù)器接收信息
? CONNECT? ?
? TRACE???
? DELETE
? OPTIONS
二.HTTP響應(yīng):
? 1).狀態(tài)行
? 2).消息頭
? 3).空行
? 4).正文(資源的內(nèi)容,比如index.htm文件的文本內(nèi)容)
1.狀態(tài)行
? HTTP版本信息 狀態(tài)碼 響應(yīng)碼描述
? 例: HTTP/1.1 200 OK
2.狀態(tài)碼(第一位表示響應(yīng)的類別)
? 1xx:
? 2xx:
? 3xx:
? 4xx:
? 5xx:
HTTP協(xié)議狀態(tài)碼具體意義
?? 100? :? Continue
?? 101? :? witchingProtocols
?? 200? :? OK
?? 201? :? Created
?? 202? :? Accepted
?? 203? :? Non-AuthoritativeInformation
?? 204? :? NoContent
?? 205? :? ResetContent
?? 206? :? PartialContent
?? 300? :? MultipleChoices
?? 301? :? MovedPermanently
?? 302? :? Found
?? 303? :? SeeOther
?? 304? :? NotModified
?? 305? :? UseProxy
?? 307? :? TemporaryRedirect
?? 400? :? BadRequest
?? 401? :? Unauthorized
?? 402? :? PaymentRequired
?? 403? :? Forbidden
?? 404? :? NotFound
?? 405? :? MethodNotAllowed
?? 406? :? NotAcceptable
?? 407? :? ProxyAuthenticationRequired
?? 408? :? RequestTime-out
?? 409? :? Conflict
?? 410? :? Gone
?? 411? :? LengthRequired
?? 412? :? PreconditionFailed
?? 413? :? RequestEntityTooLarge
?? 414? :? Request-URITooLarge
?? 415? :? UnsupportedMediaType
?? 416? :? Requestedrangenotsatisfiable
?? 417? :? ExpectationFailed
?? 500? :? InternalServerError
?? 501? :? NotImplemented
?? 502? :? BadGateway
?? 503? :? ServiceUnavailable
?? 504? :? GatewayTime-out
?? 505? :? HTTPVersionnotsupported
三.HTTP消息頭:
1. 普通
2. 請求頭
3. 響應(yīng)頭
4. 實(shí)體頭
格式:(名字大小寫無關(guān))
<名字>:<空格><值>
1.普通頭
? .Cache-Control? (HTTP1.1,? HTTP1.0:Pragma)
????? 緩存指令:
????? 請求時(shí): no-cache,no-store,max-age,max-stale,min-fresh,only-if-cached
????? 響應(yīng)時(shí): public,private,no-cache,no-store,no-transform,must-revalidate,proxy-revalidate,max-age,s-maxage.
????? 例: Cache-Control: no-cache
? .Date
????? 客戶端:在發(fā)送正文時(shí)要包含Date,
????? 服務(wù)器:在響應(yīng)時(shí)包含Date.
? .Connection
? .Pragma(1.0用)
2. 請求頭
? .Accept
? .Accept-Charset
? .Accept-Encoding
? .Accept-Language
? .Authorization
? .Host(必須的)
? .User-agent
3.響應(yīng)頭
? .Location
? .Server
? .WWW-Authenticate,要包含在401中.
4.實(shí)體頭
? .Content-Encoding
? .Content-Language
? .Content-Length
? .Content-Type
? .Last-Modified
? .Expires
?
package wlz.xml;
import javax.xml.parsers.*;
import org.w3c.dom.*;
//import javax.xml.transform.*;
//import javax.xml.transform.dom.DOMSource;
//import javax.xml.transform.stream.StreamResult;
import java.io.*;
import org.apache.xml.serialize.*;
public class WriteXml {
??? public static void writeXml(Document doc,String filename) throws Exception{
??????? /*TransformerFactory tf=TransformerFactory.newInstance();
??????? Transformer f=tf.newTransformer();
??????? //f.setOutputProperties();
??????? DOMSource source=new DOMSource(doc);
??????? StreamResult result=new StreamResult(new File(filename));
??????? f.transform(source,result);*/
?????? ?
??????? FileOutputStream fos = new FileOutputStream(filename);
??????? OutputFormat of = new OutputFormat("XML","GB2312",true);
??????? of.setIndent(2);
??????? of.setIndenting(true);
??????? XMLSerializer serializer = new XMLSerializer(fos,of);
?????? ?
??????? serializer.asDOMSerializer();
??????? serializer.serialize(doc.getDocumentElement());
??????? fos.close();
??? }
?? ?
??? public static void outputElement(Document doc,String elementName){
??????? NodeList list= doc.getElementsByTagName(elementName);
??????? System.out.println("------------------------------------------");
??????? for(int i=0;i<list.getLength();i++){
??????????? System.out.println(elementName+"="+list.item(i).getFirstChild().getNodeValue()); //取出元素的值
??????? }
??????? System.out.println("------------------------------------------");
??? }
?? ?
??? public static void addElement(Document doc,Element root,String name,String age,String sex){
??????? Element student=doc.createElement("student");
??????? Element ename=doc.createElement("name");
??????? Element eage=doc.createElement("age");
??????? Element esex=doc.createElement("sex");
???????????? ?
??????? ename.appendChild(doc.createTextNode(name));
??????? eage.appendChild(doc.createTextNode(age));
??????? esex.appendChild(doc.createTextNode(sex));
?????? ?
??????? student.appendChild(ename);
??????? student.appendChild(eage);
??????? student.appendChild(esex);
?????? ?
??????? root.appendChild(student);
??? }
?? ?
??? public static Document createDocument() throws Exception{
??????? DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
??????? DocumentBuilder db=dbf.newDocumentBuilder();
??????? Document doc=db.newDocument();
??????? return doc;
??? }
?? ?
??? public static void main(String[] args) throws Exception{
/*
output the xml
<class name="計(jì)算機(jī)1班">
?? ?<student>
?? ??? <name>
?? ??? <age>
?? ??? <sex>
??? </student>
??? <student>
?????? <name>
?????? <age>
?????? <sex>
??? </student>
</class>
?*/?? ?
??????? Document doc=createDocument();
??????? doc.createProcessingInstruction("encoding","gb2312");
??????? Element root=doc.createElement("class");
??????? root.setAttribute("name","計(jì)算機(jī)1班");
??????? doc.appendChild(root);
?????? ?
??????? addElement(doc,root,"黃蓉","30","女");
??????? addElement(doc,root,"郭靖","32","男");
??????? addElement(doc,root,"楊過","8","男");
?????? ?
??????? outputElement(doc,"name");
??????? outputElement(doc,"sex");
?????? ?
??????? writeXml(doc,"mydomxml.xml");
??????? System.out.println("output ok.");
?????????????? ?
??? }
}
1.線程中要使用的類.各線程只有其一個引用.
public class VarClass {
???
??? private static ThreadLocal threadVar=new ThreadLocal(){
??????? protected synchronized Object initialValue(){
??????????? System.out.println(Thread.currentThread().getName()+" initial value is 1");
??????????? return new Integer(1);
??????? }};
???
??? public int getValue(){
??????? return ((Integer)threadVar.get()).intValue();
??? }
???
??? public void setValue(){
??????? int a=getValue();
??????? a++;
??????? threadVar.set(new Integer(a));
??? }
}
2.線程類
public class Worker extends Thread {
??? private long interval=0;
??? private boolean isRun=true;
??? private VarClass v=null;
???
??? public Worker(String name,VarClass v,long interval){
??????? setName(name);
??????? this.v=v;
??????? this.interval=interval;
??? }
??? public void run() {
??????? while(isRun){
??????????? try{
??????????????? Thread.sleep(interval);
??????????? }catch(InterruptedException e){
??????????????? e.printStackTrace();
??????????? }
??????????? v.setValue();
??????? }
??????? System.out.println(getName()+" is over at "+v.getValue());
??? }
???
??? public void stopThread(){
??????? isRun=false;
??? }
}
3.測試類
public class TestThreadLocal {
?? public static void main(String[] args){
?????? VarClass v=new VarClass();
??????
?????? Worker w1=new Worker("Thread_A",v,100);
?????? Worker w2=new Worker("Thread_B",v,200);
?????? Worker w3=new Worker("Thread_C",v,300);
?????? Worker w4=new Worker("Thread_D",v,400);
?????? Worker w5=new Worker("Thread_E",v,500);
??????
?????? w1.start();
?????? w2.start();
?????? w3.start();
?????? w4.start();
?????? w5.start();
?????????????????????????
?????? System.out.println("All threads is over after 20 seconds");
??????
?????? //延時(shí)20秒后,終止5個線程
?????? try{
?????????? Thread.sleep(20000);
?????? }catch(InterruptedException e){
?????????? e.printStackTrace();
?????? }
??????
?????? System.out.println("All threads will be overed");
?????? w1.stopThread();
?????? w2.stopThread();
?????? w3.stopThread();
?????? w4.stopThread();
?????? w5.stopThread();
? }
}
4.測試結(jié)果:
All threads is over after 20 seconds
Thread_A initial value is 1
Thread_B initial value is 1
Thread_C initial value is 1
Thread_D initial value is 1
Thread_E initial value is 1
All threads will be overed
Thread_A is over at 200
Thread_B is over at 101
Thread_D is over at 51
Thread_C is over at 68
Thread_E is over at 42
5.結(jié)果說明:雖然各線程使用的是同一個對象的引用,但由于使用了ThreadLocal,實(shí)際上每個線程所操作的數(shù)據(jù)是不一樣的.
1. Base64使用A--Z,a--z,0--9,+,/ 這64個字符.
2. 編碼原理:將3個字節(jié)轉(zhuǎn)換成4個字節(jié)( (3 X 8) = 24 = (4 X 6) )
??????????? 先讀入3個字節(jié),每讀一個字節(jié),左移8位,再右移四次,每次6位,這樣就有4個字節(jié)了.
3. 解碼原理:將4個字節(jié)轉(zhuǎn)換成3個字節(jié).
??????????? 先讀入4個6位(用或運(yùn)算),每次左移6位,再右移3次,每次8位.這樣就還原了.
proxool連接池的配置(0.8.3)
1. 配置文件(xml形式,文件名任意)
--------------------------------
<?xml version="1.0"?>
<!-- the proxool configuration can be embedded within your own application's.
Anything outside the "proxool" tag is ignored. -->
<something-else-entirely>
? <proxool>
??? <alias>mypool</alias>? <!-- add "proxool" before alias -- proxool.alias -->
??? <driver-url>jdbc:oracle:thin:@localhost:1521:oradb</driver-url>
??? <driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
??? <driver-properties>
????? <property name="user"???? value="username"/>
????? <property name="password" value="password"/>
??? </driver-properties>
?? ?<connection-lifetime>60</connection-lifetime>
?? ??? ?<maximum-connection-count>50</maximum-connection-count>
?? ?<minimum-connection-count>4</minimum-connection-count>
??? <house-keeping-test-sql>select CURRENT_DATE</house-keeping-test-sql>
? </proxool>
</something-else-entirely>
2.web.xml配置
--------------
<servlet>
??? <servlet-name>ServletConfigurator</servlet-name>
??????? <servlet-class>
?? ???? org.logicalcobwebs.proxool.configuration.ServletConfigurator
??????? </servlet-class>
?? ?<init-param>
?? ???? <param-name>xmlFile</param-name>
?? ???? <param-value>WEB-INF/proxool.xml</param-value>
??????? </init-param>
??? <load-on-startup>1</load-on-startup>
</servlet>
<!-- monitor proxool status -->
<servlet>
??? <servlet-name>Admin</servlet-name>
??? <servlet-class>org.logicalcobwebs.proxool.admin.servlet.AdminServlet</servlet-class>
</servlet>
<servlet-mapping>
??? <servlet-name>Admin</servlet-name>
??? <url-pattern>/admin</url-pattern>
</servlet-mapping>
3. 程序調(diào)用
Connection conn=null;
try {
??? Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");
??? conn = DriverManager.getConnection("proxool.mypool"); //add "proxool" before "mypool" in proxool.xml
}catch(ClassNotFountException e){
??? e.printStackTrace();
}catch(SQLException e) {
??? e.printStackTrace();
}
1. wait:
?? Causes current thread to wait until either another thread invokes the notify()
method or the
notifyAll()
method for this object
?? This method should only be called by a thread that is the owner of this object's
monitor
?? 使當(dāng)前線程放棄對象鎖(等待?),直到其它線程為該對象調(diào)用notify()或notifyAll().
?? 這個方法只能被擁有對象鎖(監(jiān)聽器?)的線程執(zhí)行。
??
??
2. notify,notifyAll
?? Wakes up a or all threads that are waiting on this object's monitor.
??
?? 喚醒正在等待指定對象的鎖的一個或所有線程。
-- 這樣翻譯也不知是否準(zhǔn)確,括號內(nèi)是按直譯過來的意思。
3. 四種方式?
?? 1.static synchronized method(){}
?? 2.sychronized(Class)
?? 3.sychronized method(){}
?? 4.sychronized() {}
4.? 書上沒說過的: Spin? Lock (旋轉(zhuǎn)鎖)
本例以租房子為例:
一.說明:
?? 動態(tài)代理可動態(tài)為某個類添加代理,以攔截客戶端的調(diào)用,在此基礎(chǔ)上進(jìn)行額外的處理.
?? 目前較流行的AOP技術(shù),就有以動態(tài)代理為技術(shù)基礎(chǔ)進(jìn)行實(shí)現(xiàn)的.
?? 本例中,中介作為房子的動態(tài)代理,所有調(diào)用房子的方法,必須經(jīng)過中介類(HouseAgency).
二.源代碼:
?? 1.House接口:
public interface House {
??? public void rent();
??? public int getPrice();
}
?? 2.House接口實(shí)現(xiàn)類ConcreateHouse:
public class ConcreteHouse implements House{
??? private int price;
???
??? public ConcreteHouse(int price){
??? ??? this.price=price;
??? }
???
??? public void rent(){
??? ??? System.out.println("rent ok!");
??? }
???
??? public int getPrice(){
??? ??? return price;
??? }
}
?? 3.實(shí)現(xiàn)InvocationHandler接口的中介類:
???
import java.lang.reflect.*;
public class HouseAgency implements InvocationHandler {
??? private Object house;
??? public HouseAgency(Object house){
??? ??? this.house=house;
??? }
???
??? public? HouseAgency(){}
?? ??
??? public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
??? ??? Object result=null;
??? ??? if("getPrice".equals(method.getName())){
??? ??? ??? System.out.println("invoking getPrice() to query rent price.");
??? ??? }
??? ??? if("rent".equals(method.getName())){
??? ??? ??? System.out.println("invoking rent() to rent the house.");
??? ??? }
??? ??? result=method.invoke(house,args);
??? ??? return result;
??? }
}
?? 4.客戶端
import java.lang.reflect.*;
public class HouseClient{
??? public static void main(String[] args){
??? ??? ConcreteHouse house1=new ConcreteHouse(400);
??? ??? HouseAgency ha=new HouseAgency(house1);
??? ??? House house=(House)Proxy.newProxyInstance(house1.getClass().getClassLoader(),
??? ??? ??? ??? ????????????????????????????????? house1.getClass().getInterfaces(),ha);
??? ???
??? ??? int price=house.getPrice();
??? ???
??? ??? System.out.println("the house rent is : "+price);
??? ???
??? ??? if(price>300){
??? ??? ??? house.rent();
??? ??? }
??? }
}
三:打印結(jié)果
invoking getPrice() to query rent price.
the house rent is : 400
invoking rent() to rent the house.
rent ok!