3
votes

I tried to reproduce a Servlet example which implements a Servlet using only the javax.servlet.Servlet interface. However, it is not working. When I try to run the servlet using the web browser, it shows me an error message:

The requested resource () is not available.

The error message logged in glassfish is

[#|2012-10-15T07:00:58.703-0500|SEVERE|glassfish3.0.1|global|_ThreadID=39;_ThreadName=Thread-1;|The Class app01a.MyServlet having annotation javax.servlet.annotation.WebServlet need to be a derived class of javax.servlet.http.HttpServlet. symbol: TYPE location: class app01a.MyServlet |#]

I'm using glassfish, eclipse and jdk1.7.0_03.

Why is this happening?

I know that the normal way of implementing a Servlet is extending the HttpServlet Class. However, I'm curios about why the author is doing that.

The name of the book is "Servlet and JSP: A Tutorial By Budi Kurniawan".

Below is the example code.

Thank you for your help

package app01a;

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

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;


/**
 * Servlet implementation class MyServlet
 */
@WebServlet("/MyServlet")
public class MyServlet implements Servlet {

    private transient ServletConfig  servletConfig;

    /**
     * Default constructor. 
     */
    public MyServlet() {
    }

    /**
     * @see Servlet#init(ServletConfig)
     */
    public void init(ServletConfig config) throws ServletException {
        this.servletConfig = servletConfig;
    }

    /**
     * @see Servlet#destroy()
     */
    public void destroy() {
    }

    /**
     * @see Servlet#getServletConfig()
     */
    public ServletConfig getServletConfig() {
        return servletConfig;
    }

    /**
     * @see Servlet#getServletInfo()
     */
    public String getServletInfo() {
        return "My Servlet";
    }

    /**
     * @see Servlet#service(ServletRequest request, ServletResponse response)
     */
    public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
        String servletName = servletConfig.getServletName();
        response.setContentType("text/html");
        PrintWriter writer = response.getWriter();
        writer.print("<html><head></head><body>Hello from " +
                     servletName + "</body></html>");
    }

}
2

2 Answers

2
votes

I was reading this book and also encountered the same issue on Jetty with the following error message: WARN:oeja.WebServletAnnotation:main: app01a.MyServlet is not assignable from javax.servlet.http.HttpServlet

This is happening because @WebServlet annotation can be applied only to subclasses of javax.servlet.http.HttpServlet.

The Java 6 EE Tutorial says:

Classes annotated with @WebServlet must extend the javax.servlet.http.HttpServlet class

So the issue can be easily fixed by replacing implements Servlet with extends HttpServlet.

1
votes

I can see you asked this question a while back but I had a similar problem.

Try removing the WebServlet altogether and deploying the site again. You might get a more helpful error message that's closer to the root of the problem.

I did this and discovered I had made an invalid NamedQuery in one of my entity classes. I put the WebServlet annotation back afterwards and the site was fine.