30-Nov-07 (Created: 30-Nov-07) | More in 'Howto'

05.04 How to use JSP and Aspire

You can use jsp with Aspire quite well. You will invoke the jsp page using a typical controller approach. Aspire will retrive the data set and transfers control to the jsp page. At the top of the jsp page you will retrieve this data set from the http request using an attribute name. What follows is a quick introduction to jsp and how to use jsps in aspire.

Create a JSP page

Consider the root of your web application under tomcat as


c:\my-web-app

Imagine a sub directory and a file as follows


c:\my-web-app\jsps\test.jsp

Using tomcat you will invoke this page using the following syntax


http://host:port/myApp/jsps/test.jsp

Where "myApp" is defined to tomcat to represent the "my-web-app" directory. So now you know how to create the jsp file that is recognizable to tomcat. Now let use what goes into the jsp file

Please note however that the invocation syntax for a jsp page is slightly different. Although you can invoke the jsp page directly as mentioned here you won't get the benefits of mvc unless you invoke it through aspire. Nevertheless it is quite reasonable sometimes to invoke the jsp page directly, especially public pages that do not need any data from the server side.

The jsp header

Any text file is a jsp file as well. But to be able to use Java APIs inside the body you will have to import some useful java classes. For Aspire the most useful imports are


<%@ page import="com.ai.htmlgen.*" %>
<%@ page import="com.ai.application.utils.*" %>
<%@ page import="com.ai.common.*" %>

You will place these three lines at the top of the page.

How to include another JSP file

One of the most useful things with JSPs is the ability to include other JSP files as server side includes. Here is the syntax and an example.


<%@include file="/myApp/jsps/testHeader.jsp" %>

How to get data into JSP from Aspire

Aspire stuffs the data object into the request using the name of "Aspire.formHandler". This data confirms to the "com.ai.htmlgen.ihds" interface. You can see the source code for the API to access this data


<% 
	ihds pageData = (ihds)request.getAttribute("Aspire.formHandler");
	if (pageData == null)
	{
	   out.println("Some html segment about empty data set if needed");
	   return;      
	}         
%>

How to write html from JSPs

Here is an example using the out.println


<% 
String say = "Hello world";
out.println(say);
%> 

Here is an inline usage of JSP


<p>This is html. Let me say <%=say%>

<!--*****************************************************-->
<h3>Here is how you can use key value pairs from ihds</h3>
<!--*****************************************************-->

<p>This is html line1
<p>This is line2. 
<%
	String key1=pageData.getValue("key1");
	String key2=pageData.getValue("key2");
%>
<p>Thirdline <%=key1> and <%=key2>

Please note in the example above, the "pageData" must be retrieved from the request as shown in the previous code.

Here is how you delve into the named loops of ihds


<select>
<%
	ihds foldersLoop = pageData.getChild("FoldersLoop");
	for(foldersLoop.moveToFirst();!foldersLoop.isAtTheEnd();foldersLoop.moveToNext())
	{
%>

<option value="<%=foldersLoop.getValue("folder_id")%>" > 
  <%=foldersLoop.getValue("folder_name")%> 
</option>

<%
	} //close the for loop
%>
</select>

Here is how you define data for this page


#
# Define the URL: ShowFiledReportsViewJSPURL
# Identify the transform as JSP
# Identify java class responsible for that transform
#
TestJSPURL=/jsps/test.jsp
TestJSPURL.transformType=JSP
request.TestJSPURL.transform.className=com.ai.jsp.JSPTransform
TestJSPURL.formHandlerName=TJU-FH

#
#Data definition
#
request.TJU-FH.form_handler.class_request.className=\
com.ai.htmlgen.DBHashTableFormHandler1

#
#Maindata request: Outside key value pairs
#
request.TJU-FH.maindataRequest.classname=com.ai.db.DBRequestExecutor2
request.TJU-FH.maindataRequest.db=reportsDB
request.TJU-FH.maindataRequest.stmt=\
\
select first_name as user_first_name \
	,last_name as user_last_name \
	,email as user_email \
from users \
where user_id = {ownerUserId.quote}

#
# A loop of data for a loop called FoldersLoop
#
request.TJU-FH.FoldersLoop.class_request.className=com.ai.htmlgen.GenericTableHandler6
request.TJU-FH.FoldersLoop.query_request.className=com.ai.db.DBRequestExecutor2
request.TJU-FH.FoldersLoop.query_request.db=reportsDB
request.TJU-FH.FoldersLoop.query_request.stmt=\
	select * from folders  \
	where 1=1 \
		and parent_folder_id is null \
		and public = 'Y' \
		and owner_user_id = {ownerUserId.quote} \
	order by folder_name 

Here is how you invoke the JSP page with Data


http://host:port/servlet/DisplayServlet?url=TestJSPURL

Notice how the "TestJSPURL" is translated into the real jsp page using the properties file to /jsps/test.jsp.

References

JSP syntax page from Sun

JDK 1.4 api docs. These will come handy when you are using java objects on the jsp page

Server side Java docs Once in a while you may have to refer to servlet related APIs in the JSP page