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
Recently I need to change the version of MySQL da...
MySQL installation tutorial, for your reference, ...
Table of contents Preface 1. Basic Environment 1....
This question originated from a message on Nugget...
Table of contents 1. Insert statement 1.1 Insert ...
Recently I saw the article Build your own React o...
1. Service method Check the firewall status: [roo...
The day before yesterday, I encountered a problem...
MySQL 5.7.27 detailed download, installation and ...
Table of contents Preface Creating Components Sum...
【question】 We have an HP server. When the SSD wri...
This article uses examples to illustrate common b...
Table of contents Class Component Functional Comp...
Table of contents 1. Introduction to priority que...
3. MySQL Data Management The first method: not re...