Jython is the best choice if you have already developed applications in Java. However many developer/programmer feel that it is slower than Java because it is interpreted and consume more resources and many other issues, but I don't want to discuss it here.
Let see how we can run Jython on Tomcat server.
After installing Java set JAVA_HOME, PATH and CLASSPATH environment variables in your system for running Java, Jython and Servlets.
After finishing all Java related settings, now install Jython. After downloading Jython you can see that it is a jar file, so go to Download folder where you have downloaded it using command prompt.
And run command
java -jar jython_installer-2.2.1.jar
Jython installation GUI will open and just install it.
After installation of Jython, install
Apache Tomcat and check it by typing
http://localhost:8080/ in any of your favorite browser. If you see page like given below, then it is working.
Now you have done all setup for your first Jython application.
Create a directory myfirstjython in webapps directory. Also create WEB-INF directory and in WEB-INF create a directory classes and lib and a xml file web.xml. So, you WEB-INF directory will look like -
To test that our every set up is working properly, we will run a Servlet on this server.
So write your servlet like-
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyFirstServlet extends HttpServlet {
public void doPost (HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType ("text/html");
PrintWriter toClient = response.getWriter();
toClient.println ("<html><head><title>My First Servlet</title>" +
"<body><h2>My First Servlet</h2></body></html>");
}
}
compile it on command prompt by running
javac MyFirstServlet.jar
If there is no error. Open web.xml file and deploy here your Servlet by editing this file as -
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0"
metadata-complete="true">
<servlet>
<servlet-name>MyFirstServlet</servlet-name>
<servlet-class>MyFirstServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyFirstServlet</servlet-name>
<url-pattern>/MyFirstServlet</url-pattern>
</servlet-mapping>
</web-app>
Restart your Tomcat Server and then run Servlet on browser by typing-
http://localhost:8080/myfirstjython/MyFirstServlet
Now after running Servlet, You are ready to run your first Jython program on web.
So, first write your Jython program to run on web. Here is a Jython program that is equivalant to above Servlet -
from javax.servlet.http import HttpServlet
class MyFirstJythonServlet (HttpServlet):
def doGet(self,request,response):
self.doPost (request,response)
def doPost(self,request,response):
toClient = response.getWriter()
response.setContentType ("text/html")
toClient.println ("<html><head><title>This is my first Jython.</title>" +
"<body><h1><center>This is my first Jython.<center></h1></body></html>")
And save this file to your web root directory.
Now in web.xml (projectfolder/WEB-INF/web.xml) do the following entry.
<servlet-name>PyServlet</servlet-name>
<servlet-class>org.python.util.PyServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>PyServlet</servlet-name>
<url-pattern>*.py</url-pattern>
</servlet-mapping>
If you change web.xml file, you will require to restart tomcat server every time.
Now restart your Tomcat Server and open any browser and type -
Servlet requires every time to compile after every change in file, while Python does not require.
http://localhost:8080/jythondemo/MyFirstJythonServlet.py
If all is right, output will be -
The complete web.xml file is -
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0"
metadata-complete="true">
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
<servlet>
<servlet-name>MyFirstServlet</servlet-name>
<servlet-class>MyFirstServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>PyServlet</servlet-name>
<servlet-class>org.python.util.PyServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MyFirstServlet</servlet-name>
<url-pattern>/MyFirstServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>PyServlet</servlet-name>
<url-pattern>*.py</url-pattern>
</servlet-mapping>
</web-app>