Servlets Notes
Servlets Notes
Any company want to develop the website that can be developed in two ways, they are
static website and dynamic website.
1. A static website is one where there is no interaction from the end user. To develop
static website, we can use the markup languages like HTML, DHMTL, XML, JavaScript
etc.
2. A dynamic website is one in which there exist end user interaction. To develop
dynamic websites, in industry we have lot of technologies such as CGI (Common
Gateway Interface), java, dot net, etc.
In the recent years SUN micro systems has developed a technology called Servlets to
develop the dynamic websites and also for developing distributed applications.
A distributed application is one which always runs in the context of browser or www. The
result of distributed application is always sharable across the globe. To develop distributed
application one must follow the following:
Client-Server architecture:
1. 2-tier architecture (Client program, Database program).
2. 3-tier or MVC (Model [Database] View [JSP] Controller [Servlets]) architecture (Client
program, Server program, Database program).
3. n-tier architecture (Client program, Firewall program, Server program, Database
program). To exchange the data between client and server we use a protocol caller
http which is a part of TCP/IP.
4. A client is the program which always makes a request to get the service from server.
5. A server is the program which always receives the request, process the request and
gives
6. response to ‘n’ number of clients concurrently
A server is the third-party software developed by third party vendors according to SUN
micro systems specification. All servers in the industry are developed in java language only.
The basic purpose of using server is that to get concurrent access to a server-side program.
According to industry scenario, we have two types of servers; they are web server and
application server.
2. Web server does not contain enough 100% security to the server side
enough services to develop effective 4. For examples web logic server, web
NOTE: Web logic server acts as both web server and as well as application server
In the initial days of server side programming there is a concept called CGI and this was
implemented in the languages called C and PERL. Because of this approach CGI has the
following disadvantages
1. Platform dependency.
2. Not enough security is provided.
3. Having lack of performance. Since, for each and every request a new and separate process
is creating (for example, if we make hundreds of requests, in the server side hundreds of
new and separate processes will be created)
To avoid the above problems SUN micro system has released a technology called Servlets.
A servlet is a simple platform independent, architectural neutral server independent java
program which extends the functionality of either web server or application server by
running in the context of www.
Servlets is the standard specification released by SUN micro systems and it is implemented
by various server vendors such as BEA corporation (Web logic server), Apache Jakarta
(Tomcat server).
In order to run any servlet one must have either application server or web server. In order to
deal with servlet programming we must import the following packages:
javax.servlet.*;
javax.servlet.http.*;
Servlet Hierarchy:
LIFE CYCLE METHODS of servlets:
In servlets we have three life cycle methods, they are
Whenever control comes to service () method the server will create two objects of
ServletRequest and ServletResponse interfaces. Object of ServletRequest contains the data
which is passed by client. After processing client data, the resultant data must be kept in an
object of ServletResponse.
An object of ServletRequest and ServletResponse must be used always within the scope of
service () method only i.e., we cannot use in init () method and destroy () method.
Once the service () method is completed an object of ServletRequest and an object of
ServletResponse will be destroyed.
public void destroy ():
The destroy () method will be called by the server in two situations; they are when theserver
is closed and when the servlet is removed from server context. In this method we write the
block of statements which are obtained in init () method.
NOTE: Life cycle methods are those which will be called by the server at various times to
perform various operations
GenericServlet
Method-2 is used for obtaining all parameter names and their corresponding parameter
values.
For example:
Enumeration en=config.getInitParameterNames ();
while (en.hasMoreElements ())
{
Object obj=en.nextElement ();
String pname= (String) obj;
String pvalue=config.getInitParameter (pname);
out.println (pvalue+” is the value of ”+pname);
}
<web-app>
<context-param>
<param-name>Name of the param</param-name>
<param-value>Value of the param</param-value>
</context-param>
<servlet>
…………..
…………..
</servlet>
<servlet-mapping>
…………..
…………..
</servlet-mapping>
</web-app>