Standing in the Field @ Valley Forge

Standing in the Field

Notes from SJS Application Server Field Engineering

« Previous month (Feb 2005) | Main | Next month (Apr 2005) »
Monday March 28, 2005
Headless Jython Servlets

Tim Bray (the co-worker I developed my Jython instructions for) tells me that he needs to run his installation headless. Which I didn't document last time. So here is an addendum to my previous instructions if you don't have X11 access to your box.

Installing SJS Application Server Headlessly

When executing the SJSAS installer (j2eesdk-1401_2005Q1-solaris-i586.bin or similar), just add the argument of "-console". This runs the installer in text mode. Using the installer is generally self-explanatory but there some basic instructions displayed when the installer begins. Just type in the same options you would have selected in the graphical installer.

Installing Jython Headlessly

When executing the Jython installer add the argument "-o /install-jython". Adjusting for the directory where you want Jython, of course. ("java jython-21 -o /install-jython") This skips the installer completely and makes an installation in the specified directory.

Command line deployments

At this point you can perform the rest of the installation instructions as they are all browser and command line oriented. However, just for reference, I'll show how to use the command line to deploy the application template rather than the browser based admin GUI.

The deployment command is as follows: "/install-dir/bin/asadmin deploy --name jythondemo --contextroot jythondemo /install-dir/samples/quickstart/hello.war " The admin server must be started for this command to work.

(2005-03-28 09:22:15.0) Permalink Comments [1]


Thursday March 24, 2005
Jython Servlets

Background

A co-worker of mine was doing a project that used servlets written. Sean McGrath wrote a tutorial on getting this up and running. Unfortunately, the tutorial assumes that you will be deploying to Tomcat. I wanted to encourage my co-worker to use Sun Java System Application Platform Edition, so I volunteered to get Jython servlets running on Sun's appserver.

There really isn't any significant difference in the way Jython works on these two platforms, but for those who are new to SJS Application Server's filesystem layout and administrative interface this document may be helpful. After following these instructions you will want to go read Sean's original documentation as this document only covers the server setup and not the Jython programming.

All code is the property of Sean McGrath as I just tweaked the install instructions a little.

Pre-Requisites/Assumptions

This document was developed using Sun Java Application Server Platform Edition 8.1 2005Q1 UR1 on Solaris 10 x86 Edition. However, these instructions should work for any platform, and for any edition of SJS Application Server. (This document is written with UNIX style PATH names and command prompts, however. Adjust appropriately for Windows machines.)

These instructions do not assume root privileges, although you will need to choose directories and ports appropriate to your account. It is assumed that you have a monitor. There are headless installation alternatives available for both SJS Application Server and Jython, but they are not covered in this document.

Install Sun Java System Application Server

Download Sun Java System Application Server from http://www.sun.com/downloads. These instructions assume that you are using the "All-In-One-Bundle".

Make the downloaded file executable : "chmod +x j2eesdk-1401_2005Q1-solaris-i586.bin". (Your filename may differ if you are running a different patch level or platform.)

Now start your application server by executing "/install-dir/bin/asadmin start-domain" (adjusting for your installation directory). You should now be able to reach the application server on http://localhost:8080 and the adminstrative interface on http://localhost:4848 .

Install Jython

Download Jython 2.1 from http://www.jython.org/download.html .

