vajra11
Posts: 41
Joined: Thu Jul 05, 2018 4:44 am

Hello world server program in java

Mon Jul 09, 2018 10:10 am

I am trying print Hello world! in java through pressing button on web page. whenever I will press the button Hello world should be print on browser.

I have tested apache server with html code it's running on raspberry pi 3. I also can run the java program on pi but I don't understand the server program

How to print Hello world! in java through pressing button on web page

html code

Code: Select all

<!DOCTYPE html>
<html>
<body>

<h2>Button</h2>
<form action="button">
    <button type="submit">Press Button!</button>
<form>
 
</body>
</html>
java program

Code: Select all

class hello {
    public static void main(String[] args) {
        System.out.println("Hello, World!"); 
    }
}

scotty101
Posts: 3027
Joined: Fri Jun 08, 2012 6:03 pm

Re: Hello world server program in java

Mon Jul 09, 2018 10:15 am

Given up on python?
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

User avatar
topguy
Posts: 4738
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Hello world server program in java

Mon Jul 09, 2018 10:50 am

Why Java ? Some special goal here or just trying to learn ?

Using Java as server-side language is usually something for quite large systems, and then using what is called a application-server like Tomcat ( https://tomcat.apache.org/download-80.cgi ). I dont see Tomcat mentioned much on this forum so I'm not sure its event runnable on RPi.

A smaller more suitable system is probably Jetty. https://www.eclipse.org/jetty/
And while this might be possible by following some guide: https://bastide.org/2015/04/05/raspberr ... with-java/
... I'm still going to ask "Why Java ?".

vajra11
Posts: 41
Joined: Thu Jul 05, 2018 4:44 am

Re: Hello world server program in java

Mon Jul 09, 2018 1:15 pm

scotty101 wrote:
Mon Jul 09, 2018 10:15 am
Given up on python?
In that example we were using Flask server with python but I want to use Apache server that's reason I am looking for example.

I am planning to use apache server for both python and java even for c++ language. I just want to do practice with one server, why i need different server. I have seen apache server is suitable for python java c++

Note : I am trying to setup programming platform on raspberry Pi so that I can run my program on Raspberry Pi. I started with hello program. My ultimate goal is to learn GPIO programming for raspberry Pi
Last edited by vajra11 on Mon Jul 09, 2018 1:27 pm, edited 1 time in total.

vajra11
Posts: 41
Joined: Thu Jul 05, 2018 4:44 am

Re: Hello world server program in java

Mon Jul 09, 2018 1:23 pm

topguy wrote:
Mon Jul 09, 2018 10:50 am
Why Java ? Some special goal here or just trying to learn ?

Using Java as server-side language is usually something for quite large systems, and then using what is called a application-server like Tomcat ( https://tomcat.apache.org/download-80.cgi ). I dont see Tomcat mentioned much on this forum so I'm not sure its event runnable on RPi.

A smaller more suitable system is probably Jetty. https://www.eclipse.org/jetty/
And while this might be possible by following some guide: https://bastide.org/2015/04/05/raspberr ... with-java/
... I'm still going to ask "Why Java ?".
Most of person suggest both java and python programming language for raspberry. I think learning java programming will not waste of time.

In most of job requirement they ask for knowledge of java programming . if I have a knowledge of java programming for raspberry. it's plus point for me.

User avatar
topguy
Posts: 4738
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Hello world server program in java

Mon Jul 09, 2018 9:30 pm

vajra11 wrote:
Mon Jul 09, 2018 1:23 pm
I think learning java programming will not waste of time.
It was not my intention to be negative on Java as a programming language, just that using it as a server side language in a web-application is not necessarily for beginners. And you might want to read a bit more on Tomcat and Jetty which are webservers made specifically for Java.

In general learning to program something for the web can be complex because you not only has to learn the language to implement functionality in the server, but you also usually has to learn HTML and most likely some Javascript to run in the browser.

If you want to learn web-programming, maybe take a look at PHP which is very easy to enable support for in Apache.
Or just create Java programs that doesnt need to run on a web-server.

ejolson
Posts: 1832
Joined: Tue Mar 18, 2014 11:47 am

Re: Hello world server program in java

Tue Jul 10, 2018 12:20 am

vajra11 wrote:
Mon Jul 09, 2018 1:23 pm
In most of job requirement they ask for knowledge of java programming. if I have a knowledge of java programming for raspberry. it's plus point for me.
This is a very practical approach. While I find Java of little interest personally, a friend of mine uses it almost exclusively. A good working knowledge is definitely a requirement for many jobs and application areas including server side web development.

vajra11
Posts: 41
Joined: Thu Jul 05, 2018 4:44 am

Re: Hello world server program in java

Tue Jul 10, 2018 12:49 am

topguy wrote:
Mon Jul 09, 2018 9:30 pm
just create Java programs that doesnt need to run on a web-server.
I have done it before. I can run java program on raspberry. Now I just want to run one java server program on web-server
topguy wrote:
Mon Jul 09, 2018 9:30 pm
you might want to read a bit more on Tomcat and Jetty which are webservers made specifically for Java.

In general learning to program something for the web can be complex because you not only has to learn the language to implement functionality in the server, but you also usually has to learn HTML and most likely some Javascript to run in the browser.
How does most of people write java server programs on Raspberry Pi.?

Do they write java program on their Laptop/PC and later they deploy program on Raspberry ?

Do they install apache server on raspberry pi and write java server program on Raspberry Pi

I want to follow the method which the most of peoples follow

Heater
Posts: 9696
Joined: Tue Jul 17, 2012 3:02 pm

Re: Hello world server program in java

Wed Jul 11, 2018 8:13 am

vajra11,

I sense a great deal of confusion here.

Confusion about how web servers and web browsers work. Confusion about HTML, buttons, etc..

Never mind the apparent confusion over programming in Python or Java or whatever.

Anyway, what you asked for was to be able to display a text message when a button is pressed on a web page in a browser. I will try to give some clues as to how this all works and present the simplest working example I can think of for you. I will assume that the text message should come from the web server when the button is pressed, not just something that is done in the web page on the browser:

No matter what language you want to use, Java, Python, PHP, etc, at your server end the first thing you need is some HTML for the browser to display as a web page. That HTML needs to contain a <button> element and an element into which your text will go. It could look like this:

Code: Select all

<html>
  <head>
    <title>Button Example</title>
  </head>
  <body>

    <h1>Button example</h1>
    <br>

    <button onclick="clickHandler()">Click me</button>
    <br>

    <p id="buttonId">No text fetched yet, press the button!</p>

    <script src="index.js"></script>

  </body>
</HTML>
Note that this HTML button has an "onClick" attribute set which is Javascript function to be run when the button is pressed "clickHandler()".

It has a paragraph tag <p> where text will be displayed when the button is pressed. That element has an id attribute such that the onclick handler code can refer to it for updating.

It also has a <script> tag which references some Javascript file that the browser should load from the server. That Javascript will contain the clickHandler() function we need.

I suggest you create a "myButtonProject" directory and put that HTML in there as the file "index.html".

The Javascript file looks like this:

Code: Select all

// This function is called on button click
function clickHandler() {
  // Show that we are fetch data.
  let element = document.getElementById("buttonId").innerHTML = "Fetching data...."

   // Fetch some text to display from the server
   fetch('button.json')
     .then(function(response) {
     //console.log(response.json)
     return response.json();
     })
     .then(function(data) {
       document.getElementById("buttonId").innerHTML = data.text
   });
}
Basically it sets the content of the HTML element that will display your text. Then it fetches some data, in JSON format, from the server at /button.json. Then it updates the text element with new text that the server responds with.

I suggest you put this Javascript file into myButtonProject/index.js

So far so good. But what we need is a web server to server up that index.html, then the index.js that the index.html references, then the data that is requested when the button is pressed. As you see there are quite a lot of moving parts to this simple button already.

Now no doubt this can be done in Java or Python or whatever language with Apache or whatever web server. However the simplest thing to do is create a little webserver in Javascript using node.js. Such a web server could look like this:

Code: Select all

// Pull in the express HTTP server module and create our application server
const express = require('express')
const app = express()

// Pull in the required logging modules
const expressLogging = require('express-logging')
const logger = require('logops')

// Here we count the number of times our button is pressed
let buttonRequestCount = 0

// Log all requests to the server so we can see what is going on as it runs
app.use(expressLogging(logger));

// Tell express to serve files from some directory
app.use(express.static('./'))

// Serve our button message data whenever /button.json is requested with an HTTP GET request
app.get('/button.json', (req, res) => {
  // Count the number of times the button is pressed
  buttonRequestCount += 1

  // This object contains the text sent to the browser when data.json is requested
  const data = {text: `The button has been pressed ${buttonRequestCount} times`} 
  // Send the text message as an HTTP response
  res.send(data)}
) 

// Start the server listening on some port number
app.listen(3000, () => console.log('Example app listening on port 3000!')) 
This should be almost self explanatory given the comments I have put in there.

The important part is the "app.get('/button.json'...". This is the code that is run when your button in the web page is pressed and the onClick handler makes an HTTP get request for data using that "fetch" function.

There is no actual file called "button.json" to serve at that URL but the server generates it on the fly as a JSON object containing the required text.

I suggest you put that Javascript server code into a file called myButtonProject/index.js

How can you run this thing? Good question.

1) Install node.js

Code: Select all

$ apt-get install node.js
2) Change to the myButtonProject directory and install the required node.js modules:

