简体   繁体   中英

How to automate the process of deploying a servlet using Eclipse and Tomcat?

I'm developing a web service application, and although the services are operating like they should, I think the time I take to develop, test and fix my code are being wasted. Here is what I'm doing:

  1. I have a normal java project in Eclipse, where I have all my code for the web service. I added a javax.servlet.jar library to the build path, and receive the requests using service(HttpServletRequest req, HttpServletResponse resp) from HttpServlet , and print the result in JSON format. I use the main method to test and fix some part of the code

  2. When I feel confident that my code is fine I navigate to the workspace, copy all the class files, stop tomcat server, delete the old files deployed at web-inf, paste the new class files and start the tomcat server.

My question is: Is there a faster way to do (ii) so I can iterate faster in the process of programming/testing/fixing? I have seen that I can achieve what I want using Dynamic Web Project, but I fear it might be difficult to migrate and cause problems with SVN.

Thank you!

There a lot of ways to achieve that.

You could for example configure your java project to output .class files in to the WEB-INF\\classes of the docbase of the web-app hosting your web services.

right click on your project > Build path > configure build path > source

an then browse to the folder you want.

Start your tomcat from eclipse using tomcat eclipse plugin . It starts it by default in debug mode.

Or you could add - at least - the facet Dynamic Web Module to your project under

right click on your project > properties > Project Facets

and choose tomcat as runtime (click on new... to create a new one if not already done). In that case you can remove the extra jar you have for HttpServlet because the new runtime adds all the libs you need. After doing that you can just run project on the server using

right click > run as > run on server 

and then choosing a server on which you want your project to run on. You can run in debug mode using debug as instead of run as .

In both cases you can take advantage from the Hot swap abilities of the jvm . The server will only need to be restarted if it is not possible to hot swap the code, ie whe you change a method signature or add some fields to class.

I like to use Sysdeo tomcat plugin for eclipse. http://www.eclipsetotale.com/tomcatPlugin.html it lets you point tomcat to use your target directory in your project as a webapp. Allowing for hotswap code replacement and easy start, stop and restart. Start by downloading the plugin from the link above then making a file ROOT.xml in this sub directory of your tomcat instillation conf\\Catalina\\localhost\\

The content would look something like this

<?xml version="1.0" encoding="UTF-8"?>
<Context docBase="C:\workdir\myapp\src\main\webapp" path="/"/>

Then you can add that projects library dependencies to tomcats classpath as well by going to eclipse->properties->tomcat->advanced also eclipse->properties->tomcat->source path and checking the box next to your project

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM