Reputation: 347
I'm developing a Servlet using Tomcat Servlet Engine(version 6.0)
I'm a completely newbie about this stuff, so I'm reading some stuff on the net. Now I create my servlet folder under:
/var/lib/tomcat6/webapps/ROOT/myapp
under this I create WEB-INF folder and classes one. So I have this hierarchy:
/var/lib/tomcat6/webapps/ROOT/myapp/WEB-INF/classes
I'm trying to execute this simple servlet:
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 TestServlet 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!</h1>");
out.println("</body>");
out.println("</html>");
}
}
after compiling I put .class file, of course under classes directory, and then I created web.xml file under WEB-INF directory, here't the content:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<description>
Servlet and JSP Examples.
</description>
<display-name>Servlet and JSP Examples</display-name>
<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
</web-app>
Then I simply go to:
http://localhost:8080/myapp/test
but i get 404 error.
What can I do? Thanks in advance
Upvotes: 0
Views: 3836
Reputation: 1199
You need not move the Web Application project under Root folder. Keep it as :
/var/lib/tomcat6/webapps/myapp
Also, make a correction for method signature its doGet(HttpServletRequest request, HttpServletResponse response) {}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException {
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("<h1>Hello Servlet!</h1>");
out.println("</body>");
out.println("</html>");
}
That's all, it should work now!
Upvotes: 1
Reputation: 12774
Move the folder from ROOT to webapp, folder structure should be
/var/lib/tomcat6/webapps/myapp
Upvotes: 2