Code: Select all

$ cd myButtonProject
$ npm init
$ npm install express
$ npm install express-logging
$ npm install logops
the npm init command will ask a bunch of questions, just hit return til it's done.

3) Run the server:

Code: Select all

$ node server.js
4) View in the browser:

Point your browser at http://localhost:3000

It should display your button.

Click the button, it should show a new message each time.

I have attached all the files above as a tar archive.

Now, I can understand that you may not want to use node.js as the server if you have your heart set on Java or Python or whatever but you will need to know about HTML and HTTP GET requests etc, as shown here. You will most likely need to know some Javascript to put in the web page as shown here. The server end could be done with Apache and PHP or Python or Java or many other ways. Personally I find they make life more complicated than it need be.
Attachments
myButtonProject.tgz
(5.4 KiB) Downloaded 22 times

Balin
Posts: 3
Joined: Mon Sep 17, 2018 7:43 am

Re: Hello world server program in java

Tue Sep 18, 2018 12:36 pm

vajra11,

with a mere Apache, some HTML and a simple Java program, this is - well, unusual. Sure, you could use a Java program as a CGI, like mentioned here: https://bit.ly/2ODhSz2. But usually, you use some JSP server to deal with the java part of a web application, like Tomcat, Jetty, Websphere or any other. You can even combine Apache HTTP server and Tomcat, to let the HTTP server serve the static parts and let Tomcat with Java do the dynamic parts. Or you can set up an HTML/Javascript page and let the Javascript communcate with some Java server program. Another possibility would be a Java web framwork, like Vaadin or JWT., where you can do everything in pure Java, without a single bit of HTML. You also can use a fully fledged JEE application server, like Glassfish or Wildfly, and do big, fat JEE applications, but I am afraid, that a Pi is not really suitable for this kind of applications.

