- 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.
- javax.servlet.*;
Generic Servlet
import java.io.*; import java.servlet.*; public class HelloWorldServlet extends GenericServlet { public
void service(ServletRequest req, ServletResponseres) public
String getServletInfo() {
|
How a Servlet works:
1) Read any data sent by the user |
Why build web-pages dynamically?
|
Advantage of Servlets over Other Languages Processing
CGI
|
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.