In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "how to use java to simulate simple tomcat methods". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
We can now use tomcat, but the specific principle of tomcat is still very vague, so let's use java to briefly simulate tomcat, implement the browser to access port 8888, and then get a web page to show it. Note: this is just a simple simulation
We will then use the knowledge of io and the Internet in java. First of all, to make it simple, the browser accesses port 8888, then outputs a sentence, and the code is as follows
@ Test public void myTomcat () throws IOException {/ / listen on port 8888 ServerSocket serverSocket = new ServerSocket (8888); / / wait to connect while (! serverSocket.isClosed ()) {/ / get socket for communication Socket socket = serverSocket.accept () when the server is not shut down / / get the output stream OutputStream outputStream = socket.getOutputStream (); / / write data outputStream.write ("this is myTomcat" .getBytes ()); / / close the output stream and socket outputStream.close (); socket.close ();} / close the server serverSocket.close ();}
We use firefox to visit, and the website visited by localhost:8888,firefox is shown as follows:
As you can see, it has been successfully realized. Note that firefox must be used, but neither geogle nor edge can be used because they do not parse the data for the tcp protocol.
We further deepen on this basis, so that when the browser accesses the server, the server returns a html file, and then the browser displays it, and the code is as follows
@ Test public void myTomcat () throws IOException {/ / listen on port 8888 ServerSocket serverSocket = new ServerSocket (8888); / / wait to connect while (! serverSocket.isClosed ()) {/ / get socket for communication Socket socket = serverSocket.accept () when the server is not shut down / / get the output stream OutputStream outputStream = socket.getOutputStream (); / / get the input stream BufferedInputStream bufferedInputStream = new BufferedInputStream (new FileInputStream ("src/hello.html")); int len = 0; byte [] bytes = new byte [1024] While ((len = bufferedInputStream.read (bytes))! =-1) {/ / write data outputStream.write (bytes, 0, len);} / / close the output stream and socket outputStream.close (); socket.close ();} / shut down the server serverSocket.close () }
The html file is as follows
Hello this is myTomcat
After running the java file, the browser accesses the localhost:8888, which is displayed as follows
We can see that we have succeeded, but the browser did not parse this as html, not to mention the problem, now we have implemented the simplest tomcat in java, which will go deeper later.
That's all for "how to use java to simulate simple tomcat methods". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.