C#/.NET转Java学习笔记

时间:2023-03-09 01:26:39
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.集合类

C#/.NET转Java学习笔记

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表达式

C#/.NET转Java学习笔记