FAST way of generating JSP pages using EJB’s to provide the data!

Ok firstly sorry I’ve not posted much but had a lot of personal issues.

Anyway – here’s the story, we were using the awesome DHTLMX components with JSP’s feeding the data but we could never get the speed up especially since our source database had 270,000 records or so (and increasing!). The server is a GlassFish server by the way.

So basically the woodstock page replaced the DHTLMX combo boxes, and grid by feeding from JSP’s that opened a database in the standard way. Which is SLOOWWW.

By accident I found that moving the actual routine to an ejb with an entity manager made the code run about a million times faster. In fact when one of the customer’s date entry people tried it they nearly fell off their chair as it was so much faster than their existing system.

This technique can be used with just about any application to call a routine and return data, but it must be java.! Note I’m not a fan of XML because I think it gives back a whole load more data than it needs! But some of the components need it! 🙂

Ok – so 3 teirs.

JSP PAGE <—> EJB Remote Interface <—> EJB

Here’s the JSP page that generates the XML in the WAR project (web application)

<?xml version="1.0" encoding="ISO-8859-1"?>
<%@ page contentType="text/xml;charset=ISO-8859-1" %>
<%@ page import = "java.sql.*" %>
<%@ page import = "javax.naming.Context" %>
<%@ page import = "javax.naming.InitialContext; "%>
<%@ page import = "PHJ.JobManagerRemote.*" %>
<%



 PHJ.JobManagerRemote jobManagerBean=null;
 try {
 Context c = new InitialContext();
 jobManagerBean=(PHJ.JobManagerRemote)c.lookup("java:comp/env/JobManagerBean");
 } catch (Exception ne) {
 System.out.println("Could not find Job Manager Bean "+ne);
 }

 String sql = "SELECT TOP 40 tblAddressBook.AddrName, tblAddressBook.AddrNum, tblAddressBook.AddrCity, tblAddressBook.AddrCounty, tblAddressBook.ID ";
 sql=sql+ " FROM tblAddressBook ";
 sql=sql+" WHERE tblAddressBook.LastUsed>DATEADD (day,-60,GETDATE() )  ";


 if (request.getParameter("mask")!=null) {
 sql=sql+" and addrName like '"+(request.getParameter("mask").replace("'","''"))+"%' ";
 }
 sql=sql+" ORDER BY tblAddressBook.AddrName ";
 

// connect to database

 //  String connectionURL = "jdbc:sqlserver://" + db_ipp_addr + ":1433;databaseName=" + db_name;

 //  Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
 //
 
 out.println(jobManagerBean.CustomerAddressserverXML(sql));
 
%>

Look how tiny it is! 🙂

Ok So – then I have an EJB in an enterprise applcation called JobManagerBean. In the remote code we have….

public  String CustomerAddressserverXML(String sSQL);

and in the EJB itself…

@Override
 public  String CustomerAddressserverXML(String sSQL) {

 String sSQLTemplate = sSQL;
 List results = null;
 Query q = em.createNativeQuery(sSQLTemplate, TblAddressBook.class);
 
 try {
 results = q.getResultList();
 } catch (Exception e) {
 return "<complete></complete>";
 }

 String sResult = "";
// output data in XML format
 sResult = "<complete>";
 String sThisFields="";
 TblAddressBook thisCustomer = null;
 for (int i = 0; i < results.size(); ++i) {
 thisCustomer= (TblAddressBook) results.get(i);
 sThisFields=thisCustomer.getAddrName();
 sThisFields=sThisFields+" "+thisCustomer.getAddrNum();
 sThisFields=sThisFields+" "+thisCustomer.getAddrCity();
 sThisFields=sThisFields+" "+thisCustomer.getAddrCounty();
 sThisFields=sThisFields+" - "+thisCustomer.getId();
 sResult = sResult + ("<option value=\"" + (thisCustomer.getAddrName().replace("&","&amp;")) + "\"><![CDATA[" +
 sThisFields + "]]></option>");

 }
 sResult = sResult + ("</complete>");
 
 return sResult;



 }

Simples!

In a sense ofcourse you could move ALL your JSP code into EJB’s and actually have ONE JSP for your whole web site!

Advertisements

About Paul C

I am a Senior Java programmer/Web Technologies, Play in a band called Mandolin Monday, I have a cat and I live on a boat.

Posted on October 4, 2009, in Uncategorized. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: