servlets
This is an old revision of the document!
Servlets
This lecture covers the JEE's approach to server-side processing. It introduces the overall Tomcat architecture and focuses on the servlet container. The hands-on lab session will walk you through the installation of Tomcat and introduces you to the capabilities of its three engines, Coyote, Catalina, and Jasper.
Outline
The Architecture
- Tomcat = Coyote + Catalina + Jasper. It is a reference implementation of Sun's Servlet/JSP standards.
- All three are 100% Java
- Coyote is a web server / connector, Catalina is a servlet container, and Jasper is a JSP compiler.
- Client uses HTTP to reach Coyote.
- Coyote uses web.xml to determine how to serve: traditional NFS (static HTML), CGI, and SSI, or delegate to Catalina.
- You supply your servlet subclass to Catalina and it handles the request.
- Jasper turns JSP documents into servlets. Think of it an a JSP compiler.
- Coyote can also operate as a connector to an existing web server, such as Apache or IIS. In that case, the web server would handle traditional serving and, for servlet requests, would use the JK protocol (similar to HTTP but binary) to delegate to Catalina in process.
The MVC Design Pattern
- Separation of concerns
- Controller = servlet
- Model = POJO
- View = JSP
The Life Cycle
- Server makes one instance of your servlet
- It invokes the
init
method on it. You may need to instantiate POJO's/beans or initialize ininit
. - It creates a pool of threads ready to invoke your servlet's
service
method. - When a client connects, a thread is (randomly) chosen and assigned to serve this request.
- There is no client-to-thread mapping: the same client may get served by the same or a different thread.
- When it is time to stop this servlet, the server invokes its
destroy
method then unloads it.
The API
- No need to override
service
since it filters based on the request method. Override eitherdoGet
ordoPost
(or both) - The request object enables you to retrieve socket data, HTTP data, the headers, the parameters, and the payload (for POST).
- Note that parameters are available as Map<String,String[]> and that for payloads you can have a raw stream or a wrapped up one.
- The response object allows you to set the HTTP response line and headers as well as the payload.
The Franework
- Infrastructural services
- Data structures and session management
- Database connectivity
- Events and filtering
- Declarative security
To Do
- Read Sections 6.1 through 6.5 of our textbook.
- Take an overall look at the servlet API (accessible from our Resources Page.
- Review Lab-3
servlets.1223466952.txt.gz · Last modified: 2008/10/08 11:55 by roumani