JHP: J Hypertext Processor

JHP is a J based engine to run server-side web applications and create dynamic web content. Syntax is similar to PHP or ASP. JHP can run on any platform and web server.

Browse JHP at olegykj.sourceforge.net to see it in action live on that server. The above link also contains documentation. (Note: those pages require that javascript be enabled in the browser.)

See also: examples in examples folder in SVN; change history.

Examples

The following source code is an example of JHP.

<% ContentType'text/html' %>
<html><head>
  <link rel="stylesheet" type="text/css" href="style.css">
</head><body>

<h2>Random Test</h2>

<p>Time is <%= 6!:0 '' %></p>

<% 9!:1 >.{:6!:0 '' %>

<p>Random array with seed <%= 9!:0'' %></p>

<pre><%= ":3 4 ?@$ 100 %></pre>

</body></html>

Syntax

JHP syntax is similar to ASP or PHP. Plain HTML is entered as is. J code is introduced by matching pairs of percent brackets <% ... %>. As a result, plain text is sent to the server as is and the J code is executed.

Any output in a particular piece of code is sent in between the surrounding plain HTML. Two verbs are used for output: print and println, the latter adding a LF to the end. These verbs format arguments as string.

<%= ... %> is a short cut for <%print ... %>. The content between such brackes cannot contain new lines, whereas the regular brackets permit them, as in J script.

It is required that the first line of the page specifies the MIME format, such as

<% ContentType'text/html' %>
<html><head>
  ...

It is possible to produce any format output, such as XML, CSV, images, audio, etc. However, you must pay attention to observe spaces or absence thereof between the percent brackets.

JHP Configuration steps for Microsoft IIS 7.0

Apache for Windows

Testing

To run native Windows scripts (CMD or VBScript etc) the #! pattern is not necessary.

Here's an example of CMD script which sets PATH_TRANSLATED. Place it to cgi-bin and verify that it works.

«test.cmd»=
@echo off

echo Content-type: text/plain
echo.

set JHP_EG=d:\Math\j602\addons\web\jhp\examples
set PATH_TRANSLATED=%JHP_EG%%SCRIPT_NAME:/=\%

echo Hello from CMD
echo.

set

It is possible to install Perl and use it as a shell script. However the #! needs to be adapted of ExecCGI registry set.

«jhp_info.pl»=
#!perl
##
##  jhp -- run J from Perl CGI
##

print "Content-type: text/plain; charset=iso-8859-1\n\n";

$JHP_EG = 'd:\Math\j602\addons\web\jhp\examples'

$PATH_TRANSLATED = $ENV{SCRIPT_NAME};
$PATH_TRANSLATED =~ s|/|\\|g;

$ENV{PATH_TRANSLATED} = $JHP_EG . $PATH_TRANSLATED;

foreach $var (sort(keys(%ENV))) {
    $val = $ENV{$var};
    $val =~ s|\n|\\n|g;
    $val =~ s|"|\\"|g;
    print "${var}=\"${val}\"\n";
}

Place this test.ijs into D:\Tools\Apache22\cgi-bin\. J is installed on the same drive into d:\Math\j602 (note first #! line and use of -jprofile to turn off profile).

«test.ijs»=
#!/Math/j602/bin/jconsole -jprofile 

stdout_z_=: 1!:2&4
stderr_z_=: 1!:2&5
exit=: 2!:55
LF_z_=: 10{a.
padj=: }:@,@:(,&LF"1)^:(1 < #@$)@":
print_z_=: 0 0&$@stdout@padj NB. smoutput NB. 0 0&$@(1!:2&4)
println_z_=: [: print LF [ print

println 'Content-type: text/plain',LF

println 'Hello from J!',LF

println '   6!:0 ''''  NB.  time '
println 6!:0''

9!:1 ] 1e6 | 6!:9 ''    NB. random seed
println '   ]A=: 3 4 ?@:$ 10   NB. random matrix'
println A=: 3 4 ?@:$ 10

println '   +/A                NB. sum columns'
println +/A

exit ''

Setup JHP Examples ...

See Also

JHP (last edited 2009-10-19 22:24:39 by OlegKobchenko)