3/26/24, 11:57 AM Generic Servlet in Java with Examples - Dot Net Tutorials
Java Servlets – Basics
Generic Servlet
Introduction to Web
Application and Web
Back to: Java Servlets Tutorials
Terminology
Introduction to
Servlets
HTTP Protocol and
HTTP Methods
Java Servlet API
Java Servlet Interface
Generic Servlet
HTTP Servlet in Java
Servlet Life Cycle
Replay
Steps to Create
Servlet Application
How to Create Java
Servlet Application
How Servlet Works
[Link] 1/13
3/26/24, 11:57 AM Generic Servlet in Java with Examples - Dot Net Tutorials
User Interface Form
Designs Generic Servlet in Java with Examples
WAR File in Java
Welcome File List in In this article, I am going to discuss Generic Servlet in Java with Examples. Please read our previous
Servlet article where we discussed Java Servlet Interface. At the end of this article, you will understand the
Creating Servlet using following pointers.
Eclipse IDE
Creating Servlet using 1. Java Generic Servlet
MyEclipse IDE 2. What are the limitations of the GenericServlet?
Creating Servlet using 3. What is the general structure of a real servlet (used in real web-based java projects in
NetBeans IDE the industry)?
Servlet
4. Methods of Generic Servlet class
Communication in
5. Example to demonstrate Generic Servlet
Java
ServletRequest
Java Generic Servlet
Interface
RequestDispatcher in
The GenericServlet implements the Servlet and ServletConfig interfaces. GenericServlet could also be
Servlet
directly extended by a servlet, although it’s more common to increase a protocol-specific subclass like
Servlet Chaining in
HttpServlet. GenericServlet makes writing servlets easier. It provides simple versions of the lifecycle
Java
methods init and destroy and of the methods within the ServletConfig interface. GenericServlet also
Request Redirection
implements the log method, declared within the ServletContext interface. To write a generic servlet, you
in Servlet
need to override the abstract service method.
ServletConfig
Interface
ServletContext Advertisements
Interface
Java Servlet Attributes
[Link] 2/13
3/26/24, 11:57 AM Generic Servlet in Java with Examples - Dot Net Tutorials
C# MVC Web API Design Patterns .NET Core Dotnet Data Bases Java C/C++/DSA More…
Java Servlets – What are the limitations of the GenericServlet?
Advanced
1. It cannot provide HTTP features to user-defined servlets. For example Session tracking, URL
Servlet Scopes in Java
rewriting, cookies, etc.
Session Tracking in
2. A real-time website should have HTTP features.
Servlet
3. Therefore, user defined servlet should not inherit directly from GenericServlet.
Cookies Session
Tracking Mechanism
Note: [Link] which is a sub-class of GenericServlet addresses the limitations of
Hidden Form Fields
GenericServlet.
Session Tracking
Mechanism
URL Rewriting Session
Tracking Mechanism
HttpSession Session
Tracking Mechanism
Servlet Events and
Listeners
Servlet Wrappers
[Link] 3/13
3/26/24, 11:57 AM Generic Servlet in Java with Examples - Dot Net Tutorials
Servlet Filters
CRUD in Servlet
Exception Handling in
Servlet
Servlet Annotations
Servlet Input Output
Stream Classes
Single Thread Model
Interface
Server Side Include
(SSI) in Servlet
Servlet Debugging
Servlet
Internationalization
[Link] 4/13
3/26/24, 11:57 AM Generic Servlet in Java with Examples - Dot Net Tutorials
Real-time Application What is the general structure of a real servlet (used in real web-based java projects
Development Examples in the industry)?
Registration Form in
Servlet
Fetch Data from public class MyServlet extends HttpServlet
Database using {
Servlet public void init (ServletConfig config) throws ServletException
Improving Servlet {
performance to fetch //resource allocation code
records from }
database
Uploading and public void doGet / doPost (HttpServletRequest request, HttpServletRespon
Downloading Files in
ServletException
Servlet
{
Sending Email
// client request handling code
through JavaMail API
} //service method
in Servlet
Write data into PDF
public void destroy ()
using Servlet
{
Login Form in Servlet
//resource releasing code
Display Images using
Servlet }
Auto Refresh Page }
using Servlet
Java Servlet – Interview
Note: When the client request is GET implement the doGet() method as a service method in a user-defined
servlet. If post then implement doPost() method.
[Link] 5/13
3/26/24, 11:57 AM Generic Servlet in Java with Examples - Dot Net Tutorials
Servlet Interview
Questions and
Answers
Popular Servlet Books
Methods of Generic Servlet class
Most Recommended
Servlet Books
1. destroy(): It is called by the servlet container to indicate to a servlet that the servlet is being taken
out of service.
2. getInitParameter(String name): It returns a string containing the value of the named initialization
parameter, or null if the parameter does not exist.
3. getInitParameterNames(): If the servlet has no initialization parameters it returns the names of the
servlet’s initialization parameters as an Enumeration of String objects, or an empty Enumeration.
4. getServletConfig(): It returns this servlet’s ServletConfig object.
5. getServletContext(): It returns a reference to the ServletContext in which this servlet is running.
6. getServletInfo(): It returns information about the servlet, such as author, version, and copyright.
7. getServletName(): It returns the name of this servlet instance.
8. init(): It is a convenience method that can be overridden so that there’s no need to call
[Link](config).
9. init(ServletConfig config): It is called by the servlet container to indicate to a servlet that the servlet
is being placed into service.
10. log(String msg): It writes the specified message to a servlet lof file, prepended by the servlet’s
name.
11. log(String message, Throwable t): It writes an explanatory message and a stack trace for a given
Throwable exception to the servlet log file, prepended by the servlet’s name.
12. service(ServletRequest req, ServletResponse res): It is called by the servlet container to allow
the servlet to respond to a request.
[Link] 6/13
3/26/24, 11:57 AM Generic Servlet in Java with Examples - Dot Net Tutorials
Example to demonstrate Generic Servlet
[Link]
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Generic Servlet Demo</title>
</head>
<body>
<a href="welcome">Click to call Servlet</a>
</body>
</html>
[Link]
import [Link].*;
import [Link].*;
public class ServletInterface extends GenericServlet
{
public void service (ServletRequest req, ServletResponse res) throws IOEx
{
[Link] 7/13
3/26/24, 11:57 AM Generic Servlet in Java with Examples - Dot Net Tutorials
[Link] ("text/html");
PrintWriter pwriter = [Link] ();
[Link] ("<html>");
[Link] ("<body>");
[Link] ("<h2>Generic Servlet Example</h2>");
[Link] ("<p>Hello Readers!</p>");
[Link] ("</body>");
[Link] ("</html>");
}
}
[Link]
<web-app>
<welcome-file-list>
<welcome-file>[Link]</welcome-file>
<welcome-file>[Link]</welcome-file>
<welcome-file>[Link]</welcome-file>
<welcome-file>[Link]</welcome-file>
<welcome-file>[Link]</welcome-file>
<welcome-file>[Link]</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>ServletInterface</servlet-name>
[Link] 8/13
3/26/24, 11:57 AM Generic Servlet in Java with Examples - Dot Net Tutorials
<servlet-class>ServletInterface</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletInterface</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
</web-app>
Output
[Link] 9/13