View on GitHub

Weblets

weblets full project mirror

Download this project as a .zip file Download this project as a tar.gz file

Usage of Weblets in a JSP Centric Environment

As Weblets originated from a project which was developed for an Ajax and JSF centric book. Weblets has extensive tooling support for JSF , but it tries to cover as many web frameworks as possible in at least a generic way. And the most important ones in native ways if possible.

Therefore, native support of JSP as a very important frontend technology was important.

Setup

The setup is already described in the Setup guide .

To get Weblets up and running you need a valid Weblets servlet configuration. Unlike JSF we do not have any automatisms in place. The configuration has to be done explicitly, with a path pattern as trigger for the servlet.

    <servlet>
        <servlet-name>Weblets Servlet</servlet-name>
        <servlet-class>net.java.dev.weblets.WebletsServlet</servlet-class>
    </servlet>


    <!-- The <<<Weblets>>> servlet mapping must be path based otherwise <<<Weblets>>> will fail! -->
    <servlet-mapping>
        <servlet-name>Weblets Servlet</servlet-name>
        <url-pattern>/weblets/*</url-pattern>
    </servlet-mapping>

More information on the entire web.xml configuration can be found under Weblets:Setup guide

Basic Functions

Introduction

The Weblets APIs follow the general patterns as described in Weblets Users Guide: Introduction to the API Patterns . for further information please follow the link.

JSP Frontend APIs

Over the years JSP has evolved, Weblets tries to cover JSP as extensively as possible. Hence, it provides several APIs to the frontend developer, which can be used accordingly to the JSP version in use.

The Basic JSP Bean API

Weblets provides as the most basic and most compatible mechanism a simple bean, which can be utilized by the backend or frontend, you can use scriptlets to access this bean. It should be compatible with all JSP versions, although we have more elegant and simpler solutions for JSP2.x.

Code 3: Accessing <<<Weblets >> via beans and scriptlets>

    <jsp:useBean class="net.java.dev.weblets.WebletUtils" scope="application" id="jspweblet" />
    bean definition in your header
    
    <%= jspweblet.getResource( "weblets.demo","/welcome.js") %>
    <%= jspweblet.getURL( "weblets.demo","/welcome.js") %>

As you can see we include our utils class as simple JSP bean, the bean then provides our resource and url functions.

EL Functions for JSP 2.x

JSP2 provides newer EL implementations which have so called EL Functions , those are functions which can be added extend the actual functionality provided by standard EL. Weblets provide such function

The next example shows how to use the EL functions:

Code 5: Usage of <<<EL >> functions

     xmlns:wbl="http://weblets.dev.java.net/tags_jsp2" this has to be added to your namespace definitions

      <h:outputText value="${weblet:url('weblets.demo','/welcome.js')}" />
      <h:graphicImage value="${weblet:resource('weblets.demo','/img/icon_alert.gif')}" />

JSP Backend APIs

The JSP backend APIs are the same as the servlet APIs for further reference see Weblets: Java APIs }

Additional notes

For the usage of JSP scriptlets, do not use the weblet name for your bean name, it is reserved by weblets itself, if you do use it in a mixed jsf jsp enviornment you will get name clashes with jsf - Weblets internal beans that way. Hence all the demos default to jspweblet in our case! In a pure jsp environment the usage of the weblet as bean variable name is safe, but it still is not advicable to use it, it might block the path to further extensions in your application or it might block the path to future weblet updates!