Kbase P47238: How to make a servlet working with AppServer ?
Autor |
  Progress Software Corporation - Progress |
Acesso |
  Público |
Publicação |
  09/10/2003 |
|
Status: Unverified
GOAL:
How to make a servlet working with AppServer ?
FIX:
Generate Java proxy classes for your 4GL code running on the AppServer.
A servlet ones initialized with establish a permanent connection with the AppServer. The request coming on the Servlet will run concurently getting data from AppServer.
// SessionExample.java begin
import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import util.HTMLFilter;
import util.Sample1;
import com.progress.open4gl.*;
public class SessionExample extends HttpServlet {
ResourceBundle rb = ResourceBundle.getBundle("LocalStrings");
SDOAppObject appObj;
public SessionExample()
{
System.out.println( "---> Session Example Initializing ... " );
try {
RunTimeProperties.setWaitIfBusy();
appObj = new SDOAppObject( "AppServer://localhost:5162/asbroker1",
"", "", "" );
} catch ( Exception e ) {
System.out.println( "---> Error: " + e.getMessage() );
e.printStackTrace();
}
System.out.println( "---> Session Example Initializing ... DONE " );
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body bgcolor=\"white\">");
out.println("<head>");
String title = rb.getString("sessions.title");
out.println("<title>" + title + "</title>");
out.println("</head>");
out.println("<body>");
System.out.println( "---> Calling AppServer ... " );
Sample1 sample = new Sample1( appObj, "cust_q.w", out );
sample.run();
// Thread thread = new Thread( new Sample1( appObj, "cust_q.w", out ) );
// thread.start();
System.out.println( "---> Calling AppServer ... DONE " );
out.println("</body>");
out.println("</html>");
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
doGet(request, response);
}
}
// SessionExample.java end
// Sample1.java end
package util;
import java.io.*;
import com.progress.open4gl.*;
import java.sql.*;
public class Sample1 implements Runnable
{
public Sample1( SDOAppObject appObj, String pname, PrintWriter out )
{
this.appObj = appObj;
this.procName = pname;
this.out = out;
}
public synchronized void run()
{
System.out.println( "---> RUN starting ... " );
long time1, time2;
time1 = System.currentTimeMillis();
SDOResultSet rSet;
try {
if( ( rSet = createResultSet( appObj, procName ) ) != null )
{
doSomething( rSet );
rSet.close();
}
} catch ( Exception e ) {
System.out.println( "---> " + e.getMessage() );
e.printStackTrace();
}
time2 = System.currentTimeMillis();
System.out.println( "Time:"
+ Thread.currentThread().toString()
+ " ---> " +( time2 - time1 ) / 1000 );
}
public boolean createResultSet( String pname )
{
try {
rSet = appObj._createSDOResultSet( pname );
} catch ( Exception e ) {
e.printStackTrace();
return fal.se;
}
return true;
}
public SDOResultSet createResultSet( SDOAppObject appObj, String pname )
{
SDOResultSet rSet;
try {
rSet = appObj._createSDOResultSet( pname );
} catch ( Exception e ) {
e.printStackTrace();
return null;
}
return rSet;
}
public void doSomething( SDOResultSet rSet ) throws Open4GLException, SQLException
{
boolean output=false;
int ii=0;
// rSet.first();
while( rSet.next() )
{
ii++;
if(output)
System.out.println(
Thread.currentThread().toString()
+ "-->"
+ rSet.getObject("custNum")
);
}
out.println( "Number of records from Customer table:" + ii );
}
public void release() throws Open4GLException, SQLException
{
rSet.close();
appObj._release();
}
SDOAppObject appObj;
SDOResultSet rSet;
String procName;
PrintWriter out;
}
// Sample1.java end.