Class SurveyServlet
java.lang.Object
|
+--javax.servlet.GenericServlet
|
+--javax.servlet.http.HttpServlet
|
+--SurveyServlet
- public class SurveyServlet
- extends javax.servlet.http.HttpServlet
- implements javax.servlet.SingleThreadModel
A sample single-threaded servlet that takes input from a form
and writes it out to a file. It is single threaded to serialize
access to the file. After the results are written to the file,
the servlet returns a "thank you" to the user.
You can run the servlet as provided, and only one thread will run
a service method at a time. There are no thread synchronization
issues with this type of servlet, even though the service method
writes to a file. (Writing to a file within a service method
requires synchronization in a typical servlet.)
You can also run the servlet without using the single thread
model by removing the implements statement. Because the
service method does not synchronize access to the file, multiple
threads can write to it at the same time. When multiple threads try
to write to the file concurrently, the data from one survey does not
follow the data from another survey in an orderly fashion.
To see interaction (or lack of interaction) between threads, use
at least two browser windows and have them access the servlet as
close to simultaneously as possible. Expect correct results (that
is, expect no interference between threads) only when the servlet
implements the SingleThreadedModel
interface.
- See Also:
- Serialized Form
Method Summary |
void |
doPost(javax.servlet.http.HttpServletRequest req,
javax.servlet.http.HttpServletResponse res)
Write survey results to output file in response to the POSTed
form. |
void |
init(javax.servlet.ServletConfig config)
|
Methods inherited from class javax.servlet.http.HttpServlet |
doDelete,
doGet,
doOptions,
doPut,
doTrace,
getLastModified,
service,
service |
Methods inherited from class javax.servlet.GenericServlet |
destroy,
getInitParameter,
getInitParameterNames,
getServletConfig,
getServletContext,
getServletInfo,
init,
log,
log |
Methods inherited from class java.lang.Object |
clone,
equals,
finalize,
getClass,
hashCode,
notify,
notifyAll,
toString,
wait,
wait,
wait |
SurveyServlet
public SurveyServlet()
init
public void init(javax.servlet.ServletConfig config)
throws javax.servlet.ServletException
- Overrides:
- init in class javax.servlet.GenericServlet
doPost
public void doPost(javax.servlet.http.HttpServletRequest req,
javax.servlet.http.HttpServletResponse res)
throws javax.servlet.ServletException,
java.io.IOException
- Write survey results to output file in response to the POSTed
form. Write a "thank you" to the client.
- Overrides:
- doPost in class javax.servlet.http.HttpServlet