Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to add java startup commands to tomcat services

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "how to make tomcat services add java startup commands". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to make tomcat services add java startup commands" can help you solve the problem.

Existing code:

1. Main function: mainGame.java (picking function started by the game.)

two。 Framework run class: gameEnterFrame.java (responsible for loop execution, which I set to run every 2 seconds and write numbers in the database.)

There are two key points about self-startup:

1. You need to modify a configuration file named web.xml

In WEB-INF under webRoot.

If you do not have the same path as my picture, it is a pity that your project type is not created correctly.

Remember to create a new web server project.

This file simply adds three lines of code, telling tomcat that I'm going to run a self-starting class, and I'll name it autoRun. As shown in the following figure, the blue part is the code I added.

Listener-classgame.autoRun/listener-class

/ listener runtime can execute the autoRun class under the game package (the game package is my own game package, and you can create the name of the package you like). This autoRun class is the self-startup code I wrote.

How to write it, take a look at the following:

two。 How to write the self-startup code:

We need to get the self-startup code to the main function. So, under the game package, create a new file called autoRun.java

Packagegame

Import javax. Servlet. SERVLETContextEvent;// this is a self-starting class, server background event

Import javax. Servlet. Servlet context listener;// this is a self-starting class with a server monitor in the background

Import game. Main game;// we import the main functions for easy operation.

/ / declare an autoRun class that uses the server background listening interface. Fixed usage, rote memorization

Publicclass autorunelementsservletcontextlistener {

/ / when the background is initialized, the event of tomcat startup occurs, which is a fixed usage

Public void context initialized (servletcontexteventarg 0) {

/ / what you have to do, write it here

System. Out. Println ('main functionis running,')

Maingame. Main (null)

}

/ / when the backend terminates, a tomcat shutdown event occurs, which is a fixed usage

Public void context destored (servletcontexteventarg 0) {

/ / the execution content is written here.

}

As you can see, there are two parts to monitoring the startup and shutdown status of tomcat.

Here we go. What should I do

The other one is closed. What should I do

Of course it's off. I don't need to do anything yet. I just need to perform the main functions of the game after startup. So I left the main function on startup.

This is the end of the introduction to "how to add java startup commands to tomcat services". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report