0% found this document useful (0 votes)
5 views9 pages

Generic Servlet in Java with Examples

This document provides an overview of Generic Servlets in Java, detailing their structure, limitations, and methods. It explains how GenericServlet simplifies servlet creation and highlights the necessity of using HttpServlet for HTTP features. Additionally, it includes example code for implementing a Generic Servlet and its configuration in a web application.

Uploaded by

smtmaherbanusmc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views9 pages

Generic Servlet in Java with Examples

This document provides an overview of Generic Servlets in Java, detailing their structure, limitations, and methods. It explains how GenericServlet simplifies servlet creation and highlights the necessity of using HttpServlet for HTTP features. Additionally, it includes example code for implementing a Generic Servlet and its configuration in a web application.

Uploaded by

smtmaherbanusmc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

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

https://dotnettutorials.net/lesson/generic-servlet/ 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

https://dotnettutorials.net/lesson/generic-servlet/ 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: javax.servlet.http.HttpServlet 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
https://dotnettutorials.net/lesson/generic-servlet/ 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

https://dotnettutorials.net/lesson/generic-servlet/ 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.

https://dotnettutorials.net/lesson/generic-servlet/ 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
super.init(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.

https://dotnettutorials.net/lesson/generic-servlet/ 6/13
3/26/24, 11:57 AM Generic Servlet in Java with Examples - Dot Net Tutorials

Example to demonstrate Generic Servlet

index.html

<!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>

ServletInterface.java

import java.io.*;
import javax.servlet.*;

public class ServletInterface extends GenericServlet


{
public void service (ServletRequest req, ServletResponse res) throws IOEx
{

https://dotnettutorials.net/lesson/generic-servlet/ 7/13
3/26/24, 11:57 AM Generic Servlet in Java with Examples - Dot Net Tutorials

res.setContentType ("text/html");
PrintWriter pwriter = res.getWriter ();
pwriter.print ("<html>");
pwriter.print ("<body>");
pwriter.print ("<h2>Generic Servlet Example</h2>");
pwriter.print ("<p>Hello Readers!</p>");
pwriter.print ("</body>");
pwriter.print ("</html>");
}
}

web.xml

<web-app>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>ServletInterface</servlet-name>

https://dotnettutorials.net/lesson/generic-servlet/ 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

https://dotnettutorials.net/lesson/generic-servlet/ 9/13

You might also like