Solved

Writing CGI in Java

Posted on 1999-01-05
2
227 Views
Last Modified: 2013-12-25
I decided to try to write a Java CGI program as an experiment to see how well it ports from UNIX to Win9*
(yes, I know it's slow as a dog and wouldn't work well
in practice...)

The Java source code is:
import java.lang.*;

public class JavaHello {
  public static void main(String argv[]) {
    char space = ' ', newline = '\n', plus = '+';
    String request = System.getProperty("JavaHello.request");
    String clength = System.getProperty("JavaHello.clength");
    String query_s = System.getProperty("JavaHello.query");
    StringBuffer query = new StringBuffer(query_s);
    for (int i=0;i<query.length();i++) {
      if (query.charAt(i) == plus) {
        query.setCharAt(i,space);;
      }
    }
    query_s = query.toString();
    System.out.println("Content-type: text/plain");
    System.out.println();
    System.out.println("Hello World!");
    System.out.println("Request type is " + request);
    System.out.println("Query String is " + query_s);
    System.out.println("Content-Legnth: " + clength);
    System.out.flush();
  }
}

This is called by a shell script javahello.cgi in UNIX:
#!/bin/sh
JAVA_HOME=/usr/local/jdk1.1.3
PATH=/usr/local/jdk1.1.3/bin:/usr/local/jdk1.1.3/bin:/usr/local/bin:/bin:/usr/bin:.:/usr/X11R6/bin:/usr/bin/mh:/usr/X11R6/bin:/usr/bin/mh
cd /home/httpd/html/JAVAtest/joetest
-DJavaHello.query=$QUERY_STRING -DJavaHello.clength=$CONTENT_LENGTH JavaHello
exec java -DJavaHello.request=$REQUEST_METHOD -DJavaHello.query=$QUERY_STRING -DJavaHello.clength=$CONTENT_LENGTH JavaHello
#java JavaHello

My question is, how would I get this to run on a Windows
server?

0
Comment
Question by:petrow
  • 2
2 Comments
 
LVL 7

Accepted Solution

by:
yoren earned 100 total points
ID: 1830488
Basically, it's the same, except environment variables are accessed a bit differently.

1. Set up Java. If you're running Windows 95 or 98, define CLASSPATH and PATH in Autoexec.bat. If you're running NT, define them in Control Panel - System - Environment.

2. Do nothing to your Java program.

3.Convert your shell script to a .BAT file, as follows (all on 1 line):

Yuval

java -classpath "%CLASSPATH%;c:/my/program/is/here"
-DJavaHello.query=%QUERY_STRING% -DJavaHello.clength=%CONTENT_LENGTH% -DJavaHello.request=%REQUEST_METHOD%  -DJavaHello.query=%QUERY_STRING%
 -DJavaHello.clength=%CONTENT_LENGTH% JavaHello

0
 
LVL 7

Expert Comment

by:yoren
ID: 1830489
Somehow my answer got a bit jumbled -- your batch file will work better if you don't include my name in it. Instead, include the stuff below it.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need help to rewrite script 3 80
importing users to Security group 2 51
Element alignment and word wrapping 9 69
ICACL switch help. How to include current folder 2 44
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question