This document discusses Java servlets, which are server-side components that dynamically extend Java-enabled servers. Servlets benefit from Java's architecture and platform independence. A servlet takes an HTTP request as input and responds with an HTTP response. Servlets are more efficient and powerful than traditional CGI technologies. The document covers servlet types, software requirements, environment setup, advantages and disadvantages, the HTTP request lifecycle, and provides a simple "Hello World" servlet example.
2. Servlets are protocol- and platform-independent server side components,
written in Java, which dynamically extend Java-enabled servers.
They have become more and more popular as they benefit from all the
advantages of the Java programming language in particular Architecture and
Platform Independence and the ability to build robust and secure
applications.
Servlet
12/21/2015University of Education, Okara
2
3. A Servlet, in simple terms, is a Java program running under a web server
taking a 'request' object as an input and responding back by a 'response'
object. Typically a web browser will send the request in HTTP format. The
Servlet container will convert that into a request object. Similarly the
response object - populated by the Servlet is converted into an HTTP
response by the Servlet container.
This mechanism makes the browser - web server interaction very easy.
Java Servlets are more efficient, easier to use, more powerful, more portable,
and cheaper than traditional CGI and many alternative CGI-like technologies.
(More importantly, Servlet developers tend to get paid more than Perl
programmers)
12/21/2015University of Education, Okara
3
Servlet
4. Servlet Mechanism
A servlet is a Java class and thus needs to be executed in a Java Virtual Machine
by a service called a servlet engine. The servlet engine loads the servlet before it
can be used. The servlet then stays loaded until it is unloaded or the servlet
engine is shut down.
12/21/2015University of Education, Okara
4
5. Servlet Mechanism
12/21/2015University of Education, Okara
5
Client/
Browser
Server Servlet
JVM
Request
Response
Extends the functionality of
the server by
Generating HTML pages
dynamically
6. Servlet Types
Servlets are based on two main packages
javax.servlet
javax.servlet.http
GenericServlet
For writing protocol independent servlets
HttpServlet
Extends from GenericServlet class
Adds functionality for writing HTTP specific servlets
12/21/2015University of Education, Okara
6
8. Software Requirements
To use Java servlets, following softwares will be needed
J2SE
Additional J2EE based libraries for servlets such as servlet-api.jar, jsp-
api.jar. You can download these APIs separately but they are also
available with the web server you’ll be using.
A capable servlet web engine (web server)
12/21/2015University of Education, Okara
8
9. Environment Setup
Steps
1. Download the Apache Tomcat Server
2. Install Tomcat
3. Set the JAVA_HOME variable
4. Set the CATALINA_HOME variable
5. Set the CLASSPATH variable
6. Test the Server
12/21/2015University of Education, Okara
9
10. Servlet Advantages & Disadvantages
The advantage of Servlets is,
Portability
o Portable across operating systems and across web servers
Power
o Harness the full power of the core Java APIs: networking and URL access,
multithreading, image manipulation, data compression, JDBC, object
serialization, internationalization
Efficiency & Endurance
o Memory resident, so invocation highly efficient—no process to spawn or
interpreter to invoke
12/21/2015University of Education, Okara
10
11. Servlet Advantages & Disadvantages
Safety
o Support safe programming since inherit Java’s strong type safety, exception-
handling mechanism
Elegance
o Code is clean, object-oriented, modular, and simple (i.e.. Session tracking, cookie)
Integration
o Tightly integrated with the server—translate file paths, perform logging, check
authorization, and MIME type mapping
12/21/2015University of Education, Okara
11
12. Servlet Advantages & Disadvantages
The disadvantage of Servlets is,
Web Administrator will need to learn how to install and maintain Java
Servlets
Tedious uses of out.println() statements
o Can be remedied by using Java Server Page (JSP)
12/21/2015University of Education, Okara
12
13. Types of HTTP Requests
Get
Post
Delete
Options
Put
Trace
12/21/2015University of Education, Okara
13
14. How HTTP Sends Request
Client
Server
Servlets
Some HTTP request types
• Get -- Attr/Val pairs attached after ? of URL
E.g. http://www.gmail.com/register?name=ali
• Post -- Attr/Val pairs attatched with the request body
15. HTTP Request Example
12/21/2015University of Education, Okara
15
Request parameters etc.