大學(xué)研究了三年的.Net,由于偶然的機(jī)會(huì),拿到IBM的Java web實(shí)習(xí)offer,所以決定轉(zhuǎn)行搞Java(綜合了校招情況、發(fā)展前景和其他各種因素)。
下面是我在學(xué)習(xí)Java web的一些學(xué)習(xí)筆記(可能會(huì)比較亂,希望能做個(gè)備忘,如果能對(duì)您有幫助那就更好了)
Servlet相關(guān)--------------------------1.Servlet的生命周期:
Servlet生命周期分為三個(gè)階段:
1,初始化階段:調(diào)用init()方法
2,響應(yīng)客戶請(qǐng)求階段:調(diào)用service()方法
Service()方法內(nèi)部對(duì)請(qǐng)求的類型(get/post)進(jìn)行了判斷,自動(dòng)調(diào)用doPost/doGet
3,終止階段:調(diào)用destroy()方法
2.Servlet的單例多線程:
單例:Servlet只在用戶第一次請(qǐng)求時(shí)被實(shí)例化,并且是單例的,在服務(wù)器重啟或關(guān)閉時(shí)才會(huì)被銷毀。
多線程:當(dāng)請(qǐng)求到達(dá)時(shí),Servlet容器(Tomcat...)通過(guò)線程池中可用的線程給請(qǐng)求者并執(zhí)行Service方法。
Java基礎(chǔ)相關(guān)-----------------------1.多線程
線程創(chuàng)建的兩種方法:
第一種,實(shí)現(xiàn)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,參見(jiàn)賣票程序。同步的對(duì)象必須是同一個(gè)對(duì)象。
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 用于任意對(duì)象(二進(jìn)制格式)
Writer、Reader 用于字符對(duì)象(字符格式)
使用示例:
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.集合類

1.jsp工作原理:
當(dāng)一個(gè)JSP文件第一次被請(qǐng)求的時(shí)候,Tomcat首先會(huì)把這個(gè)JSP文件轉(zhuǎn)換成一個(gè)Java源文件。在轉(zhuǎn)換過(guò)程中如果發(fā)現(xiàn)JSP文件有語(yǔ)法錯(cuò)誤,轉(zhuǎn)換過(guò)程將中斷,并向服務(wù)端和客戶端輸出出錯(cuò)信息。如果轉(zhuǎn)換成功,Tomcat用javac把該Java源文件編譯成相應(yīng)的.class文件并將該.class文件加載到內(nèi)存中。(通過(guò)查看原文件,可知jsp最終也是轉(zhuǎn)化被成Servlet,.java就是一個(gè)Servlet)
2.jsp九大內(nèi)置對(duì)象?
request 用戶端請(qǐng)求,此請(qǐng)求會(huì)包含來(lái)自GET/POST請(qǐng)求的參數(shù) response 網(wǎng)頁(yè)傳回用戶端的回應(yīng) pageContext 網(wǎng)頁(yè)的屬性 session 與請(qǐng)求有關(guān)的會(huì)話信息 application out 用來(lái)傳送回應(yīng)的輸出 config 存取servlet實(shí)例的初始化參數(shù) page exception
3.JSTL標(biāo)簽
1.表達(dá)式控制標(biāo)簽:out、set、remove、catch
2.流程控制標(biāo)簽:if、choose、when、otherwise
3.循環(huán)標(biāo)簽:forEach、forTokens
4.URL操作標(biāo)簽:import、url、redirect
4.EL表達(dá)式

新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注