Reputation: 6342
Trying to learn Ant/Tomcat and I'm running into some difficulty deploying my war. I'm getting no errors in catalina, localhost, or localhost_access logs so I'm confident its just a configuration issue. Ant build runs fine. Deploys the war. When I go to localhost:8080/antDemo/hello, I get a 404. Code below.
One thing that is strange to me is that when I navigate to tomcat/webapps/antDemo I only see the HelloServlet.java file and a META-INF folder when I'd expect to see a web.xml and a classes folder or something like that.
I've read many stack overflow questions (whilst feeling like a moron for not being able to sort it out) but with no luck.
Help would be nice but even more so, some direction toward a comprehensive information resource that not only explains how things like this should be configured but WHY they need to be configured that way. Getting the why has been harder to for me locate...and is the most important part.
Project Structure:
tomcatdemo
|-src
|---HelloServlet.java
|-WebContent
|---WEB-INF
|------classes
|---------HelloServlet.class
|------web.xml
|-build.xml
HelloServlet.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloServlet extends HttpServlet{
public void doGe(HttpServletRequest request, HttpServletResponse response)
throws IOException{
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("<h1>Hello Servlet Get</h1>");
out.println("</body>");
out.println("</html>");
}
}
build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project default="clean" basedir=".">
<property name="tomcat" value="C:\tomcat"/>
<property name="tomcat.deployment" value="${tomcat}/webapps"/>
<property name="tomcat.bin" value="${tomcat}/bin"/>
<property name="base" value="."/>
<property name="source" value="${base}/src"/>
<target name="clean">
<delete file="${tomcat.deployment}/antDemo.war"/>
<antcall target="compile"/>
</target>
<target name="compile">
<javac srcdir="src" destdir="${base}/WebContent/WEB-INF/classes" classpath="C:/tomcat/lib/servlet-api.jar"/>
<antcall target="deployTarget"/>
</target>
<target name="warTarget">
<war warFile="antDemo.war" webxml="${base}/WebContent/WEB-INF/web.xml">
<fileset dir="${source}"/>
</war>
<antcall target="deployTarget"/>
</target>
<target name="deployTarget">
<copy file="${base}/antDemo.war" todir="${tomcat.deployment}" />
<antcall target="startTomcat"/>
</target>
<target name="startTomcat">
<exec executable="${tomcat.bin}/startup.bat">
<env key="CATALINA_HOME" value="${tomcat}"/>
</exec>
</target>
<target name="stopTomcat">
<exec executable="${tomcat.bin}/shutdown.bat">
<env key="CATALINA_HOME" value="${tomcat}"/>
</exec>
</target>
</project>
web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
<display-name>myServletWAR, a first Web Application</display-name>
<description>
</description>
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
EDIT: war structure is as follows (and its clearly wrong)
antDemo
|-META-INF
|----MANIFEST.MF
|-HelloServlet.java
Upvotes: 0
Views: 1581
Reputation: 75426
The problem is
<fileset dir="${source}"/>
You should use
<fileset dir="${base}/WebContent"/>
instead (untested).
Upvotes: 1