3
votes

I trying to figure out how to write CGI scripts in Java.

I followed this examples -> http://www.javaworld.com/jw-01-1997/jw-01-cgiscripts.html?page=1

It provide cgi_lib.java, hello.html and hello.java

Everything seems fine, but in the html part. The action is pointed to cgi_lib/hello.cgi

There's no cgi provided. So I tried with cgi_lib/hello.java, and it print the entire source code in the hello.java.

Then i tried to edit the hello.java extensions into hello.cgi, and tried again. The browser returns me error 500.

What is the problem? Is it that, there's some specific method to compile the hello.java into hello.cgi? The script in hello.cgi is different from hello.java?

Please help.

Thank you.

UPDATE I added hello.cgi

#!/bin/sh
java     -Dcgi.content_type=$CONTENT_TYPE     -Dcgi.content_length=$CONTENT_LENGTH     -   Dcgi.request_method=$REQUEST_METHOD     -Dcgi.query_string=$QUERY_STRING     -Dcgi.server_name=$SERVER_NAME     -Dcgi.server_port=$SERVER_PORT     -Dcgi.script_name=$SCRIPT_NAME     -Dcgi.path_info=$PATH_INFO   hello

So is the $CONTENT_TYPE, $CONTENT_LENGTH,... remain the same? Or should I enter something?

Just to make things clearer. I put the the cgi_lib, hello.java and hello.cgi in the C:\xampp\cgi-lib

And the hello.html in C:\xampp\htdocs\test

When I tried to connect it returns me this

Server error!

The server encountered an internal error and was unable to complete your request.

Error message: 
couldn't create child process: 720002: hello.cgi

If you think this is a server error, please contact the webmaster.

Error 500

localhost
Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.7

I had check the httpd.conf in c:\xampp\apache\conf and configure according to this

LoadModule cgi_module modules/mod_cgi.so ScriptAlias /cgi-bin/ "C:/Program Files/Apache Group/Apache/cgi-bin/" Seems to be already enable.

So whats the problem right now?

3
Are you sure that you indeed want to write CGI script in java rather than to use JSP? - AlexR
For learning purpose. Actually I wanna use fastcgi, but I don't know where to start. I want to use for PHP <-> Java. But couldn't find any site that teach to install/configure fastcgi in xampp, java, php. Maybe I'm to stupid to understand. And I can't really find any sites teach how the scripts looks like in PHP and Java for them to communicate. Thats why I will try with CGI first. As for JSP, I'm learning it right now. Thanks - Ninjoe Quah

3 Answers

2
votes

The contents of hello.cgi is shown at the top of page 2 of the article.

#!/bin/sh
java     -Dcgi.content_type=$CONTENT_TYPE     -Dcgi.content_length=$CONTENT_LENGTH     -Dcgi.request_method=$REQUEST_METHOD     -Dcgi.query_string=$QUERY_STRING     -Dcgi.server_name=$SERVER_NAME     -Dcgi.server_port=$SERVER_PORT     -Dcgi.script_name=$SCRIPT_NAME     -Dcgi.path_info=$PATH_INFO   hello
0
votes

The article says that you will execute the java jar from within a cgi script. You need to make sure that this script is set up to execute your jar file by making the correct calls to the java executable with your hello.jar file as the parameter. Also make sure your web server is configured correctly to allow the execution of cgi scripts.

0
votes

You also have to chmod of your cgi and java files so others can execute them.