Hello world/Web server: Difference between revisions

m
-Category:Scala Implementations / moved Category to top
(→‎{{header|Common Lisp}}: proper indentation for serve function)
m (-Category:Scala Implementations / moved Category to top)
Line 1:
{{task|Networking and Web Interaction}} [[Category:Web]]
{{omit from|GUISS}}
The browser is the new [[GUI]]!
{{omit from|Locomotive Basic|No sockets}}
{{omit from|Lotus 123 Macro Scripting}}
{{omit from|Maxima}}
{{omit from|ML/I|No sockets}}
{{omit from|Retro}}
{{omit from|TI-83 BASIC}}
{{omit from|ZX Spectrum Basic|No sockets}}
 
The browser is the new [[GUI]] !
 
The task is to serve our standard text "Goodbye, World!" to http://localhost:8080/ so that it can be viewed with a web browser. The provided solution must start or implement a server that accepts multiple client connections and serves text as requested.<br>
The provided solution must start or implement a server that accepts multiple client connections and serves text as requested.
 
Note that starting a web browser or opening a new window with this URL is not part of the task. Additionally, it is permissible to serve the provided page as a plain text file (there is no requirement to serve properly formatted [[HTML]] here). The browser will generally do the right thing with simple text like this.
is not part of the task. <br>
Additionally, it is permissible to serve the provided page as a plain text file (there is no requirement to serve properly formatted [[HTML]] here).
The browser will generally do the right thing with simple text like this.
 
=={{header|Ada}}==
Line 32 ⟶ 45:
 
=={{header|AWK}}==
With GNU AWK (gawk) a simple web server can be implemented. The example is taken from here<br>
The example is taken from
[http://www.gnu.org/software/gawk/manual/gawkinet/gawkinet.html#Primitive-Service]
(Documentation is licensed under GNU Free Documentation License, Version 1.3)
Line 284 ⟶ 298:
 
=={{header|D}}==
Using sockets only, also shows use of heredoc syntax, std.array.replace, and casting to bool to satisfy the while conditional.
and casting to bool to satisfy the while conditional.
 
<lang D>
Line 323 ⟶ 338:
..response.close()));
}</lang>
 
=={{header|Delphi}}==
<lang Delphi>program HelloWorldWebServer;
Line 416 ⟶ 432:
 
=={{header|Erlang}}==
Using builtin HTTP server with call back to do/1. It only lasts 30 seconds (30000 milliseconds), then it is stopped. I fail to see how a longer time will serve any purpose.
It only lasts 30 seconds (30000 milliseconds), then it is stopped.
I fail to see how a longer time will serve any purpose.
 
<lang Erlang>
Line 503 ⟶ 521:
=={{header|Haskell}}==
 
