C#/.NET轉Java學習筆記,

來源:互聯網
上載者:User

C#/.NET轉Java學習筆記,

大學研究了三年的.Net,由於偶然的機會,拿到IBM的Java web實習offer,所以決定轉行搞Java(綜合了校招情況、發展前景和其他各種因素)。

下面是我在學習Java web的一些學習筆記(可能會比較亂,希望能做個備忘,如果能對您有協助那就更好了)

 

Servlet相關--------------------------

1.Servlet的生命週期:

Servlet生命週期分為三個階段:

  1,初始化階段:調用init()方法

  2,響應客戶要求階段:調用service()方法

            Service()方法內部對請求的類型(get/post)進行了判斷,自動調用doPost/doGet

  3,終止階段:調用destroy()方法

2.Servlet的單例多線程:

單例:Servlet只在使用者第一次請求時被執行個體化,並且是單例的,在伺服器重啟或關閉時才會被銷毀。

多線程:當請求到達時,Servlet容器(Tomcat...)通過線程池中可用的線程給要求者並執行Service方法。

 

Java基礎相關-----------------------

1.多線程

線程建立的兩種方法:

第一種,實現Runnable介面

package test.Thread;import org.junit.Test;//This example shows the two method to create new thread.The java file "MyThread" shows the other method.public class NewThread{    @Test    public void Fun(){        RunnableThread rt = new RunnableThread();        Thread t1 = new Thread(rt,"First");        Thread t2 = new Thread(rt,"Second");                t1.start();        t2.start();    }}class RunnableThread implements Runnable{    @Override    public void run() {        // TODO Auto-generated method stub        for(int i=0;i<100;i++){            System.out.println(Thread.currentThread().getName());        }    }    }

第二種,繼承Thread類

package test.Thread;public class MyThread extends Thread{    //The constructor without parameter    public MyThread(){            }        //The constructor with name parameter    public MyThread(String name){        super(name);    }        public void run(){        for(int i=0;i<100;i++){            System.out.println(this.getName());        }    }}

線程的同步

使用同步鎖synchronized,參見賣票程式。同步的對象必須是同一個對象。

package test.Thread;import org.junit.Test;public class Thread_Synchronized {        public static void main(String[] args){        SynchronizedRunnableThread srt = new SynchronizedRunnableThread();                Thread t1 = new Thread(srt,"window1");        Thread t2 = new Thread(srt,"window2");        Thread t3 = new Thread(srt,"window3");                t1.start();        t2.start();        t3.start();    }}class SynchronizedRunnableThread implements Runnable{        private static int tickets=100;            @Override    public void run() {        while(true){            //We can use the definition of class,because it's unique                        /*            synchronized(this){                if(tickets>0){                    System.out.println(Thread.currentThread().getName()+" is selling the "+(tickets--)+"th ticket");                }                        }            */            //or we can use the other method--synchronized method            sellTickets();                    }    }    private synchronized void sellTickets() {        if(tickets>0){            System.out.println(Thread.currentThread().getName()+" is selling the "+(tickets--)+"th ticket");        }        }}

2.IO流

四大流:

InputStream、OutputStream  用於任意對象(二進位格式)

Writer、Reader          用於字元對象(字元格式設定)

使用樣本: 

package test.Stream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileReader;import java.io.FileWriter;public class FileInputStream_FileOutputStream {    public static void main(String[] args) throws Exception{        //The difference of "FileInputStream" and "FileReader" is that "FileInputStream" read the file with byte,        //but "FileReader" read with Unicode,in other words,"FileReader" can read Chinese word.                FileInputStream is = new FileInputStream("D:\\read.txt");        FileOutputStream os =new FileOutputStream("D:\\FileOutputStream.txt");                int ch = is.read();        while(ch!=-1){            os.write(ch);            System.out.print((char)ch);            ch = is.read();        }        os.flush();                os.close();        is.close();    }}
package test.Stream;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;public class FileReader_FileWriter {    public static void main(String[] args) throws Exception{        FileReader fr = new FileReader("D:\\read.txt");        FileWriter fw =new FileWriter("D:\\write.txt");                int ch = fr.read();        while(ch!=-1){            fw.write(ch);            ch = fr.read();        }        fw.flush();                fw.close();        fr.close();            }}

3.集合類

 

jsp相關-----------------------------

1.jsp工作原理:

  當一個JSP檔案第一次被請求的時候,Tomcat首先會把這個JSP檔案轉換成一個Java源檔案。在轉換過程中如果發現JSP檔案有語法錯誤,轉換過程將中斷,並向服務端和用戶端輸出出錯資訊。如果轉換成功,Tomcat用javac把該Java源檔案編譯成相應的.class檔案並將該.class檔案載入到記憶體中。(通過查看原檔案,可知jsp最終也是轉化被成Servlet,.java就是一個Servlet)

 

2.jsp九大內建對象?

  request          使用者端請求,此請求會包含來自GET/POST請求的參數 
  response        網頁傳回使用者端的回應 
  pageContext   網頁的屬性 
  session       與請求有關的會話資訊 
  application           
  out             用來傳送回應的輸出 
  config                 存取servlet執行個體的初始化參數 
  page                  
  exception     

 

3.JSTL標籤

  1.運算式控制標籤:out、set、remove、catch

  2.流程式控制制標籤:if、choose、when、otherwise

  3.迴圈標籤:forEach、forTokens

  4.URL操作標籤:import、url、redirect

 

4.EL運算式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.