0

I am asked to create a e-commerce website for my school project. The frontend part will be developed using React. The backend part must be developed using Java where web application framework, e.g. Spring, Google Web Toolkit, Struts, … cannot be used. In this case, how can I connect the frontend and backend together?

There is a suggestion of using Node.js to call the Java for website services but I don't have a clear idea of how to do it as normally Node.js is used together with JavaScript to develop the backend. There is not much online resources for using Java with Node.js though. There is also suggestion on using Servlets or JSP to build the backend.

3
  • 1
    Using servlets would probably be the simplest option to develop a Java "backend" without a framework. Although using a framework is nowadays considered the only decent option to build web apps. Take a look over javatpoint.com/steps-to-create-a-servlet-using-tomcat-server. You'll deploy your servlet over Tomcat and you'll listen for connections on a certain port. You will send the requests to that port using React. Commented Dec 24, 2024 at 8:17
  • Your instructor must have something specific in mind, but of course we can’t know what that is. Could you post the actual text of the assignment? Commented Dec 24, 2024 at 10:02
  • Objectives: 1. Construct a software based on JAVA programming language with user-friendly graphical user interface in a team. 2. Practice the latest Human-computer interaction (HCI) principles in designing, assessing and implementing interactive computing systems. Details: Develop an e-commerce website using web server and Java. Web application framework, e.g. Spring, Google Web Toolkit, Struts, … cannot be used. That is the all content of the assignment Commented Dec 24, 2024 at 10:16

1 Answer 1

0

Without any frameworks or external dependencies you could also use the HttpServer class built into the language: https://docs.oracle.com/javase/8/docs/jre/api/net/httpserver/spec/com/sun/net/httpserver/HttpServer.html

A basic example can be found here: https://docs.oracle.com/javase/8/docs/jre/api/net/httpserver/spec/com/sun/net/httpserver/package-summary.html

Sample from the web page:

class MyHandler implements HttpHandler {
       public void handle(HttpExchange t) throws IOException {
           InputStream is = t.getRequestBody();
           read(is); // .. read the request body
           String response = "This is the response";
           t.sendResponseHeaders(200, response.length());
           OutputStream os = t.getResponseBody();
           os.write(response.getBytes());
           os.close();
       }
   }
   ...

   HttpServer server = HttpServer.create(new InetSocketAddress(8000));
   server.createContext("/applications/myapp", new MyHandler());
   server.setExecutor(null); // creates a default executor
   server.start();

You can add routes to all your pages like "/home" or "/about"

A handle like "/static/" can be used to serve your files. You get get the file path through the uri and write the file data in the response. Example: "/static/main.js"

For api calls you can add "/api/". Example: "/api/users/0"

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

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.