1

I have a Java application which expects to be sent either an integer and a String in order to carry out operations. This program is already written and functional. However, I wish to use a webpage to control it. Therefore, I am hoping to have a webpage which can send this data as controlled by the user. However, I do not know how to go about doing this.

What is the simplest way of sending messages from HTML/Javascript to a Java program?

I have access to Java SE only.

4 Answers 4

1

It depends on protocol (adapter) your application is using. It can be a HTTP, websockets or something else.

I can assume that you are talking about HTTP, so the easiest way is to use form html element and POST method.

check this

Sign up to request clarification or add additional context in comments.

1 Comment

My application currently has nothing implemented to receive messages. This will be added in once I know how communication will be occurring. I was originally thinking of using POST as this allows me to send multiple pieces of data at once which is what I require. However I have no idea how to setup Java to receive POST messages. Everything I look at looks really complex. Even the link you posted seems to make it look difficult to do using Java.
1

Please slice your project into two(three, if there is a database management) parts:

  1. Server side - core side. Here you are able to implement your java code. That means you must have running a server like tomcat. Create new web app project. Include tomcat or something else as server. Create a Servlet (do not ask how to create but search through internet). In that servlet override doPost method. Including this code inthat method gives you an assumption about server side:

response.getWriter().write("hello from server");

  1. Client side. Client side is where you implement html, js and css code. Submit form to that servlet by post method.

If you have correctly configured web.xml then on submitting this form you should receive "hello from server" as a reponse. Use eclipse as IDE that is easier to configure.

Comments

0

You can use JSP / Servlet to communicate between client and server.

See here for some basic stuff

4 Comments

That is Java EE, right? I forgot to mention I have access to Java SE only.
May i know why you are limited to J2SE?
The system that will eventually be using the program only has Java SE installed and I have no administrator rights to install EE on it instead.
try to read about spring boot application. It will just require you to add some libraries or jars to your application.
0

Question: Do you have maven? If not I would encourage you to download it. It's simple: https://maven.apache.org/

If yes, add these to your pom file:

    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.0.RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

Now in the class which has your main application add some annotations and a controller (method) to handle your POST request:

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Controller
@EnableAutoConfiguration
public class SpringBootSampleApplication {

    @RequestMapping("/")
    @ResponseBody
    @CrossOrigin
    String home() {
        return "Hello World!";
    }

    /** Receives a POST request on path / with parameter name. Will return parameter
     * 
     * @param request
     * @return */
    @RequestMapping(value = "/", method = RequestMethod.POST)
    @ResponseBody
    @CrossOrigin // this will allow requests origination from any domain. see CORS
    String greet(HttpServletRequest request) {
        return "Greetings: " + request.getParameter("name");
    }

    /** starts the application
     * 
     * @param args
     * @throws Exception */
    public static void main(String[] args) throws Exception {
        SpringApplication.run(SpringBootSampleApplication.class, args);
    }
}

This will start a web server on port 8080.

Check out this code sample here: https://github.com/mikibrv/spring.boot.sample

I have also added a small index.html file in the resources as an example of AJAX.

It's a full project, generating 2 endpoints ( a GET and POST ) with CORS enabled and when you build it with Maven (mvn clean install) you get a fat JAR which you can call using : java -jar target/spring.boot.sample.jar

For more info: http://projects.spring.io/spring-boot/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.