C #/. NET to Java learning notes,

Source: Internet
Author: User

C #/. NET to Java learning notes,

The University has been studying for three years. net, due to the occasional opportunity to get IBM Java web practice offer, so decided to switch to Java (integrated with the school recruitment situation, development prospects and other factors ).

Below are some of my learning notes on Java web (it may be messy and I hope I can make a memo. It would be better if I could help you)

 

Servlet-related --------------------------

1. Servlet lifecycle:

Servlet lifecycle is divided into three phases:

1. Initialization phase: Call the init () method

2. response to customer requests: Call the service () method

The Service () method internally determines the request type (get/post) and automatically calls doPost/doGet

3. termination phase: Call the destroy () method

2. Servlet Singleton multithreading:

Singleton: The Servlet is instantiated only when the user requests for the first time and is a singleton. It will be destroyed only when the server is restarted or shut down.

Multithreading: when a request arrives, the Servlet container (Tomcat...) sends the request to the requester through the available threads in the thread pool and executes the Service method.

 

Java basics -----------------------

1. Multithreading

Two ways to create a thread:

First, implement the Runnable interface

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());        }    }    }

Second, inherit the Thread class

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());        }    }}

Thread Synchronization

Use the Synchronous lock synchronized. For more information, see ticket selling program. The synchronized object must be the same object.

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 stream

Four major streams:

InputStream and OutputStream are used for any object (in binary format)

Writer and Reader are used for character objects (character format)

Example:

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. Collection class

 

Jsp related -----------------------------

1. jsp Working principle:

When a JSP file is requested for the first time, Tomcat will first convert the JSP file into a Java source file. If a syntax error is found in the JSP file during the conversion process, the conversion process is interrupted and an error message is output to the server and client. If the conversion is successful, Tomcat uses javac to compile the Java source file into the corresponding. class file and load the. class file into the memory. (By viewing the original file, we can see that jsp is eventually converted to Servlet, And. java is a Servlet)

 

2. What are the nine built-in jsp objects?

Request client request, which contains parameters from the GET/POST request
Response returned from the response webpage to the client
PageContext webpage attributes
Session information related to requests
Application
Out is used to send response output.
Config to access the initialization parameters of the servlet instance
Page
Exception

 

3. JSTL tag

1. Expression control labels: out, set, remove, and catch

2. Process Control labels: if, choose, when, and otherwise

3. Loop labels: forEach and forTokens

4. URL operation tags: import, url, redirect

 

4. EL expression

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.