Tuesday, August 10, 2010

Installing Rhino on mac

From Rhino :
Rhino is an open-source implementation of JavaScript written entirely in Java. It is typically embedded into Java applications to provide scripting to end users.

Rhino is an implementation of JavaScript in Java. Features include full implementation of JavaScript 1.7 and allows direct scripting of Java and more...

There are some good frameworks available like this one from my friend Fred Polgrady, http://github.com/sustainablecode/datastore

I will provide an easy way of installing Rhino on a mac. Follow these three steps for an easy installation...

1. Download Rhino

2. Create "Java/Extensions" in home directory "~/Library/Java/Extensions"

3. Copy the js.jar file from the downloaded Rhino zip file to "Extensions" directory.

Thats it!! You are all set to use Rhino.

$ java org.mozilla.javascript.tools.shell.Main

Rhino 1.7 release 2 2009 03 22

js> eval("print('hello')")


Thats all!!

Monday, August 9, 2010

Java Servlet Specification 3.0 - Overview and Quick Intro

Java Servlet Technology which is the widely accepted technology for creating dynamic java web applications and which is behind all the modern java web frameworks. This major release tries to address the most wanted featues from the community as a JSR and is approved in the form of JSR 315 and is planned to be part of JEE6 (JSR 316). The main focus for this release in helping in ease of developemnt using Java annotations, Pluggability and extendability of using fragments, Asynchoronos Servlet support and security enhancements.
Ease of Development
Using annoations Servlets join the band wagon of using declarative-style programming along with JPA, EJB3, Spring and the rest. This helpa developing Servlets, Filters more easily with the code and the related configuration in the same place and thus making the web.xml an optional. It's the Servlet 3.0 supported contianer which will process all the classes at boot time to find out all the annotated servlets, filters and listener classes and made them available for serving. If you don't want the container to process all the class files to figure out the annotated classes, you can turn it off using the new web.xml configuration parameter metadata-complete on web-app element to true. The other important addition which helps in ease of development and configuration is programmatic setup of serlvets and filters.

The new sample web.xml configuration

<web-app xmlns=“http://java.sun.com/xml/ns/javaee” xsi=“http://www.w3.org/2001/XMLSchema-instance
                schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
        <display-name>Sample Web Application to test Servlet 3.0 </display-name>

@WebServlet and @WebFilter are the two main annotations for configuring the servlets and filters in Servlet 3.0.

The urlPatterns attribute of the @WebServlet annotation qualifies the URL's for this servlets to respond.

@WebServlet(name = "helloServlet", urlPatterns = {"/hello"})
public class HelloServlet extends HttpServlet {
         protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                PrintWriter writer = response.getWriter();
                writer.print("<h1>HttpServlet using Servlet 3.0</h1>");

Note that the class still needs to extends HttpServlet (It is a mandatory requirement)

Servlet filters and Listeners follow the same pattern except that the class needs to implement Filter and ServletContextListener classes and the annoation @WebFilter, @WebListener respectively

Asynchronous Support
Asynchronous Http Request Processing (aka Comet) is a way that allows you to process a single Http Request using non-blocking I/O. More details about Coment and Asynchronous programming here(http://en.wikipedia.org/wiki/Comet_%28programming%29). Basically all this means is your servlet can continue working on the request while waiting for some other resource to be released or some other operation to be done like a database update.
To enable async support for your servlet, you can enable it using asyncSupported attribute of @WebServlet to true. Enabling this will give you support for few new methods in the Servletrequest itself inclluding startAsync(servletRequest, servletResponse), startAsync(), and getAsyncContext(). Once async attribute is set on the Servlet class you should either call startAsync(servletRequest, servletResponse) or startAsync() method to make an asynchronous request.
A simple example:

@WebServlet("/foo" asyncSupported=true)
public class MyServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res) {
AsyncContext aCtx = request.startAsync(req, res);
ScheduledThreadPoolExecutor executor = new ThreadPoolExecutor(10);
executor.execute(new AsyncWebService(aCtx));

public class AsyncWebService implements Runnable {
AsyncContext ctx;
public AsyncWebService(AsyncContext ctx) {
this.ctx = ctx;
public void run() {
// Invoke web service and save result in request attribute
// Dispatch the request to render the result to a JSP.

More detailed tutorial on writing Asynchronous Servlets in my next entry.

A new notion called web fragments has been introduced in servlet 3.0 specification which helps reduce the configuration. A webfragment is a part or all of web.xml configuration that can be specified by any framework or a jar file dropped into the web application. In this way the framework can by default configure itself to work with the application with out the application developer confioguring it.