Escolar Documentos
Profissional Documentos
Cultura Documentos
Ye Wu http://www.ise.gmu.edu/~wuye/ SWE 642 Software Engineering for the World Wide Web
sources: Java for the Web with Servlets, JSP and EJB, Kurniawan, New Riders Building Web Applications with UML, Conallen, Addison-Wesley Professional Java Server Programming, Patzer, Wrox
<HTML> <HEAD> <TITLE> WELCOME</TITLE> <HEAD> <BODY> <FORM ACTION=http://hermes.gmu.edu:8080/swe64201/servlet/Hello METHOD = POST> <P> NAME: <INPUT TYPE=TEXT SIZE=40 NAME=name></P> <P> <INPUT TYPE=SUBMIT VALUE=SUBMIT></P> </FORM> <BODY> </HTML>
2006-2-15 Dr. Wu and Dr. Offutt 2
My usual strategy:
Use POST when sending data to server Use GET when no data is sent
GET parameters are limited to 1024 bytes POST requests put form data in body of request
2006-2-15 Dr. Wu and Dr. Offutt 5
2006-2-15
Web Container
Web Container request
? ?
HTML result
Servlets
Servlets are small Java classes that perform a service Servlet container or engine
connects to network catches requests produces responses requests are handled by objects
2006-2-15
11
2006-2-15
12
2.
2006-2-15
13
2006-2-15
14
Client 1 Client 2
2006-2-15
15
2006-2-15
16
SingleThreadModel (2)
The SingleThreadModel ensures that only one thread may execute the service() method at a time Containers may implement this in two ways:
1. Instance Pooling: A "pool" of several servlet instances are available that do not share memory 2. Request Serialization: Only one request is handled at a time 3. Combination: A pool is used, and if there more requests than servlets in the pool, they are serialized
This is resource intensive and can be slow Better to synchronize only the statements that might interfere with each other
2006-2-15 Dr. Wu and Dr. Offutt 18
Servlet Lifecycle
UML State Diagram
Does not exist
instantiation based on a request or at container startup
initialization
release reference
Unavailable
Destroyed
2006-2-15
Service
19 2006-2-15 Dr. Wu and Dr. Offutt 20
Servlet API
javax.servlet primarily containers javax.servlet.http methods to service requests GenericServlet HttpServlet MyServlet
2006-2-15
Abstract class (not all methods are implemented) All methods are implemented Servlet written by a programmer
21
2006-2-15
22
1) Init ( )
Read configuration data Read initialization parameters (javax.servlet.ServletConfig) Initialize services:
Database driver Connection pool Logging service
2006-2-15
23
2006-2-15
24
2) Service ( )
The entry point for the servlet this is the method that is called from the servlet container Called after the initialization (Init ()) Primary purpose is to decide what type of request is coming in and then call the appropriate method
doGet () doPost ()
2006-2-15 Dr. Wu and Dr. Offutt 25
2006-2-15
26
3) Destroy ( )
Called by container when the servlet instance is killed The threads from the service() method are given time to terminate before destroy() is called Can be used for clean up tasks:
Un-registering a database driver Closing a connection pool Informing another application the servlet is stopping Saving state from the servlet
Implement services by overriding these methods (most commonly doGet() and doPost())
2006-2-15
27
2006-2-15
28
4) getServletConfig ( )
Returns a ServletConfig object, which stores information about the servlet's configuration The ServletConfig object was passed into init()
5) getServletInfo ( )
Returns a String object that stores information about the servlet:
Author Creation date Description Usage
2006-2-15
29
2006-2-15
30
32
Connection
Remotely login into hermes
Connect hermes from osf1
ssh l your_hermes_id hermes
Installation
1. ClASSPATH
Add $CATALINA_HOME/common/lib/servlet-api.jar into your CLASSPATH
2006-2-15
Software Packages
Ant 1.6 (http://ant.apache.org)
Set /home/faculty/wuye/apache-ant as ANT_HOME Add $ANT_HOME/bin/ into your PATH Add $ANT_HOME/lib/ant.jar into your CLASSPATH
2006-2-15
35