Disclaimer

This is not a comparison of features. I am trying to explain web2py to Java programmers.

Overview

In J2EE and Java based frameworks the separation between the Model (the data representation), the View (the presentation layer) and the Controller (the business logic and workflow) is achieved by the following components:

Python is more expressive than Java and therefore it tends to be more compact and more readable.

In both cases a URL is mapped into a function call HelloWorld(). In web2py one can use regular expression to map any URL path into this function but there is a default URL associated to it.

A web2py controller function can return a string (as in the above example). In this case the string is returned to the browser. The most common situation is that the controller function returns a set of variables stored in a Python dictionary. In this case the dictionary is transformed into a web page by the associated view. For example

def HelloWorld():
return dict(message='Hello World')

This page would ber rendered by a view called HelloWorld.html. If such view does not exist, the built-in generic.html is used instead. This allows to create controllers and fully functional web application without writing any HTML. The view, the presentation layer, is typically added as a last step in development.

In web2py, the controller has access to the following objects (request, response, session, cache) and they can be used, for example, to read request GET/POST variables and environment variables (according to the WSGI specs) as in this example:

The code in {{ }} is pure Python code (not a subset). Blocks start with a line ending in colon and stop with the keyword pass.

As in JSP, web2py views can extend and include other views:

{{extend 'layout.html'}}
{{for i in range(1,10):}}
Hello Number {{=i}}<br/>
{{pass}}

layout.html comes with web2py but it is easy to create other layouts using HTML or using our layout designer.

Notice that:

in web2py all the output printed with {{=..}}, by default, is escaped to prevent possible XSS vulnerabilities. In Java it is up to the developer to remember to explicitly escape the output.

the use of the {{ }} tags instead of the < > tags has been chosen because it is transparent to WYSIWYG HTML editors and thus they can be used with web2py without special plug-ins.

Data Persistence and Models

Any web application needs persistence. In Java this is achieved by JavaBeans. There are JavaBeans to handle session variables and there are JavaBeans to interact with the database. web2py uses three distinct mechanisms to handle persistence: sessions, ORM, and cache.

session variables

session is a built-in object used to store session variables. When queried for an attribute that does not exist (if session.c) is False. When an attribute is set (session.c=1) this is stored in the session. By default sessions are stored in files and locked when used. Optionally they can be stored in the database. Almost any Python object can be stored into a session. web2py automatically creates session cookies.

Object Relational Mapper

J2EE, out of the box, does not have something equivalent to an ORM. Perhaps you can get one from a third party vendor but I have never seen one. web2py's ORM provides a API to interact with the database that is completely independent on the database backed. This is not the same as JDBC! JDBC is all about connecting to the database, sending SQL queries and a retrieving response but the developer still has to write SQL and the SQL queries depend on the dialect of the database back-end. If you develop your Java+JDBC app using Oracle and you decide to move to Postgresql (or vice versa), you have to rewrite most of the SQL queries.

The web2py ORM (as any Python ORM) writes the SQL for you, in real time, with no overhead, preventing SQL injections, for any supported database back-end.

The web2py ORM (unlikely many Python ORM) does migrations. It means after you connect to the database and define a table, if the table does not exist, it will create it; if it exists but is different, it will alter it accordingly. Here is an example or ORM usage:

cache

web2py can cache any function in ram (cache.ram), on disk (cache.disk), or using memcache. Any function includes results of select, controller functions, views, etc. Here is how to cache a controller for 60 seconds

The second function is the controller (which we choose to call "handler") and it exposes as XML-RPC the function add. One can expose multiple functions via the same "handler" function.

We really do not feel we need to add any comment here.

Internationalization

In web2py:

T('this is a message')

(and as usual no need to import anything since you are supposed to use T)
You web2py you can also do

T('this is message %(name)s',dict(name='xxx'))

In both cases the expression is evaluated lazily when displayed in views.

In web2py, the web based administrative interface provides a translation page interface.

To create a language file in web2py just type its name (for example it-it.py ) in the admin interface.

What else?

web2py comes in one executable package including an SSL enabled web server, the sqlite database, a web administratve interface that allows you to create/edit/deploy and manage all your applications.

Exception are automatically caught by web2py which logs the traceback, the code causing the exception, and issues a ticket to the visitor that triggered the exception. No code is exposed to the visitor, not even by mistake, ever.

web2py has helpers that help you build objects that can be serialized in HTML or XML. Any correct HTML/XML can be generated using exclusively helpers.

web2py code runs on the Google App Engine.

web2py packages everything you need in one binary file that you don't even need to install. I keep mine on a USB stick. You just click on it and it start web server, the sqlite database, fires the browser for access to the administrative interface.

web2py deals with static files for you, streams them when they are large (both in upload and download), and automatically supports IF_MODIFIED_SINCE and PARTIAL CONTENT. You can do streaming audio and video without any extra tools or settings.

web2py can map URLs using regular expressions so that you can use it to handle legacy URLs.

web2py has no configuration files. You just create an empty app via the shell or the web based admin interface and create/edit your models, controllers and views (using an editor or the web based admin interface).

There is a repository of free web2py apps here and an interactive FAQ there.