Benja Garrido
Benja Garrido

Reputation: 711

Problems running a servlet

I am having problems with my Servlet. When I execute the code in Apache Tomcat, it returns this message:

type Informe de estado (status report)
mensaje /certificacion/ch1/Serv1
descripción El recurso requerido no está disponible. (Not available resource)

My code is simple. The xml contains:

<?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/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">
    <servlet>
        <servlet-name>Chapter1 Servlet</servlet-name>
        <servlet-class>Ch1Servlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>Chapter1 Servlet</servlet-name>
        <url-pattern>/Serv1</url-pattern>
    </servlet-mapping>
</web-app>

And the Servlet contains:

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class Ch1Servlet extends HttpServlet {
    public void doGet (HttpServletRequest request, HttpServletResponse response) throws IOException{
        PrintWriter out = response.getWriter();
        java.util.Date today = new java.util.Date();
        out.println("<html> "+"<body>"+"<h1 align=center>Chapter1 Servlet</h1>"+"<br>"+today+"</body>"+"</html>");
    }
}

The folder structure is:

--tomcat
  ->webapps
    ->certificacion
      ->ch1
        ->WEB-INF
          ->web.xml
          ->classes
            ->Ch1Servlet.class

The example is taken from the book "Head First Servlets & JPS" page 31.

Can you help me?

Upvotes: 1

Views: 312

Answers (2)

msrd0
msrd0

Reputation: 8371

Your folder structure is wrong. It must be like this:

--tomcat
  ->webapps
    ->certificacion
      ->WEB-INF
        ->web.xml
        ->classes
          ->Ch1Servlet.class
      ->ch1

Then, if you want the Servlet to be accessed like

http://localhost:8080/certification/ch1/Servlet

than you need to add/change your web.xml file to contain this:

<servlet>
    <servlet-name>Chapter 1 Servlet</servlet-name>
    <servlet-class>Ch1Servlet
</servlet>
<servlet-mapping>
    <servlet-name>Chapter 1 Servlet</servlet-name>
    <url-pattern>/ch1/Servlet</url-pattern>
</servlet-mapping>

Upvotes: 1

Anand Rajasekar
Anand Rajasekar

Reputation: 837

if the application is deployed in root context try host:port/Serv1

or this would work host:port/certification/Serv1

Upvotes: 1

Related Questions