Thursday, 27 September 2018

Run your first Jython page on Apache Tomcat Server

What is Jython? 
Jython is a Java implementation of Python that combines expressive power with clarity. Jython is freely available for both commercial and non-commercial use and is distributed with source code. Jython is complementary to Java and is especially suited for the following tasks:
  • Embedded scripting - Java programmers can add the Jython libraries to their system to allow end users to write simple or complicated scripts that add functionality to the application.
  • Interactive experimentation - Jython provides an interactive interpreter that can be used to interact with Java packages or with running Java applications. This allows programmers to experiment and debug any Java system using Jython.
  • Rapid application development - Python programs are typically 2-10X shorter than the equivalent Java program. This translates directly to increased programmer productivity. The seamless interaction between Python and Java allows developers to freely mix the two languages both during development and in shipping products.
                                               As described at https://wiki.python.org/jython.

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.

Note :- I have used Windows Server 2016, Java SE 10.0.2 and Apache Tomcat 9.

Pre-requisite :- 
  • Download Java from here.
  • Download Jython from here.
  • Download Apache Tomcat from here
After downloading these three install it. You can install Java just double clicking on it as normal software installation in Windows.


After installing Java set JAVA_HOME, PATH and CLASSPATH environment variables in your system for running Java, Jython and Servlets.
For setting environment variable, go to This PC/My Computer on your desktop, right click and then go to Properties 


Click on Advanced system settings


Then click on Advanced tab and then Environment Variables


Click on New button. In Variable name input field enter variable name JAVA_HOME and
variable value C:\Program Files\Java\jdk-10.0.2; (path of your jdk home directory) (After Java Installation)

Again click on New and put the variable name CLASSPATH and value will be  like C:\Program Files\Apache Software Foundation\Tomcat 9.0\lib\servlet-api.jar; for running Servlet. (After Tomcat Installation to test of running servlet)

Not required for running Jython on web, skip this:- [You should also add java and jython (after installation of Jython) path for running java command from anywhere. To do this click on Path variable then click on Edit button then click on New and add just put C:\Program Files\Java\jdk-10.0.2\bin for running Java and for C:\jython_2.2.1 (path of your jython home)]

You can see all values after setting this the screenshot below.




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>

No comments:

Post a Comment

Change image source dynamically on hyperlink

 Changing image source dynamically using JQuery. Here in this example I have created there hyperlink and stored all images in the same folde...