Servlets
- Java's answer to processing CGI.
- Most Servlets handle such HTTP requests, there are
other types of servers as well, e.g. FTP servers, Mail servers, etc
and you can develop servlets for them. (you will extend
a different java class when creating an HTTP-based servlet compared
to a non-HTTP-based servlet).
- Servlets are designed to support a request/response computing
model (CGI)....client sends a request (to invoke servlet), and server
responds (sending servlet response).
- Java Servlet API: located in javax.* packages.
Generic Servlet
- must import the java.servlet package
- must extend the GenericServlet class or HttpServlet class if
- implementing http features
- must override the service method in the class
THE Hello World Servlet:
import java.io.*;
import java.servlet.*;
public class HelloWorldServlet
extends GenericServlet {
public
void service(ServletRequest req, ServletResponseres)
throws ServletException, IOException{
PrintStream out = newPrintStream(res.getOutputStream());
out.println("Hello world!");
}
public
String getServletInfo() {
return "Hello World Servlet";
}
}
|
How a Servlet works:
1) Read any data sent by the user
e.g. from an HTML
form, or an applet,etc.
2) Look up HTTP request information
e.g Browser capabilities,
requesting host, cookies, etc.
3) Generate Results
e.g. using
possibly database access- JDBC, RMI, directo computation, calling
other applicaitons, etc.
4) Format the results inside a document
e.g. produce a web-page
(HTML)
5) Set HTTP response parameters
e.g. MIME type(e.g.
returning HTML), cookies, compression, etc.
6) Send the document to the client |
Why build web-pages dynamically?
- Web-page is based on user submitted data.
- e.g. results page from search engines, order-confirmation from
on-line store, etc.
- Web-page is derived from data that changes frequently
- e.g. stock prices, weather report, etc.
- Web-page uses information from databases or other server-side
sources.
- e.g. looking at prices in a database, etc.
|
Advantage of Servlets over Other Languages Processing
CGI
- Efficient
- Threads instead of OS processes.....only one servlet copy,
is persistent!!
- This is unlike other languages, Perl, C/C++, etc. where you
invoke a new process everytime.
- Convenient
- High-level utilities built-in. Makes things like parsing
CGI data easier.
- Powerful
- Because of persistence, information can be maintained from
request-to-request (cache previous computations, keep open DB
connections, etc).
- Different servlets can more easily share information.
- Servlets can talk directly to the web-server. (other
languages can but, must use server-specific APIs if they are
available).
- Portable
- basic benefit of Java (but, scripting languages like Perl
are also portable)
- Secure
- basic benefit of Java (array-bounds checking, etc).
|
Important: when you use other java classes, beans, etc.
from a Servlet, you need to deploy them in a package inside of a webapplication.
The location will be WEB-INF/classes/directoryMatchingPackageName. Or
alternatively you can place in a JAR file and put in WEB-INF/lib. This
is important because you need to import them and the default package location
for different Servlet containers is not standard. The default package
that a Servlet is placed into is not standardized int the Servlet Container
spec so, different containers do different things!!!! Be carefull and
always use packages and deploy them appropriately.
|