My first server program I'm currently learning to write online games, so I need to write server-side programs. I looked around for PHP, JAVA, and C. Finally, I chose Java for its compatibility with Alibaba Cloud and Tencent Cloud, low cost, and low learning difficulty. Then start learning how to write java classes. And how to connect to the database and how to run the code every few seconds. After all, these two combined can become the simplest server. My first program is very simple. After Tomcat is started, it runs every 6 seconds to generate a set of random numbers and send them to the MySQL database. Since writing to the database and timing operations have been introduced in previous articles. Therefore, this article mainly introduces how to start automatically. Existing code: 1. Main function: mainGame.java (the function that starts the game.) 2. Frame running class: gameEnterFrame.java (responsible for loop execution. I set it to run once every 2 seconds and write the number to the database.) There are two key points about self-starting: 1. You need to modify a configuration file called web.xml In WEB-INF under webRoot. If you don't have the same path as mine, unfortunately, that means you created the wrong project type. Remember to create a new web server project. Simply add three lines of code to this file to tell Tomcat that I want to run a self-starting class, which I named autoRun. As shown in the figure below, the blue part is the code I added. For your convenience, paste it here. <listener> <listener-class>game.autoRun</listener-class> </listener> With this monitoring statement, you can execute the autoRun class under the game package (the game package is a game class package I created myself, you can create the name of your favorite package) at runtime. This autoRun class is the self-starting code I wrote. How to write it specifically, see below: 2. How to write the self-starting code: We need to have the self-starting code lead out to the main function. So under the game package, create a new file named autoRun.java package game; import javax.servlet.ServletContextEvent; //This is the class used for self-start, server background event import javax.servlet.ServletContextListener; //This is the class used for self-start, server background listening import game.mainGame; //We import the main function for easy running //Declare an autoRun class and use the server background listening interface. Fixed usage, rote memorization public class autoRun implements ServletContextListener { //When the backend is initialized, that is, the tomcat startup event occurs, fixed usage public void contextInitialized(ServletContextEvent arg0){ //Write what you need to do hereSystem.out.println("MainFunction is running."); mainGame.main(null); } //When the backend is destroyed, that is, Tomcat is closed, fixed usage public void contextDestroyed(ServletContextEvent arg0){ //Write the execution content here} } As you can see, there are two parts in monitoring the startup and shutdown status of tomcat.
Of course, it's closed, and I don't need to take any action right now. I just need to execute the main function of the game after startup. So I put the main function in the startup. The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: How to quickly clean up billions of data in MySQL database
>>: The use and difference between JavaScript pseudo-array and array
This article uses examples to illustrate the prin...
<frameset></frameset> is familiar to e...
@vue+echarts realizes the flow effect of China ma...
First, let’s understand what MySQL is? MySQL is a...
If your computer is a Mac, using homebrew to inst...
This article example shares the specific code of ...
Have you ever encountered a situation where we hav...
Table of contents 1. Routing and page jump 2. Int...
Remark: The amount of data in this article is 1 m...
Preface As we all know, everything in Linux is a ...
In the MySQL documentation, MySQL variables can b...
npx usage tutorial Tonight, when I was learning V...
Table of contents Install: 1. Basic use of firewa...
Preface: Basically, whether it is for our own use...
This article shares the specific code of JavaScri...