Quantcast

Question about Jython Servlet Development

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Question about Jython Servlet Development

jlist9
Hi all,

I'm trying to follow this page to develop a servlet using Jython:

I have two questions about what's described in that page.

1. Should all URLs end with ".py"?

I put the two servlet sections mentioned in the page in 
/pydev/WEB-INF/web.xml, and NewJythonServlet.py in /pydev directory.

If I use this URL, the page loads up fine in tomcat:


However, if I skip the ".py" in the end of the URL, I'll get a 404 error:

    HTTP Status 404 - Servlet NewJythonServlet is not available

It seems ".py" is always required in the URLs? 

2. I then removed the web.xml section below and the URL with ".py" 
seems to work fine and the URL without ".py" still doesn't work (as 
expected.) Is this section of web.xml needed at all? So far my
experience has been:

   a. the PyServlet section is required in web.xml for .py files to be compiled.
I suppose it's required in each app that uses Jython. I'm not sure how to
make a global change so that all applications on that container can share
the share the setting.

   b. the .py files need to be in the web page directory, in my case,
/pydev/NewJythonServlet.py. If I put them anywhere else (I haven't tried 
classpath) I get error 404 - Servlet <NewJythonServlet> is not available.

   c. for the Jython file to be called, the file name must be the same as
the file name specified in the URL, in my case NewJythonServlet.py.
If not, I'll get error 500 with "No such file or directory" as root cause.

   d. the class name inside the Jython file must have the same name as
the file name, excluding the ".py" part, in my case, NewJythonServlet.
If not, I'll get error 500 with "No callable (class or function) named" as root cause.

Is this the correct understanding?

<servlet>
    <servlet-name>NewJythonServlet</servlet-name>
    <servlet-class>NewJythonServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>NewJythonServlet</servlet-name>
    <url-pattern>/NewJythonServlet</url-pattern>
</servlet-mapping>

Thanks,
Jack

------------------------------------------------------------------------------
Better than sec? Nothing is better than sec when it comes to
monitoring Big Data applications. Try Boundary one-second
resolution app monitoring today. Free.
http://p.sf.net/sfu/Boundary-dev2dev
_______________________________________________
Jython-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jython-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Question about Jython Servlet Development

Alan Kennedy-2
[jlist9]
> 1. Should all URLs end with ".py"?

Yes.

The documentation comments for (very limited capability)
org.python.util.PyServlet is explicit about this

See line 58 here

http://hg.python.org/jython/file/ef7876da994f/src/org/python/util/PyServlet.java

> However, if I skip the ".py" in the end of the URL, I'll get a 404 error:
>
>     HTTP Status 404 - Servlet NewJythonServlet is not available
>
> It seems ".py" is always required in the URLs?

Yes. Unfortunately, PServlet is really more a demo than a production
ready piece of code, in that it is meant to illustrate how to embed
jython in servlets, but not necessarily cover all uses cases, e.g.
fancy URL mappings, module based functionality, etc.

See here for a patch to PyServlet which does not require .py at the end.

Make PyServlet use modules instead of file names
http://bugs.jython.org/issue1420

And another related, functionality breaking patch

Run __init__.py in PyServlet.
http://bugs.jython.org/issue483305

The [j|p]ythonic way to do web applications in jython is to use WSGI,
which is both robust and well supported, with modjy. See the
"modjy_webapp" directory in the "demo" directory.

>    a. the PyServlet section is required in web.xml for .py files to be
> compiled.
> I suppose it's required in each app that uses Jython. I'm not sure how to
> make a global change so that all applications on that container can share
> the share the setting.

This is documented and discussed in the modjy documentation.

>    b. the .py files need to be in the web page directory, in my case,
> /pydev/NewJythonServlet.py. If I put them anywhere else (I haven't tried
> classpath) I get error 404 - Servlet <NewJythonServlet> is not available.

This is because PyServlet uses getServletContext().getRealPath(spath)
to find files. Which means that it finds them relative to context root
for the servlet. It won't find them if they are placed anywhere else.

>    c. for the Jython file to be called, the file name must be the same as
> the file name specified in the URL, in my case NewJythonServlet.py.
> If not, I'll get error 500 with "No such file or directory" as root cause.

This is a limitation of PyServlet, another reason why it should only
considered a demo.

>    d. the class name inside the Jython file must have the same name as
> the file name, excluding the ".py" part, in my case, NewJythonServlet.
> If not, I'll get error 500 with "No callable (class or function) named" as
> root cause.

This is a limitation of PyServlet, another reason why it should only
considered a demo.

> Is this the correct understanding?

Yes, because of PyServlet's limitations.

Please consider using modjy and WSGI for jython web applications.

http://www.jython.org/jythonbook/en/1.0/SimpleWebApps.html#wsgi-and-modjy

Alan.

------------------------------------------------------------------------------
Better than sec? Nothing is better than sec when it comes to
monitoring Big Data applications. Try Boundary one-second
resolution app monitoring today. Free.
http://p.sf.net/sfu/Boundary-dev2dev
_______________________________________________
Jython-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jython-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Question about Jython Servlet Development

Stefan Eletzhofer
In reply to this post by jlist9
Hi,

Am 05.04.2012 um 16:21 schrieb jlist9:

> Hi all,
>
> I'm trying to follow this page to develop a servlet using Jython:
> http://www.jython.org/jythonbook/en/1.0/SimpleWebApps.html

Uhh,  methinks this is a bit out-of-date.

I'd rather look at the modjy package, which is essentially a
Servlet to WSGI gateway.  You can use it directly e.g. with a light
weight python web framework like flask or bottle, or even pyramid*.

And you can look at it's source code to get an idea how they do it.  There's
even a demo web app inside the Demos folder of the Jython package.

If I understand correctly, with the current jython implementation you
can **NOT** simply have a python class extend HTTPServlet, and have this
class be called from Java**.  You need to jump through some loops, essentially
extending HTTPServlet in Java, and delegating calls from there to Jython --
this is what modjy actually does.

I'm using modjy together with flask in production using 2.5.2.

HTH,
Stefan.

* be aware though that you need 2.5.3+ for SQLAlchemy to work.
** I think this used to work in 2.2, but since 2.5 the jythonc compile and
   all it's goodness is missing.

--
Stefan Eletzhofer
[hidden email]
http://seletz.github.com


------------------------------------------------------------------------------
For Developers, A Lot Can Happen In A Second.
Boundary is the first to Know...and Tell You.
Monitor Your Applications in Ultra-Fine Resolution. Try it FREE!
http://p.sf.net/sfu/Boundary-d2dvs2
_______________________________________________
Jython-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jython-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Question about Jython Servlet Development

Alan Kennedy-2
[Stefan]
> I'm using modjy together with flask in production using 2.5.2.

That's great!

I see that others have success running flask on jython also.

http://stackoverflow.com/questions/8010517/whats-the-best-way-to-deploy-a-flask-app-using-jython-on-tomcat

But unfortunately, jython is not listed as a deployment option for flask :-(

http://flask.pocoo.org/docs/deploying/

It would be great if someone could add some documentation for jython
and modjy inside servlet containers as a deployment option for flask.

https://github.com/mitsuhiko/flask/tree/master/docs/deploying

Alan.

------------------------------------------------------------------------------
For Developers, A Lot Can Happen In A Second.
Boundary is the first to Know...and Tell You.
Monitor Your Applications in Ultra-Fine Resolution. Try it FREE!
http://p.sf.net/sfu/Boundary-d2dvs2
_______________________________________________
Jython-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jython-users
Loading...