Updated June 7, 2023
Introduction to Servlet Class
Java classes known as “servlets” carry out the javax.servlet specification and handle HTTP requests. Interface for servlets. Servlets class extend javax.servlet.http.HttpServlet interface, an abstract class that implements the Servlet interface and is specifically built to process HTTP requests, is often written by web application developers. The servlet-class element instructs the container on how to create a class instance. A java programming class known as a servlet is used to increase the functionality of servers that host applications accessed via the request-response programming model.
Key Takeaways
- Contain objects, methods, and database drivers.
- It extends and implements interfaces.
- Connects between page and web.xml file for applications operation.
- Make it easy to request and respond from another page.
- Use servlet life cycle inside the class and operate efficiently by the developer.
- It is the key point of any servlet page.
What is Servlet Class?
The main interface of the basic hierarchy is the interface. All Servlets must implement the interface, either directly or indirectly. Interface is implemented by the Servlet API’s GenericServlet class. The GenericSC executes the interface, the ServletConfig interface of the Servlet API, and the Serializable interface of the standard java.io. package. When a servlet is initialized, the Web container uses the Config interface object to give configuration information. To create a servlet that communicates via HTTP, we must extend the HttpServlet class. The HttpSC extends the GenericSC and includes HTTP capability by default.
Steps to Create
It makes use of two major packages: javax.servlet and javax.servlet.http. This package contains an interface and classes. The flowing classes us in the servlet for operation.
Classes:
- GenericServlet: The javax.servlet package contains GenericServlet class to interact with the web server. It is a generic implementation. If you want to develop for protocols other than HTTP, you should extend GenericServlet rather than explicitly implementing the Servlet interface.
- HttpServlet: The javax.servlet.http package contains classes for managing HTTP requests. It includes the HttpSC, which implements the required interfaces from javax.servlet.
Follow the below steps.
- Create a dynamic folder.
2. Create a package and page in the “src” folder.
3. Write URL mapping for deployment and use the appropriate method.
The page and class name are similar. The following syntax shows the basic class on the page.
package com.educba.servletdemo;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
@WebServlet("/Democlass")
public class Democlass extends HttpServlet {
private static final long serialVersionUID = 1L;
public Democlass() {
}
//Write servlet methods and operations.
}
4. Create a web.xml file with mapping or deployment.
Code:
<web-app>
<servlet>
<servlet-name> Democlass </servlet-name>
<servlet-class> Democlass </servlet-class>
</servlet>
</web-app>
5. Create the “url” mapping in the web.xml file.
Code:
<web-app>
<servlet-mapping>
<servlet-name> Democlass </servlet-name>
<url-pattern> /Democlass </url-pattern>
</servlet-mapping>
<web-app>
HttpServlet Class
An abstract class that provides HTTP request implementation capability. It is worth noting that the service() function is defined in the interface. It will now invoke doGet() and doPost(), which can individually be performed to offer behavior to the Servlet.
The following file sample shows us how it works in the page.
File name: Democlass.java
Code:
package com.educba.servletdemo;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/Democlass")
public class Democlass extends HttpServlet {
private static final long serialVersionUID = 1L;
public Democlass() {
}
protected void doGet(HttpServletRequest reqst, HttpServletResponse respnse) throws
ServletException, IOException {
respnse.getWriter().append("Served at: ").append(reqst.getContextPath());
}
protected void doPost(HttpServletRequest reqst, HttpServletResponse respnse) throws
ServletException, IOException {
doGet(reqst, respnse);
}
}
Explanation:
- The HttpServlet.
- The class extends the interface with a class name.
- The “doGet” and “doPost” methods use with the request and response object.
Examples of Servlet Class
The following examples show generic and HTTP SC with their output.
Example #1
Follow the below example and output.
Filename: DemoServletExample.java
Code:
import java.io.*;
import javax.servlet.*;
public class DemoServletExample extends GenericServlet
{
public void service(ServletRequest reqes, ServletResponse respes)
throws ServletException, IOException
{
respes.setContentType("text/html");
PrintWriter printout = respes.getWriter();
printout.println("<html>");
printout.println("<head><title> My GenericServlet Class </title> </head>");
printout.println("<body>");
printout.println("<h2> Welcome To Servlet Class Data! </h2>");
printout.println("</body>");
printout.println("</html>");
printout.close();
}
}
Filename: web.xml
Code:
<web-app>
<servlet>
<servlet-name> DemoServletExample </servlet-name>
<servlet-class> DemoServletExample </servlet-class>
</servlet>
<servlet-mapping>
<servlet-name> DemoServletExample </servlet-name>
<url-pattern> / DemoServletExample </url-pattern>
</servlet-mapping>
</web-app>
Output:
Example #2
The httpservlet class example and output.
Filename: DemoServletExample.java
Code:
package com.educba.servletdemo;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/Democlass")
public class DemoServletExample extends HttpServlet {
private static final long serialVersionUID = 1L;
private string messages;
public void init() throws ServletException{
messages = "The HttpServlet Class Demo";
}
protected void doGet(HttpServletRequest reqst, HttpServletResponse respnse) throws
ServletException, IOException {
respnse.getWriter().append("Served at: ").append(reqst.getContextPath());
respnse.setContentType("text/html");
PrintWriter printout = respnse.getWriter();
printout.println("<h2>" + messages + "</h2>");
printout.println("<p>" + "Welcome To Servlet Class Data!"+ "</p>");
}
public void destroy(){}
}
Filename: web.xml
Code:
<web-app>
<servlet>
<servlet-name> DemoServletExample </servlet-name>
<servlet-class> DemoServletExample </servlet-class>
</servlet>
<servlet-mapping>
<servlet-name> DemoServletExample </servlet-name>
<url-pattern> / DemoServletExample </url-pattern>
</servlet-mapping>
</web-app>
Output:
FAQ
Given below are the FAQs mentioned:
Q1. Is it a class or an interface?
Answer:
The http packages provide interfaces or classes for writing servlets. All servlets must conform to the Servlet interface, which specifies life-cycle methods. When creating a generic service, you can utilize or extend the Java Servlet API’s GenericSC.
Q2. What is the name and class of a servlet?
Answer:
There must be a specific name for every instance inside a context. On the other hand, the name is just used to correlate URL mappings with this instance and cannot have to correspond to the title or the servlet’s URL. The servlet-class element instructs the container on how to create a servlet instance.
Q3. What’s the distinction between a servlet and a Java class?
Answer:
It functions exactly like any other Java class. The language is not regarded any differently. What separates them is what is done to them. A servlet container (Tomcat) is designed to recognize class.
Conclusion
It helps to contain and operate methods, variables, and objects with data. It handles using web.xml files and annotations. It is an essential and operative function for each servlet page to handle the servlet life cycle.
Recommended Articles
This is a guide to Servlet Class. Here we discuss the introduction, steps to create, examples, and FAQ respectively. You may also have a look at the following articles to learn more –