Sorry if this is a common question, but I'm gonna crazy. I'm starting with JSP developing, using Tomcat running on Ubuntu Server. I'm trying to run my first "Hello World" servlet, without success.
I have the following stuff on the server:
- the
webapps
directory is:/var/lib/tomcat6/webapps/
- in
webapps
I have created the context-roothello/
directory hello/
containsindex.html
andWEB-INF/
WEB-INF
containsweb.xml
andclasses/HelloServlet.class
This is index.html
:
<html>
<body>
Click to request the HelloServlet.
<form action = "/hello/helloworld" method = "get" >
<input type = "submit" value = "REQUEST" />
</form>
</body>
</html>
This is WEB-INF/web.xml
:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
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">
<servlet>
<servlet-name>test</servlet-name>
<servlet-class>HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/hello/helloworld</url-pattern>
</servlet-mapping>
</web-app>
and finally this is the source file of HelloServlet
:
// HelloServlet.java, a simple Hello World servlet.
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class HelloServlet extends HttpServlet
{
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter outputStream = response.getWriter();
outputStream.println("<html>");
outputStream.println("<head>");
outputStream.println("<title>Hello, World!</title>");
outputStream.println("</head>");
outputStream.println("<body>");
outputStream.println("Hello, world! This is my first servlet!");
outputStream.println("</body>");
outputStream.println("</html>");
outputStream.close();
}
}
The problem is that, on the client side, only http://localhost/hello/
(i.e the index.html
page) works. If I click the form-submit button, I get http 404 error (resource not available).
Probably there is an error in servlet-mapping, in the form and/or in web.xml
, but I really need help to discover it.