“Hello, World” in JHS – an Example
The following is an excerpt (lightly edited) from the J Google Group in which Gosi gives an example of how to assemble a basic web page using JHS.
Topic: JHS the easiest web server
Bjorn Helgason < email@example.com > Jan 31 12:23AM -0800 ^
What do you need to get things going in JHS? Are we overlooking something trivial? We sometimes get blinded by something we think is trivial and forget to tell others about the initial steps needed.
There have been some discussions about how hard it is to create applications in J7. There is probably less to it than many people think. Here, below, I take a simple application and describe how to get it going.
I want this here line to display in a browser:
And it should be sent from the JHS server.
Once you have seen how simple it is, you can take that one and you can take the demos you see provided with JHS and do the same with them and you are well on your way to creating very advanced applications using J and browsers.
And note that even if we say you do need HTML5 in order to run the new J in browsers, it is only partly true. You can make applications and use J in older browsers too but then the applications are of course simpler so we like to say J7 needs HTML5 because we like to have the options HTML5 makes available.
I wanted to find out what is the minimum I need to do to get an html page from the JHS server. I ended up with a script 7 lines long and it should show anyone that JHS applications can be very easy.
What I do first is create a script and in it I place the info from demos.
------------- from description of demos
---------------- script ends
This sounds pretty easy and, by changing this a tiny bit, then this is what I ended up with
--------- my smallest script
coclass'test2' coinsert'jhs' HBS=: 0 : 0 jhh1 'hello world' ) create=: 3 : '''hello''jhr''''' jev_get=: create
------------ script ends
Now there are just a few more things to do. Start the JHS server, which is easy enough, by opening a console and issue the command JHS or click on the JHS start icon.
----------- starting JHS in a console in Windows
---------------------- end console output
Now the JHS server is up and running and the last line I can just copy to a browser. Then I get an ijx window to execute J sentences and what I need to do a load of my script
I then open a tab in the browser and type in
Now I have got a page with a blue J and a hello on the tab and the hello world on the page.
The script creates a locale when loaded and it is easy enough to investigate or change it there
names_test2_'' HBS create jev_get HBS_test2_ jhh1 'hello world' create_test2_ 3 : '''hello''jhr'''''
I tried creating HBS on one line instead of three:
HBS=: 0 : 0 jhh1 'hello world' )
Interestingly enough, it is not the same. Even when I added LF at the end it is not the same. I analyzed it with a.i.HBS_test2_ and it gave different results and one worked in the app and the other did not.
Anyway, without the HBS it still worked but then it did not show any text on the page and just the title on the tab and a blank page.
Seven lines in a script is not very much.
I guess many people are missing the rest of the instructions of how to display the page from the server.
If I now want to add some html text into the page then one way of doing it would be to put the html into a noun and put the name of that noun into the HBS something like this
----------- the smallest script with html addition
coclass'test2' coinsert'jhs' HBS=: 0 : 0 jhh1'hello world' jhbr text ) create=: 3 : '''hello''jhr''''' jev_get=: create text=: 0 : 0 <html> <head> <title>The Magic of J</title> </head> <body> <h2>How to use Browser <font face=monospace>from J</font> ...</h2> <p>Labs<br> Demos<br> Tutorials<br> Forums<br> Games</p> <p>This page is (C) Copyright 2008-2037 Björn Helgason. All rights reserved.</p> <p>Portions of this program use J Utils -- J software.<br/> (C) Copyright ISI : <a target=_blank href="http://jsoftware.com/zippy.htm"> http://jsoftware.com/zippy.htm</a></p> <p>Provided AS IS. Guaranteed for demos.</p> </body></html> )
-------------- script ends
After editing the script I save it and load it in the ijx window. I find it easier to do the editing in the GTK part of J but it works in the JHS part as well.
So, as you see, then it is not that difficult to create JHS apps. I tested this in Chrome and Mozilla
It is easy to take copies of the demos provided with JHS and use them as a starting point for similar experiments and then get a lot of experience using interactions with the browser.
Notice that the html text cannot be entered directly in the HBS. I put it in a noun called text but it could be called anything.
jhbr I used in the HBS is a utility noun surrogate for '
'. Similarly, jhh1 is a utility verb that puts a wrapper around the title
jhh1=: 3 : 0 '<h1>',y,'</h1>' ) jhh1 'hello world'
There are a number of utils to assist in the application you create. Here are two very useful help verbs
When you issue them in the ijx window they give good hints what can be done.
As an example, this is what I got now.
--------- end from the ijx window now
It is better to do it fresh in your own ijx window because this may change.
To create applications in the GTK part of J there are some easy demos available and if anyone finds it hard to find or understand, just ask. At the end, I send here in the script test1 I tried before test2. It is based on demo6 which is doing grid.
I put in some text here and there to see what effect it would have. I think it is important to test a lot of silly things in order to get a feeling for how things work.
I made the small script here above - test2 - as a next step after test1 to see what I needed and what not. Having a baseline is important and I think that anyone should be able to understand and set up test2 and from then on, make their own application.
The small steps needed may seem trivial but if you do not know them it may be a difficult task finding out what to do. We working with high tech have to look at the simple things as well sometimes.
What I found difficult sometimes working with browser is to know when a restart or a refresh is needed and how to do it.
----------------- end test1
Well, if you are still reading. I would like to know if you learned anything from it. Or you can also tell me this was complete waste of time and energy. Anyway, I think that genius lies in simplicity and that is a hallmark of J.
Ok, I am going to set in one of the tutorial scripts from the GTK part of the new J
----------------- script for showing hello world in a form in GTK NB. GTK+2.0 tutorial example 2 NB. NB. note the connect_swapped call to destroy the window is moved NB. into the connect handler. require 'gui/gtk' coclass 'm2' coinsert 'jgtk' hello=: 3 : 0 'widget data'=. y smoutput 'Hello World' 0 ) window_delete=: 3 : 0 'widget event data'=. y smoutput 'delete event occurred' 0 NB. 0=do delete, 1=cancel delete ) Destroy=: 3 : 0 'widget data'=. y smoutput 'destroy event occurred' if. -.IFGTK do. gtk_main_quit '' end. destroy '' 0 ) main=: 3 : 0 if. -.IFGTK do. gtkinit'' end. window=: gtk_window_new GTK_WINDOW_TOPLEVEL consig3 window;'delete-event';'window_delete' consig window;'destroy';'Destroy' gtk_container_set_border_width window, 10 button=. gtk_button_new_with_label <'Hello World' consig button;'clicked';'hello' gtk_container_add window, button gtk_widget_set_size_request button, 200 100 gtk_widget_show button gtk_widget_show window if. -.IFGTK do. gtk_main '' end. 0 ) NB. ========================================================= destroy=: 3 : 0 cbfree '' codestroy '' ) cocurrent 'base' main_m2_''
--------------- end script
It would be interesting to hear if people think the gtk part of J is easier than the jhs part. My own opinion goes back and forth between the two. You may hear me say one is easier than the other and then the next day I may have changed to the other. It is basically based on what I am doing at the time.
Now I actually like to combine them and even call one from the other is also interesting and probably no need to favor either one. Because of historical reasons, I am writing this and I write most of my scripts using GTK even if I then use it in JHS.
. Björn Helgason, Verkfræðingur . Fornustekkum II . 781 Hornafirði, . t-póst: firstname.lastname@example.org . http://groups.google.com/group/J-Programming
. Tæknikunnátta höndlar hið flókna, sköpunargáfa er meistari einfaldleikans góður kennari getur stigið á tær án þess að glansinn fari af skónum
/|_ .---------------------------. ,' .\ / | Með léttri lund verður | ,--' _,' | Dagurinn í dag | / / | Enn betri en gærdagurinn | ( -. | `---------------------------' | ) | (\_ _/) (`-. '--.) (='.'=) ♖♘♗♕♔♙ `. )----' (")_(") ☃☠ `. )----' (")_(")
Bjorn Helgason < email@example.com > Jan 31 12:09AM -0800 ^
I am so new to twitter and have not done anything yet - only logged in for a few minutes so far. If handle means username then I chose flugfiskur. It means Flyfish - I have a company called Fugl&Fiskur which means bird and fish.