alaJSP
JSP-similar processor
ver. 1.46


 What is for ?

      It is yet another servlet based preprocessor. The common idea behind that line of the products (see ColdJava site) is splitting static HTML presentation which done by designers and dynamic proceed developed by programmers.

    The main idea is simple. Just keep HTML pages as they are and especially keep HTML design process as it is. Just allow within the pages (and more precisely during the page output) create some java objects, run methods from that objects and maybe use their results during the page output.  Yes, all that can be done by servlets itself, but the idea is to hide servlets and keep current HTML development and deployment  as it is. At the end of the day it allows us to pay more attention to Java programming itself !

    What do we mean under that ? If you need some value produced by java function to be inserted into some place within your html-page (we are talking about dynamic content, yes ?) just directly mark that within this page. E.g.:

<html>
...
<!-- load class (create java object)
<%=new Balance();%>
<!-- use method from this class
<br>Your current balance is: <%=Balance.calculate();%>
...
</html>

    alaJSP server will create java object and insert results of call into html page before outputing. It is not a template based system. You do not need separate template file for marking placeholders positions. Your pages will translated (converted) into pure HTML before sending to browser.

    So, at the end of that: HTML pages in our approach are subject to be parsed by some application. It is a servlet of course !  HTML pages  may contain some tags for creating java objects and running some methods. These operations will be done during the HTML parsing stage.  Java objects may be created in the global context (so you can access to them from any future requests), in the session scope or only within the local context, what means they are exist only during the processing that page by that request.  (Different requests has got different local contexts of course). So we add also some application server elements here. And some of these classes can be preloaded during the processor's start. One of that classes (standard library) will be preloaded automatically. And you can use variables (also local,session and global) for saving some values. That is all.

   Tags are similar to JSP tags  (so why it is a-la JSP) and some simply (or stupid, to whom how) assumption were made about type castings during the creating/using java objects. See description below.

    Why it is not a full JSP ?  By our opinion the JSP is too complex. JSP completely switch the web-representation to java code. But in that case you can drop out JSP, JavaScript etc. and generate pages from your servlets. Currently, web-development (or design) is well supported process with many perfect visual tools. JSP are offering very big changes to that process.   So what is a point ?

    alaJSP is a small package and what is offered is just some set of simple entry points - how to bypass part of the process during the HTML-rendering to java applications.  The intersection with HTML stuff is a minimal to simplify the life of HTML developers. alaJSP was created as a clear description of several approaches we are using in our own servlet development during the last couple of years.
 

The common approach

 You should pass your pages as a parameters for alaJSP servlet.  So,  the first page in your project (site) you may point as a

 http://your_host/servlet/alaJSP?your_first_page_here

and within your pages just set all references to another your pages as a

<a href="http://your_host/servlet/alaJSP?your_real_page_here>
 

or you may redirect to servlet:

<HTML>
<BODY onload="document.location='http://your_host/servlet/alaJSP?your_first_page">
</BODY>
</HTML>

or you may add mapping (e.g. this was tested under JSWDK):

mappings.properties:

.ajsp=ajsp

servlet.properties:

ajsp.code=alaJSP
ajsp.initparams=admin=adm,classes=./WEB-INF/servlets,root=/home/myhome/jswdk-1.0-ea/webpages

in that case all files with extension .ajsp will be parsed by alaJSP servlet. So you can type directly:
http://your_host/test.ajsp

Remark:  see description below for initparams.

In any cases the main idea is that your pages will be preprocessed by alaJSP servlet.

    Java classes you load during that preprocessing should be placed in the special directory (you may set that in the properties file) or they should be accessible through CLASSPATH.  You can use Administrative console for class loading observe.
 

 Tags

    the common form is:    <%your_tag_here%>    In the current version the following list of tags is supported:

    =new class_name(list_of_parameters)
    =global new class_name(list_of_parameters)
    =class_name.method_name(list_of_parameters)
    =global class_name.method_name(list_of_parameters)
    =class_name.attribute.name
    =global class_name.attribute.name

    =Ident=expression
    =global Ident=expresssion
    =session Ident=expression

    =Ident
    =global Ident
    =session Ident

    delete ident
    delete global ident
    delete session ident

    include file=file_name
    include page=page_name

    if
    else
    fi

