My Java Blog Park

          2006年8月3日 #

          http學習筆記

          一.HTTP請求:
          HTTP請求分為:
          ? 1).請求行
          ? 2).消息頭
          ? 3).空行
          ? 4).正文

          1.請求行
          ? [方法 URI HTTP版本信息]
          ? 如: GET /index.htm HTTP/1.0

          2.方法(全部大寫):
          ? GET????? 請求URI標識的資源
          ? HEAD???? 請求獲取響應消息頭
          ? PUT????? 請求存儲資源,并用URI作為其標識
          ? POST???? 請求服務器接收信息
          ? CONNECT? ?
          ? TRACE???
          ? DELETE
          ? OPTIONS


          二.HTTP響應:
          ? 1).狀態行
          ? 2).消息頭
          ? 3).空行
          ? 4).正文(資源的內容,比如index.htm文件的文本內容)


          1.狀態行
          ? HTTP版本信息 狀態碼 響應碼描述
          ? 例: HTTP/1.1 200 OK

          2.狀態碼(第一位表示響應的類別)
          ? 1xx:
          ? 2xx:
          ? 3xx:
          ? 4xx:
          ? 5xx:
          HTTP協議狀態碼具體意義
          ?? 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. 響應頭
          4. 實體頭

          格式:(名字大小寫無關)
          <名字>:<空格><值>

          1.普通頭
          ? .Cache-Control? (HTTP1.1,? HTTP1.0:Pragma)
          ????? 緩存指令:
          ????? 請求時: no-cache,no-store,max-age,max-stale,min-fresh,only-if-cached
          ????? 響應時: public,private,no-cache,no-store,no-transform,must-revalidate,proxy-revalidate,max-age,s-maxage.
          ????? 例: Cache-Control: no-cache
          ? .Date
          ????? 客戶端:在發送正文時要包含Date,
          ????? 服務器:在響應時包含Date.
          ? .Connection
          ? .Pragma(1.0用)

          2. 請求頭
          ? .Accept
          ? .Accept-Charset
          ? .Accept-Encoding
          ? .Accept-Language
          ? .Authorization
          ? .Host(必須的)
          ? .User-agent

          3.響應頭
          ? .Location
          ? .Server
          ? .WWW-Authenticate,要包含在401中.

          4.實體頭
          ? .Content-Encoding
          ? .Content-Language
          ? .Content-Length
          ? .Content-Type
          ? .Last-Modified
          ? .Expires

          ?

          posted @ 2006-09-28 15:53 2195113 閱讀(245) | 評論 (0)編輯 收藏

          簡單的DOM(java)的操作

          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="計算機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","計算機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.");
          ?????????????? ?
          ??? }
          }

          posted @ 2006-09-26 14:36 2195113 閱讀(194) | 評論 (0)編輯 收藏

          關于ThreadLocal的使用

          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");
          ??????
          ?????? //延時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.測試結果:
          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.結果說明:雖然各線程使用的是同一個對象的引用,但由于使用了ThreadLocal,實際上每個線程所操作的數據是不一樣的.

          posted @ 2006-09-25 16:18 2195113 閱讀(226) | 評論 (0)編輯 收藏

          Base64編碼原理(隨記)

          1. Base64使用A--Z,a--z,0--9,+,/ 這64個字符.
          2. 編碼原理:將3個字節轉換成4個字節( (3 X 8) = 24 = (4 X 6) )
          ??????????? 先讀入3個字節,每讀一個字節,左移8位,再右移四次,每次6位,這樣就有4個字節了.
          3. 解碼原理:將4個字節轉換成3個字節.
          ??????????? 先讀入4個6位(用或運算),每次左移6位,再右移3次,每次8位.這樣就還原了.

          posted @ 2006-09-21 16:29 2195113 閱讀(1000) | 評論 (0)編輯 收藏

          proxool連接池的配置

          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. 程序調用
          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();
          }



          posted @ 2006-08-03 14:03 2195113 閱讀(638) | 評論 (0)編輯 收藏

          主站蜘蛛池模板: 蕲春县| 桓仁| 大港区| 汝南县| 漠河县| 肃北| 永顺县| 志丹县| 勃利县| 团风县| 万源市| 会东县| 哈密市| 涿鹿县| 乌审旗| 黑山县| 深州市| 襄汾县| 石家庄市| 高平市| 介休市| 克拉玛依市| 长宁区| 彩票| 宁南县| 华安县| 卢龙县| 资源县| 阜宁县| 古蔺县| 合江县| 收藏| 辽源市| 贵定县| 连江县| 蒙山县| 岐山县| 麦盖提县| 瓮安县| 报价| 鄂温|