The university studied three. Net, and because of the occasional opportunity to get IBM's Java Web Internship offer, so decided to switch to Java (Integrated school enrollment, development prospects and other factors).
Here are some of my learning notes on the Java Web (may be messy, I hope to be able to do a memo, if you can help that better)
servlet-related--------------------------
1.Servlet Life cycle:
The servlet life cycle is divided into three phases:
1, initialization phase: Call Init () method
2, responding to customer request phase: Call Service () method
The Service () method internally determines the type of request (Get/post) and automatically calls Dopost/doget
3, termination phase: Call Destroy () method
2.Servlet Single-threaded:
Singleton: A servlet is instantiated only when the user first requests it, and is a singleton that is destroyed when the server restarts or shuts down.
Multithreading: servlet container (Tomcat ...) when a request arrives The service method is executed through the threads available in the thread pool to the requestor.
Java Basic related-----------------------
1. Multithreading
Two methods of thread creation:
The first, the implementation of the Runnable interface
Packagetest. Thread;Importorg.junit.Test;//This example shows the "the" to create new thread. The Java file "MyThread" shows the other method. Public classnewthread{@Test Public voidFun () {Runnablethread RT=NewRunnablethread (); Thread T1=NewThread (RT, "First"); Thread T2=NewThread (RT, "Second"); T1.start (); T2.start (); }}classRunnablethreadImplementsrunnable{@Override Public voidrun () {//TODO auto-generated Method Stub for(inti=0;i<100;i++) {System.out.println (Thread.CurrentThread (). GetName ()); } } }
The second type, inherit the thread class
Packagetest. Thread; Public classMyThreadextendsthread{//The constructor without parameter PublicMyThread () {}//The constructor with name parameter PublicMyThread (String name) {Super(name); } Public voidrun () { for(inti=0;i<100;i++) {System.out.println ( This. GetName ()); } }}
Synchronization of Threads
Use the sync lock synchronized, see the ticket sales procedure. The synchronized object must be the same object.
Packagetest. Thread;Importorg.junit.Test; Public classthread_synchronized { Public Static voidMain (string[] args) {Synchronizedrunnablethread srt=NewSynchronizedrunnablethread (); Thread T1=NewThread (SRT, "Window1")); Thread T2=NewThread (SRT, "Window2")); Thread T3=NewThread (SRT, "WINDOW3")); T1.start (); T2.start (); T3.start (); }}classSynchronizedrunnablethreadImplementsrunnable{Private Static inttickets=100; @Override Public voidrun () { while(true){ //We can use the definition of class,because it ' s unique /*synchronized (this) {if (tickets>0) {System.out.println (Thread.currentt Hread (). GetName () + "is selling the" + (tickets--) + "th ticket"); } } */ //or we can use the other method--synchronized methodselltickets (); } } Private synchronized voidselltickets () {if(tickets>0) {System.out.println (Thread.CurrentThread (). GetName ()+ "is selling the" + (tickets--) + "th ticket"); } }}
2.IO Stream
Four streams:
InputStream, OutputStream for arbitrary objects (binary format)
Writer, Reader for character objects (character format)
Examples of Use:
Packagetest. Stream;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;ImportJava.io.FileReader;ImportJava.io.FileWriter; Public classFileinputstream_fileoutputstream { Public Static voidMain (string[] args)throwsexception{//The difference of "FileInputStream" and "FileReader" is a "fileinputstream" read the file with Byte,//but the "FileReader" read with unicode,in other words, "filereader" can read Chinese word. FileInputStream is =NewFileInputStream ("D:\\read.txt"); FileOutputStream OS=NewFileOutputStream ("D:\\fileoutputstream.txt"); intCH =Is.read (); while(Ch!=-1) {os.write (CH); System.out.print ((Char) ch); CH=Is.read (); } os.flush (); Os.close (); Is.close (); }}
Packagetest. Stream;Importjava.io.FileNotFoundException;ImportJava.io.FileReader;ImportJava.io.FileWriter; Public classFilereader_filewriter { Public Static voidMain (string[] args)throwsexception{FileReader FR=NewFileReader ("D:\\read.txt"); FileWriter FW=NewFileWriter ("D:\\write.txt"); intCH =Fr.read (); while(Ch!=-1) {fw.write (CH); CH=Fr.read (); } fw.flush (); Fw.close (); Fr.close (); }}
3. Collection classes
JSP related-----------------------------
1.jsp Working principle:
When a JSP file is requested for the first time, Tomcat first converts the JSP file into a Java source file. During the conversion process, if a JSP file is found to have syntax errors, the conversion process is interrupted and an error message is output to the server and the client. If the conversion succeeds, Tomcat compiles the Java source file into the appropriate. class file with Javac and loads the. class file into memory. (By looking at the original file, it is known that the JSP is eventually transformed into a Servlet,.java is a servlet)
2.jsp nine large built-in objects?
Request client requests, this request will contain parameters from the Get/post request
Response Web page back to the client's response
PageContext properties of a Web page
session information about a request
Application
output used to transmit a response
config accesses the initialization parameters of the servlet instance
page
Exception
3.JSTL Label
1. Expression control Tags: out,set, remove, catch
2. Process Control Label:if, choose, when, otherwise
3. Loop Label:ForEach, Fortokens
4.URL action tag: import, URL, redirect
4.EL-expression
c#/. NET to Java Learning notes