0% found this document useful (0 votes)
5 views

24PCS802

The document outlines the steps to create a simple servlet example, detailing six essential steps that apply to all servers. It describes three methods for creating a servlet, with the most common being to extend the HttpServlet class for its HTTP request handling capabilities. An example code for a basic servlet named HelloServlet.java is provided, demonstrating how to implement the service method to generate a simple HTML response.

Uploaded by

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

24PCS802

The document outlines the steps to create a simple servlet example, detailing six essential steps that apply to all servers. It describes three methods for creating a servlet, with the most common being to extend the HttpServlet class for its HTTP request handling capabilities. An example code for a basic servlet named HelloServlet.java is provided, demonstrating how to implement the service method to generate a simple HTML response.

Uploaded by

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

SIMPLE SERVLET

EXAMPLE
A.NELSON
24PCS802
Steps to create a servlet example

There are given 6 steps to create a servlet


example. These steps are required for all the
servers.

The servlet example can be created by three


ways:

1.By implementing Servlet interface,


2.By inheriting GenericServlet class, (or)
3.By inheriting HttpServlet class

The mostly used approach is by extending


HttpServlet because it provides http request specific
method such as doGet(), doPost(), doHead() etc.
•Compile
•Create the Servletdescriptor
a deployment
• Create
•Start the a deployment
server descriptor
and deploy the project
•Start the servlet
•Access server and deploy the project
•Access the servlet

The steps are as follows:

1.Create a directory structure


2.Create a Servlet
3.Compile the Servlet
4.Create a deployment descriptor
5.Start the server and deploy the project
6.Access the servlet
EXAMPLE:

Create a text file named HelloServlet.java, with the following


content:

//A Simple Servlet

import java.io.*;
import java.util.*;
import javax.servlet.*;
Public class HelloServlet extends GenericServlet
{ Public void service (ServletRequest request, ServletResponse
response) throws ServletException, IOException
{ responsesetContentType(“text/html”);
PrintWriter out=responsegetWriter();
Calendar cal=Calendar.getInstance();
int hour = cal.get (Calendar.HOUR_OF_DAY);
out.println ( “< HTML >” );
out.println ( “< BODY >” );
out.println ( “< H1> Hello, Servlets ! </H1 >” );
out.println ( “< BR >” );
out.println ( “< HR >” );
out.println ( “< H2> Servlet Time is ” + hour + “ </H2>” );
out.println ( “< /BODY >” );
out.println ( “< /HTML >” );
out.close ( );
}
}
OUTPUT:

You might also like