HttpServlet Example
Listing 12.4 demonstrates a Servlet that subclasses javax.servlet.http.
HttpServlet. The example implements both the doGet and
doPost methods to demonstrate how those methods are called depending on
the type of request received from the client. A web client should not allow a
POST operation to be repeated without confirmation. Because of this,
database updates, order commits, and so on should be in a doPost
method, whereas the forms to do so can reside safely in a doGet.
The get_post.py file in Listing 12.4 shows how to get parameter
names and values by using the HttpServletRequest's (req)
getParameterNames and getParameterValues. The list returned
from getParameterNames is a java.util.Enumeration, which the
doPost method uses to display the request's parameters. Jython
lets you use the for x in list: syntax with the enumeration returned
from getParameternames. Note that the getParameterValues()
method is plural. Parameters can have multiple values as demonstrated in the
hidden form fields. To prevent redundancy in the doGet and
doPost methods of the Servlet, Listing 12.4 adds a _params
method. This method is not defined anywhere in HttpServlet or its
bases, and because no Java class calls it, no @sig string is required.
The purpose of the method is merely to keep similar operations in only one
place.
Listing 12.4 Implementing a Servlet with HttpServlet
#file get_post.py
from time import time, ctime
from javax import servlet
from javax.servlet import http
class get_post(http.HttpServlet):
head = "<head><title>Jython Servlets</title></head>"
title = "<center><H2>%s</H2></center>"
def doGet(self,req, res):
res.setContentType("text/html")
out = res.getWriter()
out.println('<html>')
out.println(self.head)
out.println('<body>')
out.println(self.title % req.method)
out.println("This is a response to a %s request" %
(req.getMethod(),))
out.println("<P>In this GET request, we see the following " +
"header variables.</P>")
out.println("<UL>")
for name in req.headerNames:
out.println(name + " : " + req.getHeader(name) + "<br>")
out.println("</UL>")
out.println(self._params(req))
out.println("""
<P>The submit button below is part of a form that uses the
"POST" method. Click on this button to do a POST request.
</P>""")
out.println('<br><form action="get_post" method="POST">' +
'<INPUT type="hidden" name="variable1" value="one">' +
'<INPUT type="hidden" name="variable1" value="two">' +
'<INPUT type="hidden" name="variable2" value="three">' +
'<INPUT type="submit" name="button" value="submit">')
out.println('<br><font size="-2">time accessed: %s</font>'
% ctime(time()))
out.println('</body></html>')
def doPost(self, req, res):
res.setContentType("text/html");
out = res.getWriter()
out.println('<html>')
out.println(self.head)
out.println('<body>')
out.println(self.title % req.method)
out.println("This was a %s<br><br>" % (req.getMethod(),))
out.println(self._params(req))
out.println('<br> back to <a href="get_post">GET</a>')
out.println('<br><font size="-2">time accessed: %s</font>'
% ctime(time()))
out.println('</body></html>')
def _params(self, req):
params = "Here are the parameters sent with this request:<UL>"
names = req.getParameterNames()
if not names.hasMoreElements():
params += "None<br>"
for name in names:
value = req.getParameterValues(name)
params += "%s : %r<br>" % (name, tuple(value))
params += "</UL>"
return params
After placing the get_post.py file in the
$TOMCAT_HOME/webapps/jython/WEB-INF/classes directory, compile it with
the following:
jythonc w . deep get_post.py
To test it, point your browser at http://localhost:8080/jython/servlet/get_post.
You should see a browser window similar to that in Figure
12.1.
Figure 12.1
The GET view from get_post.py.
The parameters for the GET operation are
None in this example, but test other parameters in the doGet
method by adding some to the end of the URL(such as
http://localhost:8080/servlet/get_post?variable1=1&variable2=2).
Because the Submit button on the bottom of the first view is part of a form
implemented as a POST, clicking on the Submit button executes the doPost
method of the same Servlet. The results of the doPost method should
match what is shown in Figure 12.2.
Figure 12.2
The POST view from get_post.py.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|