Remarks:
    1) all lines begins with <!-- are ignored as a comments during the preprocessing
    2) Ident - some identificator. All identificators are case sensitive.
    3) expression - method call (including creating new object), attribute request, Identificator or constant value.

The tag's semantic is:

=new class_name(list_of_parameters)   Create new exemplar of class within the current context. Uses list_of_parameters as a parameters for constructor.

For example:

<!-- create class -->
<%=new Session()%>

Nothing will be inserted into output stream after proceedings this tag.

=global new class_name(list_of_parameters)   The same as above but class will be created in the global scope.

Remarks

1) you may use =new global with the same semantic.

2) You may also use modificator unique during the object creation. For example the record:
=unique global new A()
means that object will be created only if it does not exist  (some like singleton concept).
 

=class_name.method_name(list_of_parameters)    run the function. The returned value (string representation) will be inserted into text. For function with the type void nothing will be inserted.

=global class_name.method_name(list_of_parameters)     The same as above, but we are looking class in the global context
In both cases class must be loaded before (by <%new class_name(constructor_parameters)%>

for example:

<!--  create object -->
<%new Session();%>

<form ...  >
<!-- get the default login name from java class Session -->
< input type="text" name="login" value="<%=Session.getDefaultLogin()%>" >
...
</form>
 

=class_name.attribute.name      Inserts the value of attribute.

For example:

<!-- get the default login name from java class Session -->
< input type="text" name="login" value="<%=Session.default_login%>" >
 

=global class_name.attribute.name        The same as above, but we are always looking the global scope.

Your function may returns for example some HTML-text. In that sense it is similar to server-side included.

Remarks:
    1) list_of_parameters is list of expressions (see expression definition above) splited by commas
 

Variables

You may enter variables and use these variables within your pages. Variables can have local, session or global scope. Local variables are visible only within the current request. Global variables are visible for all requests.

=Ident=expression - calculate alaJSP expression and save that value in the variable with the name Ident

=Ident=expression    the same as above

=global Ident=expression  the same as above, but uses global variable

=session Ident=expression also the same as above, but uses session scope

=Ident - inserts the value of variable
 

For example:

<%b=new B();%>
<%c=b.func(1);%>
<%=c%>

Variables names are case sensitive, and any local variable exists during the parsing of corresponding page as well as any included page (see include). As you can see alaJSP has not variable declarations and types will be detected in dynamic. So, you can reuse your variables. You can use variables instead of  class names (except new of course).
 

File (page) include

include file=file_name    Include the mentioned file. File will be parsed by alaJSP again.

include page=page_name  Include the mentioned file 'as is', without preprocessing by alaJSP

You can set file names in forms of http://some_path or file://some_path. By default we suppose file://

For example:

<br> include file <%include file=/home/web/page1.htm%>
<br> rest of the page : <%include page=my_page.htm%>

you can also use " for bounding file names:

<%include file="/home/web/page2.htm"%>
 

Delete object

you can delete any variables or class object.

delete Ident
or
delete global Ident
or
delete session Ident

this operator does not include any text into output page.

Remarks:

1) all variables and local class objects will deleted after completing the current request
2) in the form <%delete A%> alaJSP engine will try to delete at the first hand variable with
name A and after that class A if  such variable does not exist.
3) in case some object does not exist delete operator does not generate error message
 

Conditional preprocessing

if logical_exression

else

fi

E.g.

<!-- if result is not empty -->
<% if <%=b.func()%>!="" %>
....
<%else%>
....
<%fi>

The syntax description for logical expression is ([ ] means not mandatory elements, | means alternative):

logical_expression:= unary | binary
unary:= [ ! ] alaJSP_tag
binary:= operand operation operand
operand:= alaJSP_tag | constant
operation:=  == | != | <= | >= | < | >

alaJSP_tag - see expressions above

E.g.

<!-- your function has got type boolean -->
<% if <% =m.func()%> %>

<!-- your function has got type String -->
<% if <%=m.func1()%>=="olduser" %>

<!-- your function has got type int -->
<% if <%=m.func2()%> > 3 %>

