(转载)配置tomcat支持jython
工作需要,特记录下配置tomcat支持jython开发的过程。参考链接:@http://blog.itpub.net/13186779/viewspace-201861/
*环境在win7下搭建,jdk版本1.8(目测jdk>=1.6皆可)
1.tomcat安装过程省略
下载地址:http://tomcat.apache.org/
2.下载jython(本例使用jython-installer-2.5.3.jar)
下载地址:http://www.jython.org/downloads.html
3.安装jython
双击jar包,一路点击next即可顺利安装。
4.配置环境变量:
set JYTHON_HOME=C:\jython2.5.3 set PATH=%JYTHON_HOME%;%PATH% set CLASSPATH=%JYTHON_HOME%\jython.jar;%CLASSPATH%
5.创建jython测试项目,此处引用:@http://blog.itpub.net/13186779/viewspace-201861/
*测试项目目录结构如下: webapps |----jython |----|----HelloWorld.py |----|----WEB-INF |----|----|----web.xml |----|----|----lib |----|----|----|----jython.jar(该jar可从JYTHON_HOME中拷贝而来)
6.代码
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.getOutputStream() print >>out, "<H1>Hello World by Jython Servlet!</H1>"
web.xml(jython安装目录请按实际情况修改):
<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee-web-app_2_4.xsd" version="2.4"> <servlet> <servlet-name>PyServlet</servlet-name> <servlet-class>org.python.util.PyServlet</servlet-class> <load-on-startup>1</load-on-startup> <init-param> <param-name>python.home</param-name> <param-value>C:\jython2.5.3</param-value> </init-param> <init-param> <param-name>python.path</param-name> <param-value>C:\jython2.5.3\Lib</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>PyServlet</servlet-name> <url-pattern>*.py</url-pattern> </servlet-mapping> </web-app>
7.完成上述后,启动tomcat即可
执行:${catalina.home}\bin\startup.bat
8.查看页面:http://localhost:8080/jython/HelloWorld.py
10.项目及安装文件已打包,需要的童鞋戳链接下载:http://pan.baidu.com/s/1c00aK16