Java Reference
In-Depth Information
ServletContext application
=
getServletContext
();
boolean
boolean
iWon
=
false
false
;
String user
=
request
.
getRemoteHost
() +
'@'
+
request
.
getRemoteAddr
();
// Do the synchronized stuff first, and all in one place.
synchronized
synchronized
(
application
) {
iif
(
application
.
getAttribute
(
WINNER
) ==
null
null
) {
application
.
setAttribute
(
WINNER
,
user
);
application
.
log
(
"BuzzInServlet: WINNER "
+
user
);
iWon
=
true
true
;
}
}
response
.
setContentType
(
"text/html"
);
PrintWriter out
=
response
.
getWriter
();
out
.
println
(
"<html><head><title>Thanks for playing</title></head>"
);
out
.
println
(
"<body bgcolor=\"white\">"
);
iif
(
iWon
) {
out
.
println
(
"<b>YOU GOT IT</b>"
);
// TODO - output HTML to play a sound file :-)
}
else
else
{
out
.
println
(
"Thanks for playing, "
+
request
.
getRemoteAddr
());
out
.
println
(
", but "
+
application
.
getAttribute
(
WINNER
) +
" buzzed in first"
);
}
out
.
println
(
"</body></html>"
);
}
/** The Post method is used from an Administrator page (which should
* only be installed in the instructor/host's localweb directory).
* Post is used for administrative functions:
* 1) to display the winner;
* 2) to reset the buzzer for the next question.
*/
public
public
void
void
doPost
(
HttpServletRequest request
,
HttpServletResponse response
)
throws
throws
ServletException
,
IOException
{
ServletContext application
=
getServletContext
();
response
.
setContentType
(
"text/html"
);
HttpSession session
=
request
.
getSession
();
PrintWriter out
=
response
.
getWriter
();