PART-4
XSLT IN
JSP USING BEAN
R.S.RAMASWAMY
As
pointed out earlier, the most important requirement nowadays in designing
web-applications is clean separation of
code and presentation. In the above example of servlet,
it will be seen how easily we have presented the result in table-form though there was no such
code in java servlet's source-page. This is the chief
merit of xslt approach. However, it can be refined
still further by using jspbean for the logic and jsp page.
But, let us ponder over
this option , for a while.As
every developer would agree, it is much easier to develop and test our servlets , because they are compiled in jdk environment by
ourselves. But , a JSP page can be developed and
tested only in a jsp container like Tomcat.
If there are errors in our code, the Tomcat jsp
compiler throws up a stack-trace 'at our face', and it is not easy to find the
error from this trace. For trivial programs, it may not be too difficult but
for a fairly complicated program, it is extremely frustrating
, as the line numbers do not refer to jsp page
atall. Each time to recompile our beans
, put them in the proper folder
and restart the tomcat is time-consuming and tiresome.On
the other hand,
we do not need tomcat atall to test the
result of our xsl .All that we require is jdk1.4.We
can use the built-in xalan2 in
jdk1.4 and any ordinary browser will do
,to see the result. The xalan processor is extremely
fast and it is a very easy to examine the effect of various style-sheets on our
xml document. We can do the testing by simple command-line:
>java org.apache.xalan.xslt
-in
students.xml
-xsl xsl1.xsl
-out
result.htm
By
using doskey feature, we can just substitute various xsl files and see the effect in browser,immediately.. ( this
extremely perceptive point is from page:119 of JAVA & XSLT by Eric Burke..OReilly pub.The author effectively argues in favour
of a combination of servlet for creation of XML &
TrAX.,as we have done
already. )
It appears that both
ASP.net and JSP suffer from this time-consuming shortcoming.If
we adopt xslt , the problem is solved to a
certain extent from the view of developer.But
some other authors opine that XSLT is very slow and warn us to test the
comparative performance before deciding.According to them,
the worst feature of XSLT approach is that it is slow and takes up a lot of
system resources.However, Burke argues that "Much
too often many programmers focus on raw performance metrices
without any consideration for ease of development or maintainability by
non-experts. These decisions are hard and are often subjective, based on
individual experience and preferences."
------------------------------------
Anyway, the
traditional JSP approach is given below. We begin with the bean. We should
always use package for jspbean. In other aspects, the
bean code is hardly different from either the console-mode or servlet code. Place the beans classfile
in e:\tomcat\webapps\root\web-inf\classes\ourbeans folder.
// traxjspbean.java
=====================
package ourbeans;
import java.io.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
public class traxjspbean
{
public traxjspbean()
{ }
public
String callbean(String a,String
b)
{
String r=" ";
try
{
TransformerFactory
factory=
TransformerFactory.newInstance();
StreamSource xsl=new
StreamSource(a);
Transformer transformer=
factory.newTransformer(xsl);
StreamSource xml=new StreamSource(b);
StreamResult result=
new StreamResult("g:\\tempor.htm");
transformer.transform(xml,result);
FileInputStream fis=
new FileInputStream("g:\\tempor.htm");
DataInputStream dis=
new DataInputStream(fis);
String s=dis.readLine();
while(s!=null)
{
r=r+s+"<br>";
s=dis.readLine();
}
System.out.println(r);
}catch(Exception
e1)
System.out.println(""+e1); }
return r;
}
}
==========================================================
// traxjsp.htm
===============
<html>
<body>
<jsp:useBean id="bean1"
class="ourbeans.traxjspbean" />
<%
String s1=request.getParameter("combo1");
String s2=request.getParameter("combo2");
String
a="http://localhost:8080/"+s1;
String
b="http://localhost:8080/"+s2;
out.println(a);
out.println("<br>");
out.println(b);
String s=bean1.callbean(b,a);
out.println(s);
%>
</body>
</html>
================================================================
// traxjsp.htm
====================
<html>
<body>
<form
method=post
action='traxjsp.jsp'>
Select
file
<select
name='combo1'>
<option value="students.xml">students.xml
<option value="marks.xml">marks.xml
</select>
<br>
Select
Style
<select
name='combo2'>
<option
value="xsl1.xsl">style1
<option
value="xsl2.xsl">style2
<option
value="xsl3.xsl">style3
</select>
<br>
<input
type=submit>
</form>
</body>
</html>
Place the
jsp & htm files in root
folder.
Start the browser and give url as :
'http://localhost/traxjsp.htm'
and we get correct result.
----------------------------------------------------------
==========================================================
At this point, it is worthwhile to point out that
,
if we follow a systematic method
of encapsulating the logic in a class ( ie) bean, we
can use it in :
a) console
mode
b) stand-alone
gui frame
c) client-server
d) RMI
e) servlet
f) JSP
g) EJB
(Session-bean).
The essence of an Enterprise Application
is in business-logic (algorithm)which can be tested
even as a simple console program, as we did above in JAVA & C# too!
An IDE is just an artificat for making repetetive chores easier but can never be a substitute for
business logic.
It is a moot point whether the essence of the success of Google is in its search algorithm or in its user interface!
==========================================================
This tutorial has gotten
to be too long and hence we will deal with JSTL (Java Standard Tag Library ) in the next issue and see how the XML tags in JSTL
make the job still easier.
===================================================================
Suggested books for reading:
--------------------------
1) JAVA
& XSTL by Eric Burke ( OReilly)
2) XSLT by Doug Tidwell (OReilly).. Rs.100/-
3) Professional
JAVA-XML by KalAhmed & Sudhir Sncha
(
chapters 7 & 8) ..Excellent
4) Professional
XML DataBases
by
Kevin Williams & others
(chapter-8)