What happens when I run an application on tomcat in Eclipse

后端 未结 1 1607
深忆病人
深忆病人 2020-12-05 09:18

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 backg

相关标签:
1条回答
  • 2020-12-05 09:43

    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)
    0 讨论(0)
提交回复
热议问题