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.集合类
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表达式
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?