JHP

From J Wiki
Jump to navigation Jump to search
User Guide | Installation | Development | Categories | Git | Build Log


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 %>

</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

  • Make sure the required/desired components of Internet Information Services are installed.
  • The currently installed components can be accessed via Start|Control Panel|Programs|Turn Windows features on or off
  • A fairly minimal installation might include the following components:
      • Web Management Tools
  • IIS Management Console
      • World Wide Web Services
  • Application Development Features
* CGI
  • Common Http Features
* Default Document
* HTTP Errors
* Static Content
  • Health and Diagnostics
* HTTP Logging
* Request Monitor
  • Performance Features
* Static Content Compression
  • Security
* Request Filtering
  • Create the web site
      • Start the IIS Management Console
  • Start|Control Panel|System and Maintenance|Administrative Tools|Internet Information Services (IIS) Manager
  • Right click on the Default Web Site and Add Virtual Directory
  • Alias: jhpex
  • Physical Path: d:\j60x\addons\web\jhp\examples
  • Click OK
  • Create a mapping between .jhp files and jconsole.exe
      • Click on virtual folder "jhpex" in leftmost pane.
      • Choose Handler mappings|Add Script Map...
      • Request path: *.jhp
      • Executable: "d:\j601\jconsole.exe" web/jhp/run %s %s

OR "d:\j602\bin\jconsole.exe" web/jhp/run %s %s

      • Name: "J Hypertext processor".
      • Click OK
      • Answer Yes when prompted whether you want to "enable this ISAPI extension?".
      • Click on top level server in leftmost pane
      • Open the ISAPI and CGI Restrictions and check that and entry exists allowing jconsole.exe. Edit and add a description if desired.
  • Permissions
      • The User account used for Anonymous Authentication needs to have Read & Execute access to the folder
      • The same account also needs to have write access to the ~temp\jbreak directory. A better, more secure alternative is to change the setbreak verb in ~system/main/break.ijs as described in the Troubleshooting section of JHP's config.html (~addons\web\jhp\examples\config.html).

Apache for Windows

  • Download the latest Apache (e.g. version 2.2.11 or later)
  • Install, I prefer a space-free folder, such as d:\Tools\Apache22
  • Use the Start menus for access features line configuration, restarting etc.
  • Configure by editing conf\httpd.conf
    • Uncomment LoadModule rewrite_module modules/mod_rewrite.so
    • Change port to Listen 8084 (if you use IIS for port 80)
  • Test Apache
    • Restart
    • Test page [1]
    • Test J CGI

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.

http://localhost:8084/cgi-bin/test.cmd [{{#file: "test.cmd"}} Download script: 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.

http://localhost:8084/cgi-bin/jhp_info.pl [{{#file: "jhp_info.pl"}} Download script: 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).

http://localhost:8084/cgi-bin/test.ijs [{{#file: "test.ijs"}} Download script: 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