Java Servlet Presentation
Java Servlet Presentation
Softsmith Infotech
Servers
A server is a computer that responds to requests from a client
Typical requests: provide a web page, upload or download a file,
send email
A server is also the software that responds to these requests; a
client could be the browser or other software making these requests
Typically, your little computer is the client, and someone elses big
computer is the server
However, any computer can be a server
It is not unusual to have server software and client software
running on the same computer
Softsmith Infotech
Apache
Apache is a very popular server
66% of the web sites on the Internet use Apache
Apache is:
Full-featured and extensible
Efficient
Robust
Secure (at least, more secure than other servers)
Up to date with current standards
Open source
Free
Why use anything else?
Softsmith Infotech
Ports
A port is a connection between a server and a client
Ports are identified by positive integers
A port is a software notion, not a hardware notion, so there may
be very many of them
A service is associated with a specific port
Typical port numbers:
21FTP, File Transfer Protocol
22SSH, Secure Shell
25SMTP, Simple Mail Transfer Protocol
53DNS, Domain Name Service
80HTTP, Hypertext Transfer Protocol
8080HTTP (used for testing HTTP) These are the ports
7648, 7649CU-SeeMe
of most interest to us
27960Quake III
Softsmith Infotech
Ports II
My Web page is:
http://www.softsmith.com/~servlet
But it is also:
http:// www.softsmith.com:80 /~servlet
CGI Scripts
CGI stands for Common Gateway Interface
client
server
client
script
Servlets
A servlet is like an applet, but on the server side
client
server
client
servlet
Softsmith Infotech
Servlets
A servlet is any class that implements the javax.servlet.Servlet
interface
In practice, most servlets extend the
javax.servlet.http.HttpServlet class
Some servlets extend javax.servlet.GenericServlet instead
Servlets, like applets, usually lack a main method, but must
implement or override certain other methods
Softsmith Infotech
10
HTTP requests
When a request is submitted from a Web page, it is almost always a
GET or a POST request
The HTTP <form> tag has an attribute action, whose value can be
"get" or "post"
The "get" action results in the form information being put after a ? in
the URL
Example:
http://www.google.com/search?hl=en&ie=UTF-8&oe=UTF8&q=servlets
The & separates the various parameters
Only a limited amount of information can be sent this way
"put" can send large amounts of information
Softsmith Infotech
11
Softsmith Infotech
12
13
The superclass
public class HelloServlet extends HttpServlet {
Every class must extend GenericServlet or a subclass of
GenericServlet
GenericServlet is protocol independent, so you could write
a servlet to process any protocol
In practice, you almost always want to respond to an HTTP
request, so you extend HttpServlet
A subclass of HttpServlet must override at least one method,
usually one doGet, doPost, doPut, doDelete, init and destroy, or
getServletInfo
Softsmith Infotech
14
Softsmith Infotech
15
Parameters to doGet
Input is from the HttpServletRequest parameter
Our first example doesnt get any input, so well discuss this a bit
later
Output is via the HttpServletResponse object, which we have named
response
I/O in Java is very flexible but also quite complex, so this object
acts as an assistant
Softsmith Infotech
16
Softsmith Infotech
17
Softsmith Infotech
18
Input to a servlet
A GET request supplies parameters in the form
URL ? name=value & name=value & name=value
Softsmith Infotech
19
20
Enumeration review
An Enumeration is almost the same as Iterator
Its an older class, and the names are longer
Example use:
Enumeration e = myVector.elements();
while (e.hasMoreElements()) {
System.out.println(e.nextElement());
}
Softsmith Infotech
21
Softsmith Infotech
22
23
Session Tracking
What is Session?
A Session refers to all the request that a single client makes to a
server.
Every user has a separate session and separate session variable is
associated with that session.
A session is specific to the user and for each user a new session is
created to track all the request from that user.
Softsmith Infotech
24
Softsmith Infotech
25
Softsmith Infotech
26