|
From: Brian Z. <bri...@ya...> - 2001-02-01 20:56:18
|
It works great with the change I made. Attached are the two classes I
changed:
pyservlet.PyHandler in the place of the old PyServlet
pyservlet.HandlerAwareHttpServlet
The same handler can deal with both 1) jython servlet derived from
HttpServlet and 2) jython servlet derived from HandlerAwareHttpServlet.
Fortunately most info is from request, so only when you need to port
SnoopServlet do you need to use 2.
I also send this to the jython-dev list in hoping that in future
release, we can have servlet support built-in. I view this as one of the
big strength of using jython, if JSP is mod_php to apache, we have the
equivalent of mod_python now. JDBC, XMLC, imageio, the possiblilities
are endless.
Following are some sample jython servlet scripts. I love it! Thank you
jython developers.
/Brian
# hello.py
import sys, java, javax
class hello(javax.servlet.http.HttpServlet):
def doGet(self, req, res):
res.setContentType("text/html");
out = res.getOutputStream()
out.println("""\
<html>
<head><title>Hello World</title></head>
<body>Hello World!
<P>
current server time is: %s
</P>
</body>
</html>
""" % (java.util.Date()))
out.close()
return
# titles.py
import java, javax
class titles(javax.servlet.http.HttpServlet):
def doGet(self, request, response):
response.setContentType("text/plain")
out = response.getOutputStream()
self.dbTitles(out)
out.close()
return
def dbTitles(self, out):
server, db = "ncv-dev2", "pubs"
usr, passwd = "sa", ""
driver = "com.inet.tds.TdsDriver"
port = 1433
url = "jdbc:inetdae:" + server + ":" + `port` + "?database=" + db
java.lang.Class.forName(driver).newInstance()
conn = java.sql.DriverManager.getConnection(url, usr, passwd)
query = "select title, price, ytd_sales, pubdate from titles"
stmt = conn.createStatement()
if stmt.execute(query):
rs = stmt.getResultSet()
while rs and rs.next():
out.println(rs.getString("title"))
if rs.getObject("price"):
out.println("%2.2f" % rs.getFloat("price"))
else:
out.println("null")
if rs.getObject("ytd_sales"):
out.println(rs.getInt("ytd_sales"))
else:
out.println("null")
out.println(rs.getTimestamp("pubdate").toString())
out.println()
stmt.close()
conn.close()
----- Original Message -----
From: "Brian Zhou" <bri...@ya...>
To: <jyt...@li...>
Cc: <jyt...@li...>
Sent: Wednesday, January 31, 2001 9:34 AM
Subject: [Jython-users] servlet problem self.getInitParameterNames()
under PyServlet wrapper
> Hello list,
>
> Following http://groups.yahoo.com/group/jpython/message/3714 I
succeeded
> making servlet running under PyServlet wrapper/handler on win2k
tomcat3.2.1.
> So far so good, I can run simple script like:
>
> import sys, java, javax
>
> class hello(javax.servlet.http.HttpServlet):
> def doGet(self, req, res):
> res.setContentType("text/html");
> out = res.getOutputStream()
> out.println("""\
> <html>
> <head><title>Hello World</title></head>
> <body>Hello World!
> <P>
> current server time is: %s
> </P>
> </body>
> </html>
> """ % (java.util.Date()))
> out.close()
> return
>
> Or even dynamicly generate PNG graph thanks to the new javax.imagio
package
> from Sun. I don't expect any difficulty hooking up with database using
JDBC.
>
> However, when trying to port SnoopServlet from java to jython, I found
that
> PyServlet really doesn't handover any instant variables to the jython
> HttpServlet subclass except (request, response). So
>
> enum = self.getInitParameterNames()
>
> will got a NullPointerException. Any operation involve self.attribute
like
> self.getServletContext() will also fail. I don't think jythonc
compiled
> classes will have this problem because the jython servlet handle
service()
> directly bypassing PyServlet.
>
> So my questions are:
>
> 1. Am I missing anything? Any misunderstanding of the API?
> 2. Any way around? Anyone got a better PyServlet?
> 3. If I'm the first one got bitten, any idea how we may overcome this
> problem?
>
> Seeing the power of dynamic python scripting at work (instant feedback
> without re-compiling), I really appreciate what have been done so far,
and
> want to get it going. Here are some of my ideas:
>
> 1. We can have two seperate subclasses of HttpServlet in java:
> one possibly named ServletPyHandler, doing what PyServlet
currently is
> doing, dispatching service() calls to jython servlet;
> the other JyServlet being superclass of all jython servlets, with
> constructor JyServlet(ServletPyHandler), so JyServlet always has a ref
to
> the ServletPyHandler instance;
> The downside of this approach is that now jythonc compiled servlet and
> dynamically interpreted servlet have to be written differently.
>
> 2. Have ServletPyHandler do somthing to the jython servlet class right
> before dispatching service() call, so that inside jython servlet,
later when
> messages like self.getInitParameterNames() will be redirected back to
> ServletPyHandler.
>
> TIA for any ideas,
>
> /Brian
>
>
>
> _______________________________________________
> Jython-users mailing list
> Jyt...@li...
> http://lists.sourceforge.net/lists/listinfo/jython-users
>
|