Jython - Servlets



A Java servlet is the most widely used web development technique. We can use Jython to write servlets and this adds many more advantages beyond what Java has to offer because now we can make use of the Python language features as well.

We shall use the NetBeans IDE to develop a Java web application with a Jython servlet. Ensure that the nbPython plugin is installed in the NetBeans installation. Start a new project to build a web application by choosing the following path - File → New Project → Java web → New Web Application.

Provide the Project name and location. The IDE will create the project folder structure. Add a Java servlet file (ServletTest.java) under the source packages node in the Projects window. This will add servlet-api.jar in the lib folder of the project. Also, let the IDE create the web.xml descriptor file. Add the following code in ServletTest.java.

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ServletTest extends HttpServlet {
   
   public void doGet (HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
      doPost(request, response);
   }
   
   public void doPost (HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
      response.setContentType ("text/html");
      PrintWriter toClient = response.getWriter();
      
      toClient.println (
         "<html>
            <head>
               <title>Servlet Test</title>" + "
            </head>
            <body>
               <h1>Servlet Test</h1>
            </body>
         </html>"
      );
   }
}

The web.xml file created by NetBeans will be as shown below −

<web-app>
   <servlet>
      <servlet-name>ServletTest</servlet-name>
      <servlet-class>ServletTest</servlet-class>
   </servlet>
   
   <servlet-mapping>
      <servlet-name>ServletTest</servlet-name>
      <url-pattern>/ServletTest</url-pattern>
   </servlet-mapping>
</web-app>

Build and run the project to obtain the text Servlet Test appearing in <h1> tag in the browser window. Thus, we have added a regular Java servlet in the application.

Now, we shall add the Jython Servlet. Jython servlets work by means of an intermediate Java servlet is also known as PyServlet. The PyServlet.class is present in the jython standalone.jar. Add it in the WEB-INF/lib folder.

The next step is to configure the web.xml to invoke the PyServlet, whenever a request for any *.py file is raised. This should be done by adding the following xml code in it.

<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>PyServlet</servlet-name>
   <url-pattern>*.py</url-pattern>
</servlet-mapping>

The full web.xml code will look as shown below.

<web-app>
   <servlet>
      <servlet-name>ServletTest</servlet-name>
      <servlet-class>ServletTest</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>ServletTest</servlet-name>
      <url-pattern>/ServletTest</url-pattern>
   </servlet-mapping>
   
   <servlet-mapping>
      <servlet-name>PyServlet</servlet-name>
      <url-pattern>*.py</url-pattern>
   </servlet-mapping>
</web-app>

Place the following Jython code in the WEB-INF folder inside the project folder as JythonServlet.py, which is equivalent to the previous ServletTest.java.

from javax.servlet.http import HttpServlet
class JythonServlet1 (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>Servlet Test</title>" + "
            </head>
            <body>
               <h1>Servlet Test</h1>
            </body>
         </html>"
      )

Build the project and in the browser open the following URL −

http://localhost:8080/jythonwebapp/jythonservlet.py

The browser will show the Servlet Test in <h1> tag as in case of Java Servlet output.

Advertisements