jython 使用

| 1 Comment | No TrackBacks

servlet的另类写法:使用jython众所周知,servlet是java在服务器端程序设计的重要组成部分,servlet执行效率较高,适合做程序的逻辑处理,缺点是使用起来比较繁琐,每个servlet必须在web.xml中声明并且当修改部署后的servet后一般还需要reload容器,这种情况对于开速开发是很不利的,本文介绍一种另外的serlvet开发方式:使用jython 本文假设读者熟悉某种应用服务器(如tomcat),java 和 python程序设计语言 在http://www.jython.org上下载jython,假设为D:jython-2.1 建立一个名称为jython的web应用,把D:jython-2.1jython.jar copy到webapp下的WEB-INFlib下,并配置web.xml如下: PyServlet org.python.util.PyServlet 11
python.home
D:jython-2.1

python.path
D:jython-2.1Lib
PyServlet *.py 现在编写一个HelloWorld.py,如下: from javax.servlet.http import HttpServlet class HelloWorld(HttpServlet):#需要类名和文件名一致 def doGet(self,request,response): response.setContentType("text/html;charset=utf-8"); out = response.getWriter() print>>out,"

Hello World by Jython Servlet!


"; 启动应用服务器,假设位于localhost,端口8080,访问http://localhost:8080/jython/HelloWorld.py,是不是看到了“Hello World by Jython Servlet!”字符串输出呢? 正如您所想象的一样,jython servlet无须在web.xml做任何声明,PySerlvet会拦截所有.py请求并分发给各自的jython servlet,并且在修改HelloWorld的实现后,立刻就可以看到变化,无须重启应用服务器,很方便吧! 注:编写HelloWorld.py的时候请严格缩进,python是一种以缩进区分代码块的语言,不正确的缩进是语法错误

No TrackBacks

TrackBack URL: http://www.wujianrong.com/mt-tb.cgi/467

1 Comment

用Pylons不是更容易实现,更方便吗?为啥非要绕那么大圈?

Leave a comment

About this Entry

This page contains a single entry by kevinwu published on February 23, 2006 11:40 AM.

alt.lang.jre: Get to know Jython was the previous entry in this blog.

简明 Python 教程 is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.