Reputation: 637
I'm working on ubuntu server 14.04 with tomcat 7 and jdk 8. I'm new to java servlets. So I red this tutorial http://www.tutorialspoint.com/servlets/servlets-first-example.htm, and did the same (but not in ROOT - in TESTAPP), but when I try http://localhost:8080/TESTAPP/HelloWorld i get 404 error. Directory hierarchy:
/var/lib/tomcat7/webapps/TESTAPP/
|
-- index.html
-- META_INF/
-- WEB_INF/
|
----- web.xml
----- classes/
|
-------- HelloWorld.java
-------- HelloWorld.class
index.html:
<h1>TESTAPP</h1>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="TESTAPP"
version="3.0">
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>
</web-app>
HelloWorld.java:
// Import required java libraries
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
// Extend HttpServlet class
public class HelloWorld extends HttpServlet {
private String message;
public void init() throws ServletException
{
// Do required initialization
message = "Hello World";
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
// Set response content type
response.setContentType("text/html");
// Actual logic goes here.
PrintWriter out = response.getWriter();
out.println("<h1>" + message + "</h1>");
}
public void destroy()
{
// do nothing.
}
}
then i did
export CLASSPATH=/usr/share/tomcat7/lib/servlet-api.jar
javac HelloWorld.java
sudo service tomcat7 restart
catalina.out: http://pastebin.com/raw.php?i=5SM3vatg
so then in browseer (or with curl) for http://localhost:8080/TESTAPP i get OK for http://localhost:8080/TESTAPP/HelloWorld i get NOT FOUND.
So where is my mistake? Please help.
Upvotes: 1
Views: 2053
Reputation: 323
The directory where Tomcat deploys web applications is named "webapps" and not "webapp" as appears on your directory hierarchy.
Upvotes: 1