Plugin 4: dynamic WebService Invoker

Hi Robert,

Thank you for your reply, I do appreciate it. I notice you have 2260 posts to this forum - you are generous with your time and new people like me and very grateful for guys like you! Yes, if you could post a simple JSP example that would be great. I’m currently learning about what I need to learn (if you know what I mean)! I have had a look at the HC API - am I correct in thinking that I can call a Servoy method via a JSP to parse the XML, then fire off other Servoy methods to manipulate the data, apply business logic etc? If that is the case, then I am getting somewhere!

I have had a look at the HC API - am I correct in thinking that I can call a Servoy method via a JSP to parse the XML

Yes that is correct.

Have you seen/read this document: http://www.servoy.com/docs/servoy_headless_client.pdf

Hi full_noize,

<%@ page import = "java.util.*" %>
<%@ page import = "com.servoy.j2db.server.headlessclient.*" %>
<%@ page import = "com.servoy.j2db.dataprocessing.IDataSet" %>
<%
	
	//
	// Bind the JSP session with the Servoy session
	//
	ISessionBean servoy_hc = (ISessionBean)session.getAttribute("servoy"); 
	if (servoy_hc == null) 
	{ 
		servoy_hc = HeadlessClientFactory.createSessionBean(request,"mySolutionName"); 
		session.setAttribute("servoy",servoy_hc); 
	}
	boolean ok = servoy_hc.setMainForm("myFormName");
	if (!ok)
	{
		// Seems it didn't work, lets try it again
		servoy_hc = HeadlessClientFactory.createSessionBean(request,"mySolutionName"); 
		session.setAttribute("servoy",servoy_hc); 
	}
	ok = servoy_hc.setMainForm("myFormName");
	
	//
	// Get the parameters of this request
	//
	String nParam1		= request.getParameter("param1"); // some parameters in the URL
	String nParam2		= request.getParameter("param2");
		
	//
	// Send the query to the Servoy solution with the parameters
	//
	String sResult = (String)servoy_hc.executeMethod(null, "mySolutionMethod", new Object[]{nParam1, nParam2});
	out.println(sResult); // return the result to the browser

%>

When you don’t want to open a new headless client all the time for each session then you can bind the session to the application like so:

<%@ page import = "java.util.*" %>
<%@ page import = "com.servoy.j2db.server.headlessclient.*" %>
<%@ page import = "com.servoy.j2db.dataprocessing.IDataSet" %>
<%
	
	//
	// Bind the JSP session with the Servoy application
	//
	ISessionBean servoy_hc = (ISessionBean)application.getAttribute("servoy"); 
	if (servoy_hc == null) 
	{ 
		servoy_hc = HeadlessClientFactory.createSessionBean(request,"mySolutionName"); 
		application.setAttribute("servoy",servoy_hc); 
	}
	boolean ok = servoy_hc.setMainForm("myFormName");
	if (!ok)
	{
		// Seems it didn't work, lets try it again
		servoy_hc = HeadlessClientFactory.createSessionBean(request,"mySolutionName"); 
		application.setAttribute("servoy",servoy_hc); 
	}
	ok = servoy_hc.setMainForm("myFormName");
	
	//
	// Get the parameters of this request
	//
	String nParam1		= request.getParameter("param1"); // some parameters in the URL
	String nParam2		= request.getParameter("param2");
		
	//
	// Send the query to the Servoy solution with the parameters
	//
	String sResult = (String)servoy_hc.executeMethod(null, "mySolutionMethod", new Object[]{nParam1, nParam2});
	out.println(sResult); // return the result to the browser

%>

Just keep in mind that methods won’t run threaded but sequential inside 1 instance of Headless Client and using a application bound HC then all globals are shared with all the users (hey, it’s the same session).

Hope this helps.

Thanks Paul!!!

Hi Robert,

Thank you very much, that is very helpful.

It took me a bit to realize that you can cast the return type of the executeMethod() function (thanks Jan) so here’s a tip. Casting as an IDataSet is very useful as this object has a standard array of processing functions in headless client:

<table>
<%
	// table output from servoy dataset returned in an IDataSet
	IDataSet setData = (IDataSet)servoy_hc.executeMethod(null,"returnSet",null);
	for (int i = 0 ; i < setData.getRowCount() ; i++)
	{
		out.print("<tr>");
		out.print("<td>" + i + "</td>");
		Object[] row = setData.getRow(i);
		for (int j = 0 ; j < row.length ; j++) {
			out.print("<td>" + row[j] + "</td>");
		}		
		out.print("</tr>");
	}
%>
</table>

The return type from the Servoy function needs to be a dataset. For some reason “return databaseManager.convertToDataSet(foundset)” doesn’t work though to return a foundset. So instead:

function returnSet()
{
	var dataset = databaseManager.getDataSetByQuery(
					controller.getServerName(),
					"select * from " + controller.getTableName(),
					null, -1)
	return dataset
}

Datasets are extremely easy to manipulate on the Servoy side from whatever source. The only logic left for your jsp page is to write out the results.