Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 848
user590849 Avatar asked Jul 06 '13 23:07

user590849


People also ask

What does Tomcat application server do?

Tomcat is normally defined as a reference implementation of the Java Servlet and the Java Server Page (JSP) Specifications. It basically executes Java servlets and renders web pages which include JSP coding. It is available on the Apache site in both source and binary versions.

What happens when we deploy a WAR file in Tomcat?

Java web applications are usually packaged as WAR files for deployment. These files can be created on the command line or with an IDE, like Eclipse. After deploying the WAR file, Tomcat unpacks it and stores all the project files from the webapps directory in a new directory named after the project.

How do I use Tomcat in Eclipse?

For configuring the tomcat server in eclipse IDE, click on servers tab at the bottom side of the IDE -> right click on blank area -> New -> Servers -> choose tomcat then its version -> next -> click on Browse button -> select the apache tomcat root folder previous to bin -> next -> addAll -> Finish.

How do I know if Tomcat is running in Eclipse?

Go to the project in the Project Explorer, select the web project, right click and select "Run As", you will see Apache Tomcat, create runtime configuration for the web project by selecting "New" button.


1 Answers

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)
like image 90
Pavel Horal Avatar answered Sep 20 '22 21:09

Pavel Horal