Generic Servlet in Java with Examples
Generic Servlet in Java with Examples
https://dotnettutorials.net/lesson/generic-servlet/ 1/13
3/26/24, 11:57 AM Generic Servlet in Java with Examples - Dot Net Tutorials
Servlet article where we discussed Java Servlet Interface. At the end of this article, you will understand the
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…
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 }
using Servlet
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
https://dotnettutorials.net/lesson/generic-servlet/ 6/13
3/26/24, 11:57 AM Generic Servlet in Java with Examples - Dot Net Tutorials
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.*;
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