You can also check variables intialization:

<!-- true, when global variable A exists
<% if <%global A != null %> %>
 

Sessions

Session will be created as soon as you try to set value for some object in the session scope. So if you have operator: <%session A="create new session";%>  and session doest exist at this moment new session will be created. You can use stdlib.isSession() call for checking your sessions.

Remark:
    1) alaJSP uses own session management (we need that for catching session timeouts and memory release). It is a class alaJSPSession.class
    2) you can use standard servlets session tracking stuff. See Tips.  Just if you do that do not mix these two ways.
 
 

Standard  library

Distributive version contains stdlib.class  This class will be loaded automatically during the running alaJSP
servlet. Current version has got the following list of functions

stdlib.ID()    - unique identificator
stdlib.IP()    - ip address of the client
stdlib.RANDOM(int a, int b) - random value in the intervale [a,b]
write(String) - just write an argument into output stream. (Remark: it is similar to javascript: document.write() call)
println(String) - the same as above

=COOKIE(String some_name)  inserts into output stream the value of mentioned cookie, or null if that cookie is not set. Mostly it is oriented for passing values to your java applications.

=REDIRECT(String url_name) - redirect to the mentioned URL. So, you can have for example some 'fictive' pages with java calls only:
<html>
<%=A.func1()%> <%=B.func()%>
<! -- etc. -->

<!-- redirect to the real page -->
<%=REDIRECT("http://your_real_page")%>
</html>

stdlib.HTML(String some_name)  inserts into output stream the value of current variable from the post request. (for servlets programmers: it is the same as a
getParamValue(some_name) ).
    You can use that for proceedings form submission. You can also use that as a parameter for class constructor or method call. In any cases this construction is a macros that inserts the value of appropriate HTML-form variable.
See example below about forms:

<!-- form action will be proceed by servlet -->
<form method="post" action="http://my_server/servlet/alaJSP?page2.htm">
<input type="text" name="Receipt">
<input type="submit" name="Button1" value="Ok">
</form>

and within page2.htm you can write some like that:

<html>
<!-- proceed in Java -->
<%=global Session.add(<%=stdlib.HTML(Receipt)%>,2)%>
...
</html>

during the preprocessing page2.htm  HTML(Receipt) will be replaced by the value of Receipt from the form.

stdlib.QUERY()  inserts into output stream the value of query string from the GET request.
stdlib.QUERY(String some_name)  inserts into output stream the value of current variable from the GET request. (e.g. your_file?some_name=value)

stdlib.servlet(String some_full_function_name)  allows you to pass servlet parameters HttpServletRequest, HttpServletResponse and PrintWriter to your own function. What does it mean:
<%stdlib.servlet(yourClass.yourFunc)%>  will calls function yourFunc from the class  yourClass and passes to that function three parameters mentioned above. Your function looks like:
public class yourClass
{
  public yourClass() { }
  public void yourFunc(HttpServletRequest req, HttpServletResponse res, PrintWriter out)
  {
    // and you can do here what you did in servlet's doGet/doPost
  }
}

=stdlib.isSession()  allows you to check current session. Return true or false. So you can use this call in if-tags:

<%if <%=stdlib.isSession()%>==true%>
 ...
<%else%>
...
<%fi%>

=stdlib.closeSession()  invalidate existing session. Type is void.

Remarks:

1)  for the compatibility with the old versions you may use prefix alaJSP rather than stdlib

2) For developers: you can replace this file (stdlib.class) with new one. For each function from the standard library alaJSP engine passes as a first three parameters HttpServletRequest, HttpServletResponse and PrintWriter output stream for the current request. So, for example function HTML looks so in the distributive:

public String HTML(Object _req, Object _res, Object _out, String s)
 {
   HttpServletRequest req=(HttpServletRequest)_req;
   String ans=req.getParameter(s);

   if (ans==null) ans="";
   return ans;
 }

3) if you are using stlib.servlet() call:
-  your called class should have public constructor without parameters.
- your user-defined function can also return some values to the preprocessor (so you can define that not only as a type 'void' )
- if you are using PrintWriter parameter for output some html-stuff it will  be inserted after the last preprocessed position.

