8

Hi I know the language of HTML, CSS, and Java pretty well, but I have absolutely no idea on how to connect the java backend to frontend. I'm a complete amateur when it comes to programming, I just know the basics of each of these languages pretty well. I have my HTML and CSS coded to take in input, and I have all the calculations written in Java, so I have the functions of what I want to do all ready. I just don't know how to pass the user's input from the HTML, to the calculations in Java. I just run my Java code using scanner as the user input, but I want to integrate all my codes to make it a real website, and I think I'm going to need more than scanner for a user to input something on a website. I researched this topic multiple times, and I found information about serverlets and IntelliJ, but the information was confusing to me because again, I am a complete amateur in computer science (other than knowing these languages). Some also said the best way was to convert my java code to javascript, but I'm not sure if that's the best way. Any suggestions/information on what to do? Here's some of my java code (this part takes in user input, searches for the numbers before the last slash, then adds it to double firsum). Thanks!

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    double firsum = 0; 
    while (sc.hasNext()) {
        String ip = sc.nextLine();
        int def = ip.lastIndexOf("/");
        double firnum = 0;
        for (int i = 1; i < 7; i++) {
            if (ip.charAt(def-i) != '\t') {
                firnum = Double.parseDouble(ip.substring(def - i, def));
                out.println(firnum);
            }
        }
        firsum += firnum;
    }
} 

4 Answers 4

8

Like cricket said, you can use a Java based web framework, I suggest Spring. This will provide all the functionality you need such as allowing you to save, index, search, write routes, etc for you application. Depending on how new you are, you probably want to make a basic CRUD app to get to know Spring.

You also want to think of your app from an architectural perspective. Do you want to make a single monolithic application that handles all your business logic, services, and the frontend - or do you want to make it more modular with microservices? (The second option is probably better). For this you are going to want to write an API that handles all your application's data (using Spring), and then make a small frontend app that gets that information and presents it.

Hope it helped :)

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

Comments

0

You're going to need javascript as a connecting bridge between your front end website and backend java.

Javascript is the processing language of the web, and it will run natively on a clients browser. You can use its ajax feature to take user input and upload the value to your server.

However because you mention you are new to programming, I might suggest looking into a more "standard" server language than java. Don't get me wrong it is commonly used, but for beginners languages like php and python have much more front end connection "built in" to make your life easier.

2 Comments

Thanks for the information! Do you have any suggestions on how to convert this code to PHP? I'm considering using PHP instead because I have a local host, but I don't know the PHP equivalent for scanner hasNext(), nextLine(), lastIndexOf, and parseDouble. What would this code be in PHP? Or is it not possible?
@A.Mis I think switching to php would be a great choice! In regards to converting your java to php it would be best if you ask that as a new question. It's kind of hard auditing such lengthy code here in the comment section.
0

You can use a Java micro web frameworks like Spring, Dropwizard, SparkJava to respond to HTTP requests and produce HTML content, but the common denominator to all frameworks is Servlets. Servlets respond to HTTP requests and can send "static files" (html, css, js, images, etc). Other options include full web application servers such as Tomcat, JBoss, GlassFish, Java EE

In order for dynamic form actions to occur, most web developers would use AJAX (Javascript), and have the front-end communicate via a backend API.

You'll also find that majority of websites are not using Java for hosting HTML/CSS/JS, as you can use any backend language with any front-end library via RESTful actions, which, on the web, are interacted with via Javascript (not Java Scanner objects)

Comments

0

Connecting a Java backend with an HTML/CSS frontend involves using frameworks like Spring Boot for the backend and RESTful APIs for communication, enabling seamless data exchange and dynamic user experiences.

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.