(More detailed instructions can be found at http://www.jython.org/install.html . These documents include some platform specific notes and troubleshooting tips if you run into trouble.)

Jython is distributed as a self-extracting Java class file. You can run the installer by executing "java jython-21". If you do not have java in your PATH, you can use the java installed as part of the Application Server "/install-dir/jdk/bin/java jython-21".

You should now be able to bring up a jython command line. Execute "/install-jython/jython" (substituting your own install directory) and jython will give you an interactive command line after doing some initial processing. Type ^c twice or ^d once to exit from the command line.

Deploy a Simple Application as a Template

Rather than compiling and deploying a web application from the command line as the Sean does in his Tomcat instructions, we will use one of the sample applications that comes with SJS Application Server as a template.

We will expand this simple hello world application with Jython in the next section.

Add Jython Support to the Application

First we will add the jython library to the CLASSPATH of the server by placing it in the server's lib directory. Copy the jython.jar directory from your Jython install into the application server's lib directory. ( "cp /install-jython/jython.jar /intall-dir/lib/" )

Change your directory to the home of the application you just deployed by executing "cd /install-dir/domains/domain1/applications/j2ee-modules/jythondemo/WEB-INF". (As always, adjusting for your installation directory.) Here you will find the web.xml file that describes the application to the application server.

We will add a line to this configuration file that tells SJS Application to use Jython to execute requests ending in ".py". Replace the existing web.xml with the following:

<?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.4" 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 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>hello</display-name>                                 
      <distributable/>
      <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>                                      
    </web-app>

While we are in this directory we will also copy the Jython libraries into the application's web context so that we can use later. ("cp -r /install-jython/Lib lib")

Testing your Jython Installation

We will now use Sean's first Jython servlet to test that everything is working. Go up one directory to /install-dir/domains/domain1/applications/j2ee-modules/jythondemo/ ("cd ..") and create a JythonServlet1.py file with the following code:

    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>" +
        "<body><h1>Servlet Test</h1></body></html>")

You will now need to restart the server so that it picks up the changes to the web.xml file. (You can add more Jython Servlets without restarting: it is only the web.xml changes that require a restart.) Execute "/install-dir/bin/asadmin stop-domain" to stop the server and "/install-dir/bin/asadmin start-domain" to restart it.

Once the server has restarted you should be able to access your test Jython servlet from "http://localhost:8080/jythondemo/JythonServlet1.py".

Just to make sure that the Python library is accessable we will also deploy Sean's third sample. Put the following code in JythonServlet3.py

    import sys,calendar,time

    from java.io import *

    from javax.servlet.http import HttpServlet

    from JythonServletUtils import *


    class JythonServlet3 (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 3</title>")
      toClient.println ("<body><h1>Calendar</h1><pre>%s</pre></body></html>" %
        calendar.calendar(time.localtime()[0]))

    if __name__ == "__main__":
      JS3 = JythonServlet3()
      dummyRequest = DummyHttpRequest()
      dummyResponse = DummyHttpResponse()

      JS3.doPost (dummyRequest,dummyResponse)

You will also need to create the JythonServletUtils module referenced in that code. Create another file called JythonServletUtils.py with the following code:

    from java.lang import System

    class DummyHttpRequest:
      pass

    class DummyHttpResponse:
      def setContentType(self,t):
        System.out.println ("Content-Type:%s" % t)

    def getWriter (self):
      return System.out

After creating these files you should be able to access Sean's calendar sample on http://localhost:8080/jythondemo/JythonServlet3.py .

Summary

Hopefully all of these instructions worked for you and you are now productively writing Jython servlets. Make sure to read Sean's tutorial for more about programming servlets in Jython and keep watching his blog for more updates to the tutorial.

(2005-03-24 13:48:16.0) Permalink Comments [2]


Thursday March 10, 2005
Blogging is Good for your Career

Yeah, I know. Almost a month since my last post and a long time since I've been posting regularly. February was insane. In addition to just being a busy month in general, I took on a major project for my boss's boss that I knew I didn't have time for. Plus I was a speaker at CEC and had a huge amount of prep work for that. And then I got sick (probably from working the crazy schedule I was working).

But things are better now. Once I clear some backlog I have lots of stuff I want to post. But one thing caught my attention today that should be quick to post.

Tim Bray points out the benefits of blogging on your personal career. It made me laugh because it reminded me so much of Jonathan's talk at CEC.

Like many of Jonathan's talks, his opening line was: "Do you read my blog? You should.". His next comment (if I recall correctly) was encouraging the audience to blog. Immediately afterwards, he asks if John Clingan is in the audience. I've seen him do this multiple times now.

John is clearly a rock star. (Or techno celeb if you prefer Mary's terminology.)

When your boss's boss's boss's boss's boss's boss asks for you by name when walking into a crowd of thousands because of your celeb status, that has to be good for both your career and your job satisfaction.

I'll take it as a lesson learned and try to get back to blogging regularly. :-)

(2005-03-10 12:56:09.0) Permalink