Lightweightly concurrent "hello world" web server using the [http://www.yesodweb.com/book/conduits conduit] stack:
using the [http://www.yesodweb.com/book/conduits conduit] stack:
 
<lang haskell>{-# LANGUAGE OverloadedStrings #-}
Line 575 ⟶ 594:
 
=={{header|J}}==
If the desire is to use the browser as a gui, the easiest thing to do would be to [http://www.jsoftware.com/stable.htm download] [http://www.jsoftware.com/docs/help701/user/relhigh.htm j7], edit the jhs script to start on port 8080, start jhs, visit http://127.0.0.1:8080/jijx then enter the text:
would be to [http://www.jsoftware.com/stable.htm download] [http://www.jsoftware.com/docs/help701/user/relhigh.htm j7], edit the jhs script to start on port 8080,
start jhs, visit http://127.0.0.1:8080/jijx then enter the text:
<lang j>'Goodbye, World!'</lang>
This will compute the desired result and display it (actually, it will be displayed twice since the original string will also be displayed). This would be even simpler if you could just use the default jhs port (65001)... Alternatively, a jhs form could be used (but this would not have the exact url structure specified).
This would be even simpler if you could just use the default jhs port (65001)...
Alternatively, a jhs form could be used (but this would not have the exact url structure specified).
 
However, if the desire is to implement the task exactly, any of approaches at [[j:JWebServer]] might be used.
any of approaches at [[j:JWebServer]] might be used.
 
For example, here is a web server which ignores the client's request and always returns Goodbye, World:
and always returns Goodbye, World:
<lang j>hello=: verb define
8080 hello y NB. try to use port 8080 by default
Line 611 ⟶ 636:
To deploy this server, once it has been defined, run
<lang j>hello''</lang>
This version works because reasonable http requests fit in a single tcp packet.
This version works because reasonable http requests fit in a single tcp packet. (And note that the server waits for one tcp packet before responding.) If parsing of the request is desired, one of the more complicated implementations at [[j:JWebServer]] should be used instead (but that's not really relevant for this task, except perhaps to require complete headers before responding, with broken browsers which send multiple tcp packets for the request).
(And note that the server waits for one tcp packet before responding.)
This version works because reasonable http requests fit in a single tcp packet. (And note that the server waits for one tcp packet before responding.) If parsing of the request is desired, one of the more complicated implementations at [[j:JWebServer]] should be used instead (but that's not really relevant for this task, except perhaps to require complete headers before responding, with broken browsers which send multiple tcp packets for the request).
 
=={{header|Java}}==
Multiple requests will be served in the order that they reach the server, with a queue size limit of 50 waiting requests imposed by default in the <code>ServerSocket</code> class (may be changed by adding a second positive integer argument to the <code>ServerSocket</code> constructor).
with a queue size limit of 50 waiting requests imposed by default in the <code>ServerSocket</code> class (may be changed by adding a second positive integer argument to the <code>ServerSocket</code> constructor).
<lang java>import java.io.IOException;
import java.io.PrintWriter;
Line 657 ⟶ 685:
 
=={{header|Lasso}}==
While Lasso has a built-in webserver you can use, here's how you can create a basic multi-threaded webserver of your own to complete this request:
here's how you can create a basic multi-threaded webserver
of your own to complete this request:
<lang lasso>local(server) = net_tcp
handle => { #server->close }
Line 680 ⟶ 710:
 
=={{header|Liberty BASIC}}==
This is difficult, although possible, in Liberty BASIC, but it's close relative Run BASIC is designed for serving webpages easily. The task becomes simply ..
but it's close relative ''Run BASIC'' is designed for serving webpages easily.
The task becomes simply ..
<lang lb>print "hello world!" </lang>
 
Line 863 ⟶ 895:
close CLIENT;
}</lang>
Various modules exist for using sockets, including the popular IO::Socket which provides a simpler and more friendly OO interface for the socket layer. Here is the solution using this module:
which provides a simpler and more friendly OO interface for the socket layer.
Here is the solution using this module:
{{libheader|IO::Socket::INET}}
<lang Perl>use IO::Socket::INET;
Line 877 ⟶ 911:
close $client;
}</lang>
 
Using Perl's glue power, provide a suicide note with visitor counter via netcat:
with visitor counter via netcat:
<lang Perl>while (++(our $vn)) {
open NC, "|-", qw(nc -l -p 8080 -q 1);
Line 884 ⟶ 920:
"Goodbye, World! (hello, visitor No. $vn!)\xd\xa";
}</lang>
 
Here's another solution using Plack (may be found on CPAN):
<lang Perl>use Plack::Runner;
Line 1,055 ⟶ 1,092:
=={{header|Run BASIC}}==
<lang runbasic>html "Hello World!"</lang>
 
=={{header|Rust}}==
This solution works for Rust 0.11-pre.
Line 1,087 ⟶ 1,125:
 
=={{header|Scala}}==
[[Category:Scala Implementations]]
{{libheader|Scala}}
{{Trans|Java}}It shows that Scala can simply embed XML fragments.
Line 1,164 ⟶ 1,201:
service registerServiceOn: myServer.
myServer start.</lang>
Be aware that the above is an ad-hoc minimal scripting example. Normally, a service subclass is used and response handlers are defined as methods of it (not as action blocks).
Normally, a service subclass is used and
Also, services and HTML generation is usually done using a framework (at least DOM-based, but usually a higher level toolkit).
response handlers are defined as methods of it (not as action blocks).
Also, services and HTML generation is usually done using a framework (at least DOM-based, but usually a higher level toolkit).
(at least DOM-based, but usually a higher level toolkit).
Especially take a look at smalltalk frameworks like Aida, Seaside, VisualWave etc.
 
===Pharo Smalltalk===
Pharo ships with the Zinc HTTP Component frameworks that includes a ZnServer class. Here's the simplest solution to start a web server:
that includes a ZnServer class.
Here's the simplest solution to start a web server:
<lang smalltalk>
(ZnServer startDefaultOn: 1701)
Line 1,191 ⟶ 1,234:
socket -server accept 8080
vwait forever</lang>
 
===Jim Tcl===
Jim is a small footprint reimplementation of Tcl with modern features.
Line 1,265 ⟶ 1,309:
serverSocket.hostname, ":", serverSocket.port);
serverSocket.listen(jobPipe);</lang>
 
 
{{omit from|GUISS}}
{{omit from|Locomotive Basic|No sockets}}
{{omit from|Lotus 123 Macro Scripting}}
{{omit from|Maxima}}
{{omit from|ML/I|No sockets}}
{{omit from|Retro}}
{{omit from|TI-83 BASIC}}
{{omit from|ZX Spectrum Basic|No sockets}}
Anonymous user