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容器()通过线程池中可⽤的线程给请求者并执⾏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.Name());
}
}
}
线程的同步
使⽤同步锁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){学习java的学习方法
//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:\\");
FileOutputStream os =new FileOutputStream("D:\\");
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:\\");
FileWriter fw =new FileWriter("D:\\");
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表达式
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论