GenericServlet in Java: Hello World Example with web.xml
What is GenericServlet?
- GenericServlet is a predefined class that exists in the
javax.servletpackage. - GenericServlet is an abstract class.
- GenericServlet implements the Servlet interface to provide abstraction.
- GenericServlet provides implementations for all Servlet methods except
service(). - GenericServlet also implements the ServletConfig and Serializable interfaces.
The point of GenericServlet is to save you work. If you implement the Servlet interface directly, you must write all five methods yourself — even the ones you do not care about. GenericServlet writes those for you and leaves only service() for you to implement. That is the method where your actual request handling lives.
Hello World example using GenericServlet
Create a class that extends GenericServlet and override only the service() method:
package com.deepsingh44.blogspot.controller;
import java.io.IOException;
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class HelloWorldGenericServlet extends GenericServlet {
@Override
public void service(ServletRequest arg0, ServletResponse arg1)
throws ServletException, IOException {
arg1.setContentType("text/html");
log("welcome to here");
arg1.getWriter().print("<h1>Hello Generic Servlet Example</h1>");
}
}
Map GenericServlet in web.xml
To access this servlet from the browser, you must create a URL mapping in web.xml:
<web-app>
<servlet>
<servlet-name>firstdemo</servlet-name>
<servlet-class>com.deepsingh44.blogspot.controller.HelloWorldGenericServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>firstdemo</servlet-name>
<url-pattern>/helloservlet</url-pattern>
</servlet-mapping>
</web-app>
Run the code and check the output
Deploy the application to Tomcat and open the URL that matches your url-pattern. You should see the heading "Hello Generic Servlet Example" in the browser.
What is happening here
- extends GenericServlet — Your class inherits default implementations of
init(),destroy(),getServletConfig(), andgetServletInfo(). You do not write those. - service(ServletRequest, ServletResponse) — This is the only method you override. It runs for every request.
- setContentType("text/html") — Tells the browser that the response is HTML, not plain text.
- log("welcome to here") — Writes a message to the server log. GenericServlet provides this method for free.
- getWriter().print(...) — Sends the actual response body to the browser.
Where to go next
You now know what GenericServlet is, why it exists, and how to write a servlet by extending it. You also know how to map it in web.xml and run it on Tomcat.
If you remember one thing, remember this: GenericServlet saves you from writing methods you do not need. It handles everything except the actual request handling, which is why you only override service().
If you want to go deeper, try these next steps:
- Switch from GenericServlet to HttpServlet. Extend HttpServlet instead and override
doGet()instead ofservice(). Notice how the code becomes more specific to HTTP. - Read query parameters with
request.getParameter("name")and echo them back in the response. - Replace
web.xmlwith the@WebServlet("/helloservlet")annotation. Compare both approaches. - Look at the Tomcat logs after running the servlet. You will see the
log("welcome to here")message. That is how you debug server-side code.
No comments:
Post a Comment