Question

I'm trying to make my first servlet to run in eclipse for j2ee with tomcat 7.0, but I can't figure it out what i am doing wrong. I run the whole project like this: Right-click on my project->Run As->Run on Server, the index.html file appear to me in the browser, but when i hit "Continue", it's showing me this message:

HTTP Status 404 - /PDPJ_L5/hello - The requested resource (/PDPJ_L5/hello) is not available.

I suspect that the problem is at the action attribute from the form tag, or at the web descriptor file. Please explain me what i am doing wrong and how to repair my project. Thanks.

Here is my project (PDPJ_L5) directory structure:

PDPJ_L5

  • JAX-WS Web Services
  • Deployment Descriptor: PDPJ_L5
  • Java Resources: src which contains the servlets package with my Hello.java servlet
  • JavaScript Resource
  • build
  • META-INF
  • WEB-INF with a lib folder, my index.html and web.xml

The index.html contents:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Some title</title>
</head>
<body>
<H1 ALIGN="CENTER">Choose your option:</H1>
<form action="http://localhost:8080/PDPJ_L5/hello" method="GET">
    <center>
        <INPUT TYPE="RADIO" NAME="group" VALUE="one">ONE<BR>
        <INPUT TYPE="RADIO" NAME="group" VALUE="two">TWO<BR><BR>
        <INPUT TYPE="SUBMIT" VALUE="Continue">
    </center>
</form>
</body>
</html>

Here is the Hello.java servlet:

package servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Hello
 */
public class Hello extends HttpServlet
{
    private static final long   serialVersionUID    = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Hello()
    {
        super();
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        response.setContentType("text/html");

        PrintWriter out = response.getWriter();
        out.println("<HEAD><TITLE>Success</HEAD><BODY>");
        out.println("<h1> It works </h1>");
        out.println("</BODY>");
        out.close();
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        doGet(request, response);
    }

}

And the web.xml file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-application_2_3.dtd">

<web-app>
   <servlet>
      <servlet-name>Hello</servlet-name>
      <servlet-class>servlets.Hello</servlet-class>
   </servlet>

   <servlet-mapping>
      <servlet-name>Hello</servlet-name>
      <url-pattern>/hello</url-pattern>
   </servlet-mapping>
</web-app>    
Was it helpful?

Solution 4

I found the solution for my problem.

I built a war archive with the jar command, and put it in the webapps folder of tomcat, and it's working in this way.

The only problem left is that it's not working under eclipse with run on server, but in this way it's working.

Thanks for your help.

OTHER TIPS

When your index page loads, what port is actually being used? Your hyperlink goes to 8080, but I'm thinking that the "Run on Server" is starting a temporary server that would bind to a random port.

What do the Tomcat log files say (or if you are running the Tomcat instance from within Eclipse the console)? This normally happens when some Exception was thrown when initializing the servlet or serving the request.

Assuming you've Right Click > Built the Project.

In your Eclipse workspace (in the windows explorer), can you find the actual class file - PDPJ_L5/build/classes/servlets/Hello.class? does it exist?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top