Welcome, guest | Sign In | My Account | Store | Cart

These java classes implement a jython taglib which can be used to embed jython code into jsp pages. It consists of two tags: <jython:exec> ...some code... and <jython:get var=.../> With these two tags you can write active jython pages.

Python, 138 lines
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
## store this into classes/jython/get.java

package jython;

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import org.python.util.PythonInterpreter;
import org.python.core.*;

public class get extends TagSupport{
    public PythonInterpreter interp;
    public String cmd;
    protected PageContext pageContext;

    public get(){super();}
    public void setVar(String cmd){this.cmd=cmd;}
    public void setPageContext(PageContext pageContext) {
        this.pageContext = pageContext;
    }
    public int doEndTag() throws javax.servlet.jsp.JspTagException{
        try{
            if(pageContext.getAttribute("jythonInterp")==null){
                interp = new PythonInterpreter();
              pageContext.setAttribute("jythonInterp",interp,PageContext.PAGE_SCOPE);
            } else {
                interp=(PythonInterpreter)pageContext.getAttribute("jythonInterp");
            }

            String res=interp.eval(cmd).toString();
            pageContext.getOut().write(res);
        }catch(java.io.IOException e){
            throw new JspTagException("IO Error: " + e.getMessage());
            }
        return EVAL_PAGE;
    }
}


## store this into classes/jython/exec.java

package jython;

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import org.python.util.PythonInterpreter;
public class exec extends BodyTagSupport{
    public PythonInterpreter interp;

    public void setParent(Tag parent) {
        this.parent = parent;
    }

    public void setBodyContent(BodyContent bodyOut) {
        this.bodyOut = bodyOut;
    }

    public void setPageContext(PageContext pageContext) {
        this.pageContext = pageContext;
    }

    public Tag getParent() {
        return this.parent;
    }

    public int doStartTag() throws JspException {
        return EVAL_BODY_TAG;
    }


    public int doEndTag() throws JspException {
        return EVAL_PAGE;
    }


    // Default implementations for BodyTag methods as well
    // just in case a tag decides to implement BodyTag.
    public void doInitBody() throws JspException {
    }

    public int doAfterBody() throws JspException {
            String cmd = bodyOut.getString();
            if(pageContext.getAttribute("jythonInterp")==null){
                interp = new PythonInterpreter();
                interp.set("pageContext",pageContext);
                pageContext.setAttribute("jythonInterp",interp,PageContext.PAGE_SCOPE);
            } else {
                interp=(PythonInterpreter)pageContext.getAttribute("jythonInterp");
            }
            interp.exec(cmd);
            return SKIP_BODY;
    }

    public void release() {
        bodyOut = null;
        pageContext = null;
        parent = null;
    }

    protected BodyContent bodyOut;
    protected PageContext pageContext;
    protected Tag parent;
}

## store this into jsp/jython.tld

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">

<taglib>
  <tlibversion>1.0</tlibversion>
  <jspversion>1.1</jspversion>
  <shortname>jython</shortname>
  <info>
  A simple Jython tag library
  </info>
  <tag>
    <name>exec</name>
    <tagclass>jython.exec</tagclass>
  </tag>
  <tag>
    <name>get</name>
    <tagclass>jython.get</tagclass>
    <bodycontent>empty</bodycontent>
    <attribute>
        <name>var</name>
        <required>true</required>
    </attribute>
  </tag>
</taglib>


## add this to the web.xml file

<taglib>
  <taglib-uri>http://www.jython.org</taglib-uri>
  <taglib-location>/WEB-INF/jsp/jython.tld</taglib-location>
</taglib>

compile the two java classes, copy the jython.tld file to /WEB-INF/jsp and insert the xml fragment into the web.xml file. Then you are ready to write your own jython server pages like this: <html> <%@ taglib uri="/WEB-INF/jsp/jython.tld" prefix="jython" %> <head> <jython:exec> import sys, java </head> <body> <h2>The system path we use is:</h2> <jython:get var="sys.path"/><br> <h2>Other system attributes are:</h2> <jython:get var="str(sys.__dict__)"/><br> <h2>The java system properties are:</h2> <jython:get var="java.lang.System.getProperties().toString()"/><br> </body> </html>

1 comment

Vladimir Blagojevic 19 years, 5 months ago  # | flag

Jython can not lod imports. The class Exec that invokes the jython interpreter sees all the web application libraries (e.g. jython.jar), but when the interpreter tries to load an imported java class (imported in a jython tag for example) from a jar from the included libraries, it does not find them.

We saw that if before we create the interpreter we call PythonInterpreter.initialize, where we pass "java.class.path=$webapp_classpath" with the first Properties parameters, the interpreter finds libraries listed under the $webapp_classpath. We temporarily solved it by hard coding our class path in there.

We can not find a (nice) way to get the webapp libraries from within the code, so that we can pass them to the initialize method.

Created by Ferdinand Jamitzky on Mon, 17 Nov 2003 (PSF)
Python recipes (4591)
Ferdinand Jamitzky's recipes (7)

Required Modules

  • (none specified)

Other Information and Tasks