I personally use most often Vaadin to create web applications, and a Websphere Liberty Core server (which has an amazing small memory footprint) as server, letting HTML and Javascript completely aside.
Regards, Balin.

Balin
Posts: 3
Joined: Mon Sep 17, 2018 7:43 am

Re: Hello world server program in java

Wed Sep 19, 2018 6:11 am

topguy wrote:
Mon Jul 09, 2018 10:50 am
Using Java as server-side language is usually something for quite large systems, and then using what is called a application-server like Tomcat ( https://tomcat.apache.org/download-80.cgi ). I dont see Tomcat mentioned much on this forum so I'm not sure its event runnable on RPi.
Maybe the reason that tomcat is rarely mentioned on this forum is, that it runs without issues. At least that is my experience, although I prefer Websphere Liberty Core due to it's smaller memory footprint, which is freely available as long as it doesn't use more than 2GB, which is quite unlikely on any Raspberry Pi.

Doing Java has the big advantage of a very comprehensive development environment. For nearly every aspect, there is a library or a framework, be it GUI, database access, communication, whatever you want. Development tools are very classy. And you can do the development on nearly any machine you like, because it is (nearly) system- and architecture-independent. As long as there exists a JVM, the program is able to run, regardless, where you developed it. Additionally, it is strongly typed and rather object oriented, which simplifies development and prevents from errors, and even if a runtime error occurs, there is an excellent exception management.

There are several JVMs available now for the Raspberry Pi, OpenJDK ist packed with Raspbian (and also with FreeBSD for Pi, that is), and they all run quite well. I personally prefer the Azul OpenJDK version, it has a rather good JIT and no license costs, regardless what you do with it.
Regards, Balin.

Heater
Posts: 9696
Joined: Tue Jul 17, 2012 3:02 pm

Re: Hello world server program in java

Wed Sep 19, 2018 9:15 am

Above I presented a solution to our OP's question in Javascript.

Could you post the equivalent in Java?

It would answer the OP's question and I would be interest in seeing how they compare.

Return to “Java”

Who is online

Users browsing this forum: No registered users and 5 guests