• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 432
  • Last Modified:

Tomcat and Ant problems


Must say that i am very beginner to java and i have to write web app. using tomcat, servlets as business logic, applets as front-end and Ant like make in C. I know some tomcat, have few servlets working in my web app. and applets are not the problem.
Problem is that i dont know how to make directory structure on tomcat. I have something like this:
  -client (applets and dialog forms)
  -common (classes that i use for serialisation and communication between servlets and applets)
  -server (servlets + jdbc access)
Now i have heard that in the web dir i should have only files that go to client like gif, html, applet classes etc.
And i need to have WEB-INF/classes dir inside this 'web' dir. But when i use Ant for build, directory structure on the 'web' and 'src' dir dont match and Ant is recompilling all files - because i need to have client classes in web/ and server in web/WEB-INF/classes. What should i do?
  • 3
  • 2
1 Solution
In your build.xml file, you need to do something like this:
<target name="compile" depends="init">
  <javac srcdir="myapp/src" destdir="web/applet"/>
  <javac srcdir="myapp/server"  
  <javac srcdir="myapp/server"

Your structure are mostly OK except that you missed a "distribution" directory. If you look at some build.xml sample in the apache ant webiste "http://ant.apache.org/manual/index.html", you will find that normally there are 3 directories: source, build and dist. "Source" directory is your development area and they dont contain any class files or jar files. You compile all class files, web static files, web.xml etc to your "build" directory which completedly make a web application. You configure your tomcat's server.xml to point to this directory for your testing. In "distribution" area it contains a single jar/war file which allows you to easily deploy it on any other Tomcat instance.
rrashoAuthor Commented:
Thanks Victor. I need to clarify it little bit more.
If i make distribution dir, where would it be? How could i call my servlets if they are jar-ed in dist dir?
Does build dir have structure like i have with web/applets and web/WEB-INF...?? Maybe then i would have to add /build/ or /dist/ in the paths or change web.xml for servlet url-patterns?
Hi rrasho,

The "web" directory your named before is what i mentioned the "build" directory here.

You may put your distribution dir under "myapp" directory and called it say "distDir". In your ant build.xml file, you add a task called "distTask", this task jar/war all files/directories under "build" directory to one file say "myapp.war" and copy this file to "distDir". So the distribution dir is only for you to store distribution files as war/jar format. You may use time stamp or version number for different versions of your distrubutions.

Your web.xml should not know anything about these directory name "build" or "distDir". This configuration is done by your tomcat server through context path. In your server's configuration file "server.xml", if you put <context path="whatever_contextPath_you_want" docBase="../myapp/build">.
You can access your servlet use the url like "http://localhost:8080/whatever_contextPath_you_want/sales/servlet1". In your web.xml, you set the servlet url pattern as "/sales/servlet1" or "/purchase/servlet2".

When you need to deploy your application for your client,
you can copy the "myapp.war" to your client's server. Unwar it and you get everything exactly the same as those under "build" directory. Configure Tomcat as above and this it! Another easier way to do the deployment is copy "myapp.war" to Tomcat's "webapps" directory and restart. Tomcat will automaticall deploy it. In this way the context path is decided by Tomcat which is "myapp".

rrashoAuthor Commented:
Thanx fot the help victor, i hope 20 points will be ok.
Thanks rrasho.

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now