0

I want to know how to mix the 2 languages together to make 1. What I mean is:

Assume you have a java program that finds the sum of 2 numbers:

public static void main(String[] args){
  int results = findAvg(10,20);

 public int findAvg(int x, int y){
    return (x+y)/2;
 } 
}

Assume you have another html program below:

<!DOCTYPE html>
<html>
<head>
 <title>Page Title</title>
</head>
<body>

<h1>Average of 2 numbers x and y:</h1>
<p>Ans: results</p> //where x, y and results are from the java program above

My question is: If I want to create a program that runs and displays the result in a browser, how will I go about it. Also, will the file be a .java or .html file. Any help is deeply appreciated

4
  • 1
    Look at JSP. Netbeans with Glassfish is the easiest way to get started. Commented Jul 19, 2015 at 21:40
  • I think the words you are looking for are servlet and JSP ... Commented Jul 19, 2015 at 21:40
  • The sad thing is, that I went to look for some super-easy HelloWorld example to show him ... and bumped into this "simplest Spring tutorial": crunchify.com/… . I am crying inside. Commented Jul 19, 2015 at 21:43
  • So write one if you think it should be easier. Please show us your better framework. Commented Jul 20, 2015 at 0:37

4 Answers 4

1

Browsers know HTML, CSS, and JavaScript.

If you want Java to perform a calculation for you, you'll need an HTTP listener (servlet) to accept an HTTP request, bind the data, perform the calculation, and send it back to the client so it can be displayed as HTML.

There are lots of solutions for sending HTML back to clients. They fall under the category of templating. Java Servlet Pages JSPs, Velocity, Thymeleaf, are all examples.

You can also have your client make an AJAX request to get JSON back and parse that into your HTML document.

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

2 Comments

This might be too detailed an answer.
That might be true if answers are only intended for the OP. I think lots of people read them, so it's aimed at them.
0

If you want code to run in an html page than you want javascript, not java. You can use java to write code that will be run on a server that will output custom html as responses to requests from a web browser. The best place to start learning about that is by learning about servlets.

2 Comments

The OP asked about Java, and programming Java applets for web pages is possible.
Nobody should be writing applets anymore. It's not 1995.
0

To be technically correct, its not mixing java and html, its about taking the control to JAVA layer by passing the parameters and then getting the processed value back in your html layer.

MVC pattern is a simple and elegant way to start with. Look for MVC hello world using JAVA examples on the net.

Comments

-1
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Click the Circle Game</title>
  <style>
    body {
      font-family: sans-serif;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      height: 100vh;
      margin: 0;
      background-color: #f0f0f0;
    }
    #gameArea {
      width: 600px;
      height: 400px;
      background-color: #fff;
      border: 2px solid #ccc;
      position: relative;
      margin-top: 20px;
      overflow: hidden;
    }
    .circle {
      width: 50px;
      height: 50px;
      background-color: red;
      border-radius: 50%;
      position: absolute;
      cursor: pointer;
    }
    #scoreBoard {
      font-size: 1.5em;
    }
  </style>
</head>
<body>
  <div id="scoreBoard">Score: 0 | Time: 30</div>
  <div id="gameArea">
    <div id="circle" class="circle"></div>
  </div>

  <script>
    const circle = document.getElementById("circle");
    const gameArea = document.getElementById("gameArea");
    const scoreBoard = document.getElementById("scoreBoard");

    let score = 0;
    let timeLeft = 30;
    let timer;

    function randomPosition() {
      const maxX = gameArea.clientWidth - 50;
      const maxY = gameArea.clientHeight - 50;
      const x = Math.floor(Math.random() * maxX);
      const y = Math.floor(Math.random() * maxY);
      circle.style.left = x + "px";
      circle.style.top = y + "px";
    }

    function updateScore() {
      score++;
      scoreBoard.textContent = `Score: ${score} | Time: ${timeLeft}`;
      randomPosition();
    }

    function startGame() {
      score = 0;
      timeLeft = 30;
      scoreBoard.textContent = `Score: 0 | Time: 30`;
      randomPosition();

      circle.style.display = "block";

      timer = setInterval(() => {
        timeLeft--;
        scoreBoard.textContent = `Score: ${score} | Time: ${timeLeft}`;
        if (timeLeft <= 0) {
          clearInterval(timer);
          circle.style.display = "none";
          alert(`Game over! Your score: ${score}`);
        }
      }, 1000);
    }

    circle.addEventListener("click", updateScore);

    // Start game on load
    window.onload = startGame;
  </script>
</body>
</html>

2 Comments

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
Hello Alfie and thank you for your contribution. Just a notice, the OP asks about java, not javascript (which you use in your answer). Your code cannot be used as a solution.

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.