HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Writing CGI in Java via JNI and Invocation API"
Vol. 2, Issue 11, p. 68

	

Listing 1. 

C++ code to create JVM 
JNI_GetDefaultJavaVMInitArgs(&vm_args); 
/* vmArgs is a JDK1_1InitArgs structure. 
please put your JDK1.1.x 's class library path here 
*/ 
vm_args.classpath = ".;c:\\cafe\\java\\lib\\classes.zip"; 
res = JNI_CreateJavaVM(&jvm,&env,&vm_args); 
if (res < 0) { 
fprintf(stderr, "Can't create Java VM\n"); 
exit(1); 
}   

Listing 2. 

C++ code to create a java.util.Hashtable 
// create a Java hashtable 
cls = env->FindClass("java/util/Hashtable"); 
if (cls == 0) { 
    fprintf(stderr, "Can't find HashTable class\n"); 
    exit(1); 
} 
mid = env->GetMethodID(cls, "<init>", "()V"); 
if (mid == 0) { 
   fprintf(stderr, "Can't find HashTable <init> ()V\n"); 
   exit(1); 
} 
 jobject envTable=0; 
envTable=env->NewObject(cls,mid); 
if (!envTable) { 
  fprintf(stderr, "Can't create hashtable\n"); 
  exit(1); 
}
 
Listing 3. 

C++ code to fill the hashtable with name 
char * envKeys[size]={"REMOTE_HOST", 
"REMOTE_USER","HTTP_USER_AGENT",...} char * envValues[size]; // build the 
hashtable from envValues         for (int i=0;i<size;i++) { 
                envValues[i]=getenv(envKeys[i]); 
                if (envValues[i]) { 
                        // create a java string from envValues[i] and 
                        // store name/value pair in the hashtable 
                        jstring name= env->NewStringUTF(envKeys[i]); 
                        jstring value=env->NewStringUTF(envValues[i]); 
                        env->CallObjectMethod(envTable,mid,name,value); 
                } 
        }         
Listing 4. 
 
C++ code to invoke snoop.main(java.util.Hashtable) 
cls = env->FindClass("Snoop"); 
if (cls == 0) { 
  fprintf(stderr, "Can't find Snoop class\n"); 
  exit(1); 
} 

mid = env->GetStaticMethodID(cls, "main", "(Ljava/util/Hashtable;)V"); 
if (mid == 0) { 
  fprintf(stderr, "Can't find Snoop.main(hashtable)\n"); 
  exit(1); 
} 

// call Java code here 
cls = env->FindClass("Snoop"); 
mid = env->GetStaticMethodID(cls, "main", "(Ljava/util/Hashtable;)V"); 
env->CallStaticVoidMethod(cls, mid,envTable); 

Listing 5. 

C++code to invoke snoop.main(java.util.Hashtable) 

import java.util.Hashtable; 

import HttpRequest; 

class Snoop { 

public static void main(Hashtable envTable) { 

// create a request from envrioment vars 

HttpRequest request=new HttpRequest(envTable); 

System.out.println("Content-type: text/html"); 

System.out.println(); 

System.out.println("<html><HEAD><TITLE> Test </TITLE></HEAD><BODY>"); 

System.out.println("<h1> Form Data</h1> "); 

System.out.println("<OL>"); 

System.out.println("<LI> REQUEST_METHOD: "+request.getMethod()+"</LI>"); 

System.out.println("<LI> CONTENT_LENGTH: 
"+request.getContentLength()+"</LI>"); 

System.out.println("<LI> CONTENT_TYPE: "+request.getContentType()+"</LI>"); 

System.out.println("<LI> REMOTE_USER: "+request.getRemoteUser()+"</LI>"); 

System.out.println("<LI> REMOTE_HOST: "+request.getRemoteHost()+"</LI>"); 

System.out.println("<LI> username 
"+request.getParameter("username")+"</LI>"); 

System.out.println("<LI> password 
"+request.getParameter("password")+"</LI>"); 

System.out.println("</OL>"); 

System.out.println("</BODY></html>"); 

} 
 } 




 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.