Servlet
Servlet
Java Servlets
Today we all are aware of the need of creating dynamic web pages i.e the ones which
have the capability to change the site contents according to the time or are able to
generate the contents according to the request received by the client. If you like coding
in Java, then you will be happy to know that using Java there also exists a way to
generate dynamic web pages and that way is Java Servlet. But before we move forward
with our topic let’s first understand the need for server-side extensions.
Servlets are the Java programs that run on the Java-enabled web server or application
server. They are used to handle the request obtained from the webserver, process the
request, produce the response, then send a response back to the webserver.
4. The servlet processes the request and generates the response in the form of
output.
5. The servlet sends the response back to the webserver.
6. The web server sends the response back to the client and the client browser
displays it on the screen.
Now let us do discuss eccentric point that why do we need For Server-Side extensions?
The server-side extensions are nothing but the technologies that are used to create
dynamic Web pages. Actually, to provide the facility of dynamic Web pages, Web
pages need a container or Web server. To meet this requirement, independent Web
server providers offer some proprietary solutions in the form of APIs(Application
Programming Interface). These APIs allow us to build programs that can run with a
Web server. In this case, Java Servlet is also one of the component APIs of Java
Platform Enterprise Edition which sets standards for creating dynamic Web
applications in Java.
Before learning about something, it’s important to know the need for that something,
it’s not like that this is the only technology available for creating dynamic Web pages.
The Servlet technology is similar to other Web server extensions such as Common
Gateway Interface (CGI) scripts and Hypertext Preprocessor (PHP). However, Java
Servlets are more acceptable since they solve the limitations of CGI such as low
performance and low degree scalability.
1. Loading a Servlet: The first stage of the Servlet lifecycle involves loading and
initializing the Servlet by the Servlet container. The Web container or Servlet
Container can load the Servlet at either of the following two stages :
• Initializing the context, on configuring the Servlet with a zero or positive integer
value.
• If the Servlet is not preceding stage, it may delay the loading process until the Web
container determines that this Servlet is needed to service a request.
3. Handling request: After initialization, the Servlet instance is ready to serve the
client requests. The Servlet container performs the following operations when the
Servlet instance is located to service a request :
ITPC-305 WEB DESIGN TECHNOLOGIES
• init()
• service()
• destroy()
ITPC-305 WEB DESIGN TECHNOLOGIES
init() method: The Servlet.init() method is called by the Servlet container to indicate
that this Servlet instance is instantiated successfully and is about to put into service.
//init() method
//initialization code
//rest of code
service() method: The service() method of the Servlet is invoked to inform the Servlet
about the client requests.
This method uses ServletRequest object to collect the data requested by the client.
ITPC-305 WEB DESIGN TECHNOLOGIES
// rest of code
destroy() method: The destroy() method runs only once during the lifetime of a
Servlet and signals the end of the Servlet instance.
//destroy() method
As soon as the destroy() method is activated, the Servlet container releases the Servlet
instance.
Q. Why can’t we write connected architecture code inside the constructor, since
constructor also run only once in it’s entire life?
Ans. Suppose if the connection doesn’t get established, then we can throw an exception
from init() and the rest of the steps stop executing. But in the constructor we can’t use,
throw in it’s prototype otherwise it is an error.
1. This method receives only one parameter, i.e ServletConfig object.
2. This method has the possibility to throw the ServletException.
3. Once the servlet is initialized, it is ready to handle the client request.
4. The prototype for the init() method:
public void init(ServletConfig con)throws ServletException{ }
// Initializing servlet
public void init() throws ServletException
{
output = "Advance Java Concepts";
}
JSDK
JSDK is the Java Servlet Developers Kit. This is an add on to the regular JDK (Java Developers
Kit). The JSDK has the additional files needed in order to compile Java servlets. The latest
version of the JSDK is version 2.0. Included in the JSDK is a Java Servlet Runner program.
The Servlet Runner is a program that runs on your workstation, and allows you to test servlets
you have written without running a web server. Other files included are the several Java Servlet
ITPC-305 WEB DESIGN TECHNOLOGIES
examples. Included are the .java and .class files for testing purposes and to help you understand
how the Java code is implemented. Another important file that is included is the jsdk.jar file.
This file includes the class information necessary to compile the servlets.
Installing JSDK
Let me preface the JSDK installation procedures by saying that the JSDK is an add on to the
JDK. If you are trying to compile servlets using JSDK, you will be missing software. The JDK
includes the javac.exe program and other java library and class files that are necessary to
compile java code. Before you start to install the JSDK, install the JDK, then begin JSDK
install.
Installing the JSDK is a very simple procedure. When you run the .exe that you download from
the http://java.sun.com web site, it will create some temporary web files on your hard drive. It
will then launch the installation script. You will be asked to specify the directory you wish to
have the files copied to. As in the JDK, the recommended directory includes a 'period' in the
directory name (ex. C:\JSDK.20). Change this to C:\JSDK20 instead to make it easier when
searching for the directory. After the software is installed, you will have a new subdirectory.
In order to use both the JDK and JSDK together, the java compiler (javac.exe) needs to know
where the class files are located. I found the easiest way to make this work, is to first put the
bin directory for the JDK in the path. This will make it easy to find the javac.exe program when
compiling code. Second, add the jsdk.jar file to the classpath. This can be done by adding a
SET statement to the autoexec.bat file on your workstation (for Windows 95). The SET
statement should read SET CLASSPATH = drive:JSDK install path\lib\jsdk.jar (ex. SET
CLASSPATH = C:\jsdk20\lib\jsdk.jar). Once this is done, you will have no problem compiling
your java servlets.