Tomcat工作原理之运行机制

一、Tomcat运行原理分析
1.Tomcat是运行在JVM中的一个进程。它定义为【中间件】,顾名思义,是一个在Java项目与JVM之间的中间容器。

2.Web项目的本质,是一大堆的资源文件和方法。Web项目没有入口方法(main方法),,意味着Web项目中的方法不会自动运行起来。

3.Web项目部署进Tomcat的webapp中的目的是很明确的,那就是希望Tomcat去调用
写好的方法去为客户端返回需要的资源和数据。
4. Tomcat可以运行起来,并调用写好的方法。那么,Tomcat一定有一个main方法。
5. 对于Tomcat而言,它并不知道我们会有什么样的方法,这些都只是在项目被部署进webapp下后才确定的,由此分析,必然用到了Java的反射来实现类的动态加载、实例化、获取方法、调用方法。但是我们部署到Tomcat的中的Web项目必须是按照规定好的接口来进行编写,以便进行调用

6.Tomcat如何确定调用什么方法呢。这取却于客户端的请求,http://127.0.0.1:8080/JayKing.Tomcat.Study/index.java?show这样的一个请求,通过http协议,在浏览器发往本机的8080端口,携带的参数show方法,包含此方法的路径为JayKing.Tomcat.Study,文件名为:index.java。

 

二、模拟Tomcat运行

1.客户端类

 

[java] view plain copy

  1. <code class="language-java">package JayKing.Tomcat.Study;  
  2. import java.io.IOException;  
  3. import java.io.OutputStreamWriter;  
  4. import java.io.Writer;  
  5. import java.net.Socket;  
  6. import java.net.UnknownHostException;  
  7. import java.util.Scanner;  
  8. public class Client {  
  9.     private static int port = 5228;  
  10.     private static String host = "127.0.0.1";  
  11.     //http://127.0.0.1:8080/JayKing.Tomcat.Study/index.java?show  
  12.     public static void main(String[] args) {  
  13.         try {  
  14.             Socket con=new Socket(host,port);  
  15.             System.out.println("请输入URL地址:");  
  16.             Scanner scanner=new Scanner(System.in);  
  17.             String info=scanner.nextLine().trim();  
  18.             Writer writer = new OutputStreamWriter(con.getOutputStream());  
  19.             writer.write(info);  
  20.             writer.flush();  
  21.             writer.close();  
  22.         } catch (UnknownHostException e) {  
  23.             // TODO Auto-generated catch block  
  24.             e.printStackTrace();  
  25.         } catch (IOException e) {  
  26.             // TODO Auto-generated catch block  
  27.             e.printStackTrace();  
  28.         }  
  29.     }  
  30. }  
  31. </code>  

 

2.服务器类

 
  1. package JayKing.Tomcat.Study;

  2.  
  3. import java.io.BufferedReader;

  4. import java.io.IOException;

  5. import java.io.InputStream;

  6. import java.io.InputStreamReader;

  7. import java.lang.reflect.InvocationTargetException;

  8. import java.lang.reflect.Method;

  9. import java.net.ServerSocket;

  10. import java.net.Socket;

  11.  
  12. public class TomcatTest {

  13. private static int post = 5228;

  14.  
  15. private static UrlUtil urlutil = new UrlUtil();

  16.  
  17. public static void main(String[] args) {

  18. System.out.println(" My Tomcat is Running");

  19. try {

  20. ServerSocket server = new ServerSocket(post);

  21. while (true) {

  22. Socket socket = server.accept();// 服务器每接受一次请求,就创建一个socket对象

  23. InputStream in = socket.getInputStream();

  24. BufferedReader br = new BufferedReader(

  25. new InputStreamReader(in));

  26. String info = null;

  27. String infoline = br.readLine();

  28. while (infoline != null) {

  29. info =info+infoline;

  30. infoline = br.readLine();

  31. }

  32. UrlBean url = urlutil.readString(info);

  33. if (url != null) {

  34. String path=url.getPath();

  35. String className = url.getFileName();

  36. String methodName = url.getParameter().trim();

  37. ClassLoader classloader=ClassLoader.getSystemClassLoader();

  38. try {

  39. classloader.loadClass(path+"."+className);

  40. Class<?> getclass=Class.forName(path+"."+className);

  41. Method method=getclass.getMethod(methodName, null);

  42. method.invoke(getclass.newInstance(), null);

  43.  
  44. } catch (ClassNotFoundException e) {

  45. // TODO Auto-generated catch block

  46. e.printStackTrace();

  47. } catch (NoSuchMethodException e) {

  48. // TODO Auto-generated catch block

  49. e.printStackTrace();

  50. } catch (SecurityException e) {

  51. // TODO Auto-generated catch block

  52. e.printStackTrace();

  53. } catch (IllegalAccessException e) {

  54. // TODO Auto-generated catch block

  55. e.printStackTrace();

  56. } catch (IllegalArgumentException e) {

  57. // TODO Auto-generated catch block

  58. e.printStackTrace();

  59. } catch (InvocationTargetException e) {

  60. // TODO Auto-generated catch block

  61. e.printStackTrace();

  62. } catch (InstantiationException e) {

  63. // TODO Auto-generated catch block

  64. e.printStackTrace();

  65. }

  66. } else {

  67.  
  68. }

  69. }

  70. } catch (IOException e) {

  71. // TODO Auto-generated catch block

  72. e.printStackTrace();

  73. }

  74. }

  75. }

3.工具类

 
  1. package JayKing.Tomcat.Study;

  2.  
  3. //格式:协议://主机号:端口号/目录路径/文件名

  4. //例如: http://127.0.0.1:8080/Test/Manage/index.jsp

  5. public class UrlUtil {

  6. public UrlBean readString(String info) {

  7. UrlBean url = null;

  8. int tag1 = info.indexOf(":");

  9. int tag2 = info.lastIndexOf(":");

  10. int tag3 = info.indexOf("/", tag2);

  11. int tag4 = info.lastIndexOf("/");

  12. int tag5 = info.indexOf("?");

  13. int tag6=info.lastIndexOf(".");

  14. String Protocol = info.substring(0, tag1);

  15. String Host = info.substring(tag1 + 3, tag2);

  16. String Port = info.substring(tag2 + 1, tag3);

  17. String Path = info.substring(tag3 + 1, tag4);

  18. String FileName = info.substring(tag4 + 1, tag6);

  19. String Parameter = info.substring(tag5 + 1, info.trim().length());

  20. if (Host != null && Path != null && FileName != null) {

  21. if (Protocol == null) {

  22. Protocol = "http";

  23. }

  24. if (Port == null) {

  25. Port = "8080";

  26. }

  27. url = new UrlBean(Protocol, Host, Port, Path, FileName, Parameter);

  28. return url;

  29. }

  30.  
  31. return url;

  32.  
  33. }

  34. }

4.Model类

 
  1. package JayKing.Tomcat.Study;

  2.  
  3. //格式:协议://主机号:端口号/目录路径/文件名

  4. //例如: http://127.0.0.1:8080/Test/Manage/index.jsp?a=1&b=2

  5. public class UrlBean {

  6. private String Protocol;

  7. private String Host;

  8. private String Port;

  9. private String Path;

  10. private String FileName;

  11. private String Parameter;

  12.  
  13. public UrlBean(String protocol, String host, String port, String path,

  14. String fileName, String parameter) {

  15. super();

  16. Protocol = protocol;

  17. Host = host;

  18. Port = port;

  19. Path = path;

  20. FileName = fileName;

  21. Parameter = parameter;

  22. }

  23.  
  24. public UrlBean() {

  25. }

  26.  
  27. public String getProtocol() {

  28. return Protocol;

  29. }

  30.  
  31. public void setProtocol(String protocol) {

  32. Protocol = protocol;

  33. }

  34.  
  35. public String getHost() {

  36. return Host;

  37. }

  38.  
  39. public void setHost(String host) {

  40. Host = host;

  41. }

  42.  
  43. public String getPort() {

  44. return Port;

  45. }

  46.  
  47. public void setPort(String port) {

  48. Port = port;

  49. }

  50.  
  51. public String getPath() {

  52. return Path;

  53. }

  54.  
  55. public void setPath(String path) {

  56. Path = path;

  57. }

  58.  
  59. public String getFileName() {

  60. return FileName;

  61. }

  62.  
  63. public void setFileName(String fileName) {

  64. FileName = fileName;

  65. }

  66.  
  67. public String getParameter() {

  68. return Parameter;

  69. }

  70.  
  71. public void setParameter(String parameter) {

  72. Parameter = parameter;

  73. }

  74. }

5.测试方法类

 
  1. package JayKing.Tomcat.Study;

  2. public class index {

  3. public void show() {

  4. System.out.println("方法已经被执行!");

  5. }

  6. }

6.运行结果

 

 

 

 

三、Tomcat原理总结

1. Tomcat需要main方法启动。
2. Tomcat需要监听本机上的某个端口。
3. Tomcat需要抓取此端口上来自客户端的链接并获得请求调用的方法与参数。

4. Tomcat需要根据请求调用的方法,动态地加载方法所在的类,完成累的实例化并通过该实例获得需要的方法最终将请求传入方法执行。

5. 将结果返回给客户端(jsp/html页面、json/xml字符串)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值