1.1 A Brief History of The Web: Java Servlet
1.1 A Brief History of The Web: Java Servlet
1. INTRODUCTION
The rise of server-side Java applications is one of the latest and most exciting
trends in Java programming. The Java language was originally intended for use in
small, embedded devices. It was first hyped as a language for developing elaborate
client -side web content in the form of applets. Until recently, Java’s potential as a
server-side development platform had been sadly overlooked. Now, Java is coming
into its own as a language ideally suited for server-side development.
Businesses in particular have been quick to recognize Java’s potential on the
server—Java is inherently suited for large client/server applications. The cross-
platform nature of Java is extremely useful for organizations that have a
heterogeneous collection of servers running various flavors of the Unix and Windows
operating systems. Java’s modern, object-oriented, memory-protected design allows
developers to cut development cycles and increase reliability. In addition, Java’s
built-in support for networking and enterprise APIs provides access to legacy data,
easing the transition from older client/server systems.
Java servlets are a key component of server-side Java development. A servlet
is a small, pluggable extension to a server that enhances the server’s functionality.
Servlets allow developers to extend and customize any Java-enabled server—a web
Server, a mail server, an application server, or any custom server—with a hitherto
unknown degree of portability, flexibility, and ease. But before we go into any more
detail, let’s put things into perspective.
SSGMCE, SHEGAON 1
JAVA SERVLET
Fig. 1.1
SSGMCE ,SHEGAON 2
JAVA SERVLET
Embedded in the webpages, when a browser loads a webpage the applet byte code is
downloaded and executed by the browser.
Microsoft’s Active server pages uses a suite of technologies that allows
dynamically-generated content. A series of objects and components that are executed
on the web server
Java servlets are one of the options for server-side that gives portability
,efficiency and power of java.
2. SERVLETS
2.1 DEFINITION
SSGMCE ,SHEGAON 3
JAVA SERVLET
Fig.1.3
A Servlet is a Java class in Java EE that conforms to the Java Servlet API, a
protocol by which a Java class may respond to HTTP requests A servlet is a Java
programming language class used to extend the capabilities of servers that host
applications accessed via a request-response programming model. Although servlets
can respond to any type of request, they are commonly used to extend the applications
hosted by Web servers. For such applications, Java Servlet technology defines HTTP-
specific servlet classes.. Thus, a software developer may use a servlet to add dynamic
content to a Web server using the Java platform. The generated content is commonly
HTML, but may be other data such as XML. Servlets are the Java counterpart to non-
Java dynamic Web content technologies such as CGI and ASP.NET. Servlets can
maintain state in session variables across many server transactions by using HTTP
cookies, or URL rewriting.
The servlet API, contained in the Java package hierarchy javax.servlet, defines
the expected interactions of a Web container and a servlet. A Web container is
essentially the component of a Web server that interacts with the servlets. The Web
container is responsible for managing the lifecycle of servlets, mapping a URL to a
particular servlet and ensuring that the URL requester has the correct access rights.
SSGMCE ,SHEGAON 4
JAVA SERVLET
Fig.2.2
In the fig., model is an object that defines the component's state described in
Javabeans. View is a visual screen representation in jsp of a component and
controller is an object that makes a component respond to user input which is servlet
code.
SSGMCE ,SHEGAON 5
JAVA SERVLET
The most basic servlet definitions live in the javax.servlet package and consist
of a number of interfaces. Servlet architecture represented in fig.3.1.
SSGMCE ,SHEGAON 6
JAVA SERVLET
Servlet
Fig.3.1
SSGMCE ,SHEGAON 7
JAVA SERVLET
3. The Web container calls the init() method. the servlet engine loads the
servlet’s *.class This method initializes the servlet and must be called before
the servlet can service any requests. In the entire life of a servlet, the init()
method is called only once.
4. After initialization, the servlet can service client requests. Each request is
serviced in its own separate thread. The Web container calls the service()
method of the servlet for every request. The service() method determines the
kind of request being made and dispatches it to an appropriate method to
handle the request. The developer of the servlet must provide an
implementation for these methods. If a request for a method that is not
implemented by the servlet is made, the method of the parent class is called,
typically resulting in an error being returned to the requester. when a servlet
request is made a ServletRequest object is sent with all information about the
request and a ServletResponse object is used to return the response.
5. Finally, the Web container calls the destroy() method that takes the servlet out
of service. The destroy() method, like init(), is called only once in the lifecycle
of a servlet.
SSGMCE ,SHEGAON 8
JAVA SERVLET
Fig.4.1
This first servlet java file should be saved in webapps of tomcat in your
application context in WEB-INF in classes and compile it, Run javac command in
command prompt. Servlet does not contain main method. So servlet can not run on
java directly, mean like simple java program, servlet can not execute on command
prompt. You can execute servlet on web browser, but servlet class file should be in
tomcat web server of WEB-INF/classes folder. When we try compile first servlet
example, classpath of tomcat/lib/servlet-api.jar jar file should be set.
javac first.java
import java.io.*;
SSGMCE ,SHEGAON 9
JAVA SERVLET
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
6.1 EFFICIENT
With traditional CGI, a new process is started for each HTTP request. If
the CGI program itself is relatively short, the overhead of starting the process can
dominate the execution time. With servlets, the Java Virtual Machine stays running
and handles each request using a lightweight Java thread, not a heavyweight operating
system process. Similarly, in traditional CGI, if there are N simultaneous requests to
the same CGI program, the code for the CGI program is loaded into memory N times.
With servlets, however, there would be N threads but only a single copy of the servlet
class. Finally, when a CGI program finishes handling a request, the program
terminates. This makes it difficult to cache computations, keep database connections
open, and perform other optimizations that rely on persistent data. Servlets, however,
remain in memory even after they complete a response, so it is straight forward to
store arbitrarily complex data between requests.
6.2 CONVENIENT
SSGMCE ,SHEGAON 10
JAVA SERVLET
tracking sessions, and many other such high-level utilities. Besides, you already know
the Java programming language. Why learn Perl too? You’re already convinced that
Java technology makes for more reliable and reusable code than does C++. Why go
back to C++ for server-side programming?
6.3 PORTABLE
Servlets are written in the Java programming language and follow a standard
API. Consequently, servlets written for, say, I-Planet Enterprise Server can run
virtually unchanged on Apache, Microsoft Internet Information Server (IIS), IBM
WebSphere, or StarNine WebStar. For example, virtually all of the servlets and JSP
pages in this book were executed on Sun’s Java Web Server, Apache Tomcat and
Sun’s JavaServer Web Development Kit (JSWDK) with no changes whatsoever in the
code. Many were tested on BEA WebLogic and IBM WebSphere as well. In fact,
servlets are supported directly or by a plug-in on virtually every major Web server.
They are now part of the Java 2 Platform, Enterprise Edition (J2EE), so industry
support for servlets is becoming more pervasive.
6.4 SECURE
SSGMCE ,SHEGAON 11
JAVA SERVLET
6.5 INEXPENSIVE
7. CONCLUSION
The servlet API provides the foundation on which JSPs are built, and
understanding this API can come in handy for page authors. The servlet API defines a
life cycle for servlets, starting with an init() method that is called when the servlet first
loads, a service() method that is called for each request, and a destroy() method that is
called before the servlet is retired. The init() method may allocate resources that
requests will later need, and destroy() can free these resources. The service() method
is passed a request and a response object, which it uses to get information about the
request, set information about the response, and send the data.
JSPs are ultimately servlets. Thus, for pages with any significant amount
of HTML, a JSP will almost always be the preferred means of creating pages, as it is
easier to read and maintain and it avoids all the print statements. On the other hand,
pages that are dominated mostly by code expressing page logic may be better off as a
servlet, as this will avoid having to put everything in scriptlets. Servlets can also
interact with JSPs,using beans as an intermediary. Typically, the servlet will do the
computation, build a bean with the results, and send the bean on to the JSP for
formatting, using the forward() method. This provides the cleanest separation between
logic and presentation.
SSGMCE ,SHEGAON 12
JAVA SERVLET
8. REFERENCE
1. ‘Javaserver Pages ‘ Book by ‘’Larne Peckowesky”.
3. www.java.sun.com/j2ee/tutorial/1_3-fcs/doc/Servlets.
4. www.easywayserver.com.
SSGMCE ,SHEGAON 13