user590849
user590849

Reputation: 11765

What happens when I run an application on tomcat in Eclipse

I am making a java webapplication using eclipse and tomcat server. I want to know what happens when I run my website on Tomcat ? What are the steps Eclipse does in the background to run the application on tomcat.

This will help me understand when to switch off the server (while debugging) / when to clean the server etc.

I need to know what goes into the server so that I can get better at debugging.

Right now all I do is restart the server everytime something goes wrong. I have wasted enough time doing that. I guess I need to invest a little bit more time in understanding what happens behind the scenes.

Upvotes: 6

Views: 4183

Answers (1)

Pavel Horal
Pavel Horal

Reputation: 18194

Environment assumptions

I will assume:

  • target/classes is the target folder for compiled classes
  • src/main/webapp is the web application content folder
  • Project > Build Automatically option is checked

Deployment directory

Eclipse is using exploded WAR deployment - i.e. the deployed application is deployed as a folder, not a single file archive. Application files are placed and loaded from ${workspace}/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/.

Publishing

Publishing is a central process which is responsible for assembling and deploying the web application. When talking about local Tomcat, this means copying "web content, compiled classes, libraries, ..." into deployment directory (the one in .metadata).

Eclipse is able to do partial publishing - i.e. when a single resource changes (e.g. some JSP), Eclipse will publish only that single file.

By default publish process is performed automatically when some resource changes. This can be modified in server settings (double click on the server name in Servers view).

Publishing setting

Changing static resource

If you change lets say src/main/webapp/resources/myApp/css/main.css:

  • upon publish the file gets copied to the deployment folder
  • resource is instantly available to server clients

Changing JSP file

If you change JSP file:

  • upon publish the file gets copied to the deployment folder
  • Tomcat notices that the JSP file has changed and recompiles it
  • changed JSP is ready to render content

Changing Java file

If you change a java source file:

  • the file gets compiled into target/classes
  • upon publish the file gets copied to the deployment folder
  • Tomcat notices that a class file was changed and reloads the context (i.e. web application is restarted)

You can turn of the auto-reloading feature in server settings on the Modules tab. Without auto-reloading you can still use hot swap feature, which is able to replace code in running JVM. This is possible only when method signatures are not changed.

Auto reloading feature

If you want more advanced solution (i.e. not limited to changing just a method body) when it comes to reloading java changes, you should check projects like JRebel (not free).

Cleaning

Deployed application can get corrupted. It is worth noting, that when you want to clean completely compiled and published resources, you should:

  • Clean the compiled classes (Project > Clean... - deletes target/classes)
  • Clean the deployed files (Server > Clean... - deletes deployment folder)
  • Clean Tomcat working directory (Server > Clean Tomcat Work Directory... - deletes compiled JSPs)

Upvotes: 18

Related Questions