Servlet Program in java
Write a servlet that reads the name of the servlet class and its initialisation parameters from a server file and then displays these parameters on the screen.
Packages used:
1. java.io.* : Provides for system input and output through data
streams, serialization and the file system.
2. java.util.* :
Provides for system input and output through data streams, serialization and
the file system.
3.
javax.servlet.*
:
contains a number of classes and interfaces that describe and define the contr
acts between a servlet class and the runtime environment provided for an insta
nce of such a class by a conforming servlet container.
4.
javax.servlet.http.*
: package contains a number of
classes and interfaces that describe and define the contracts between a servlet
class running under the HTTP protocol and the runtime environment provided for
an instance of such a class by a conforming servlet container.
Classes used:
·
HttpServlet : simplifies writing HTTP
servlets,provides an framework for handling the HTTP protocol. Because it is an
abstract class, servlet writers must subclass it and override at least one
method.
·
HttpServletRequest : gets data from the client to the servlet for
use in the
HttpServlet.service
method
·
HttpServletResponse : This interface allows a servlet's
service
method to manipulate HTTP-protocol specified
header information and return data to its client.
·
PrintWriter : Print formatted representations
of objects to a text-output stream. This class implements all of the print methods found in
PrintStream
·
HttpUtils : collection of static utility methods useful
to HTTP servlets.
Logical
flow of the program:
·
A public class that inherits HttpServlet is created.
·
Initialisation parameters from a server file is
obtained with the help of methods defined in HttpUtils class,object of
HttpServletRequest class is used to obtain different parameters of a server
file.
Important
notes:
·
The
servlet program must import the package “javax.servlet.*”.
·
If the servlet program is accessing database it
should import the package “java.sql.*”.
·
The
servlet class should be declared “public”.
·
The class
should extend “HttpServlet” class.
·
The class
should throw ServletException,IOException to avoid compilation error.
·
If the servlet class is extending
“GenericServlet” then the method used is “service()”.
·
If the servlet class is extending “HttpServlet”,
it should import the package “javax.servlet.http.*”.Also
the method used here is either “doGet()”
or “doPost()”.
0 comments: