<!-- start session -->
<%id=stdlib.ID()%>
<!-- use id as a parameter for all pages -->
<a href="htpp://www.host.com/page1.htm?Id=<%=id%>">Next
page</a>
<a href="http://www.host.com/page2.ajsp?Id=<%=id%>">Yet
another page</a>
Within the page preprocessed by alaJSP you can use stdlib.QUERY("Id")
for getting that parameter.
Global variables
You can use global variables for some common settings. E.g. your intial script can have the following definition:
<%global Header="<b><center>Header</center></b>"%>
<%global Footer="<b><center>Footer</center></b>"%>
and within any of your pages you can use:
<%=global Header%>
...
<%=global Footer%>
Sessions-1
Remember that each function from stdlib will be called with default
parameters. One of this parameters is the current HttpServletRequest. So
you can extend stdlib with your own functions and within that functions
have standard sessions support from servlets API:
E.g. some like that
public void createSession(Object _req, Object _res, Object _out)
{
HttpServletRequest req=(HttpServletRequest)_req;
req.getSession(true);
}
Sessions-2
You can create session just by initiating some variable with the modificator 'session'. E.g.:
... some html stuff
<%session A="12345"%>
... some html stuff
In case session was not created before new session will be created at this moment (during the parsing this line).
Of course you can keep more complex object for clients session. E.g.:
<%session B=new ShopCart();%>
and on the some another page you can write:
<!-- assume that class ShopCart has got method getItemCount()
<%=session B.getItemCount();%>
Sessions-3
It was long time under discussion do we need session scope for classes in alaJSP or not ? At the end of the day "sessionists" win. So see Session-2 about session support. But the old way we used to do that is still working:
1) within the first (starting) page of your application you can generate
session Id:
<%id=stdlib.ID()%>
within this page you can use variable id for setting query parameter
for any of your links:
<a href ="http://your_host/your_page.ajsp?id=<%=id%>">Next page</a>
2) within any another page you can just obtain this id by using stdlib
call:
<%id=stdlib.QUERY("id")%>
and now you can use that id as a key for saving all your data e.g. in
some hash-table.
By the way, you can even check access rights by this id:
<%if <%=id%> == "" %>
<!-- no session id - user must login -->
...
<%else%>
<!-- Ok, we have got some session Id -->
...
<%fi%>
Scope
You can create objects and variables within 3 diffferent scopes. E.g.:
<!-- local variable
<%a=5;%>
<!-- session variable
<%session a=6;%>
<!-- local object
<=new B();%>
<!-- session object
<=new session B(); %>
Do we have this variable ?
you can test your variables with the help of IF tag:
<% if <% session A !=null %> %>
.....
<%fi%>
this exprsession is true when session variable A was created (initialized) before.
Cache
alaJSP is a preprocessor. So all calls (results) will be converted into HTML stuff. So you can use standard approach for cache management. E.g. add some like this to your html-file for cache preventing:
<META NAME="Pragma" CONTENT="no-cache">
<META NAME="Cache-Control" CONTENT="no-cache">
<META NAME="Expires" CONTENT="0">
You can use session (or global) variables for storing some values. This
way you can avoid re-calling methods.
<img src="http://myhost/f<%=stdlib.RANDOM(1,5)%>.gif">
<%=new myBean()%>
<%=myBean.setX("some value");%>
Value is:<%=myBean.getX();%>
Login:
or in html tags:
Login:
<form method=post action="http://host/servlet/alaJSP?firstpage.htm">
Name:<input type=text name="User">
<br>Pwd: <input type=password name="Pwd">
<br><input type=Submit value="Login">
</form>
file firstpage.htm can looks so:
<%=new Login()%>
<script>
if (<%=Login.Ok(<%=alaJSP.HTML(User)%>,<%=alaJSP.HTML(User)%>)%>!=1)
location.href="http://host/login.htm";
</script>
<p>Hello <%=alaJSP.HTML(User)%> from <%=stdlib.IP()%>
<br>Your password is <%=stdlib.HTML("Pwd")%>
So, we are passing form fields values to some java function and check results. In case of negative authorization we just redirect back to the login page. Class Login.java can looks so:
import java.io.*;
public class Login
{
public String Ok(String user, String pwd)
{
System.out.println("User:"+user);
System.out.println("Password:"+pwd);
// check user id and return
0 or 1
return "1";
}
}