4) you can set cookie by the javascript call. E.g. <script> document.cookie='a=12345' </script>

5) you can maintain sessions under alaJSP development either by your own (see Tips page) or through session variables. Function isSession() will works in both cases.
 

Parameters

In all cases when we are looking class constructor or class method we need to make some conclusion about
parameter types.
If the current parameter represents integer we assume this type is int
If the current parameter represents float we assume this type is float.
If the current parameter is true or false we assume this type in boolean
In all another cases we assume this type is String.

For example:

<!-- we assume A(int,float,String,boolean) -->
<%=new A(2,5.6,a123,true)%>

for making sure you pass some parameter as a String you can use "
<!-- pass value always as a  String -->
<%=Login.add("<%=stdlib.HTML(user)%>")%>

If some string value is enclosed into ", it will be passed to java without that.
E.g.

<!-- within A parameter type is a String and value is a 222 , not a "222" -->
<%=A("222")%>
 

In all cases if the engine cannot find class in the local context we are looking in the session scope (if we have that) and after that in the global scope.
 

Name space

For each identificator alaJSP parser first checks local context, after that session context (if session exists) and after that global context. You can of course set explicit context checking. E.g.

<!-- check all context
<%=A%>

<!-- check global context only
<%=global A%>
 
 

 Administrative console

    You can see list of loaded classes as well as global/local instances and manage by reloading process. For doing that you can run the same servlet with administrator's password. You can setup password as a property (initial parameter) for servlet. For example add the following lines to the servlet.properties file:

# alaJSP servlet
servlet.alaJSP.code=alaJSP
servlet.alaJSP.initArgs=admin=12345

the tag admin set your admin's password (in this case it is 12345).

in that case you can go to http://your_host/servlet/alaJSP?12345 for administrator's console. By default admin's password is 'welcome' (so http://your_host/servlet/alaJSP?welcome should bring up console).
 

Initial parameters

You may set also default (home) directroy for your classes. Any time you load class alaJSP class loader start looking this class at the first hand in the own home directory and only after that in directories defined by CLASSPATH. By default home directory is .  (in other words it is a home directory for your servletrunner). The name of the parameter is 'classes'. E.g.:

servlet.alaJSP.initArgs=admin=welcome,classes=/home/my_dir

You may set also root directory for your document files. It is suitable in case you add some mapping for files extension.
The name of the parameter is 'root'. E.g.:

servlet.alaJSP.initparams=admin=welcome,root=/home/webroot

and for http://host/your_file.ajsp   this file will be readed from /home/my_dir/your_file.ajsp

You may order preloading some of the classes during the alaJSP start. The name of the parameter is 'load'. For this parameter you can set file (full path) that will be parsed during the loading alaJSP (without output of html stuff) . So this page can contains any of operators available in alaJSP. Usually you can create some global classes:
E.g.: your file global.ajsp (you can use any name) contains some like that:

<%=global new A(); %>

and initparams line for servlet is:

servlet.alaJSP.initparams=admin=welcome,load=/home/alaJSP/global.ajsp
(or servlet.alaJSP.initparams=admin=welcome,load=c:\\alaJSP\\global.ajsp)
 

Remarks:
1) class stdlib will be loaded regardless from that settings anyway.
2) set the proper path for your global script. You should set here full path describing this file otherwise file will be readed from the root directory (see parameter 'root').
 

The current version restrictions:

- each tag must be placed on the one line (you can not open and close tag on the different lines)
- no nested if-tags (but you can use include tag for bypassing that)
- logical expressions for if-tag are limited (see above)
- within each context (global or local) you can have one instance of class with the same name.
(you can create such classes within the different local contexts - for the different requests - without any restrictions).
So for example:

<!--  create object -->
<%=new A(2)%>
...
<!--  new object will replace old one ! -->
<%=new A(5)%>

But you can use variables and keep different instances of the same class within the local context:

<%a1=new A(2)%>
<%a2=new A(5)%>

History

Examples:   Forum   Tips

For downloading:  The current distributive contains  four java classes: servlet itself , own class loader plus session management and standard library (you need all classes).

    alaJSP  newLoader  alaJSPSession  stdlib
 
 

© Dmitry Namiot  Coldjava