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>");
}
}