Does it make sense to choose JavaScript instead of Java for server-side programming and why


Introduction

We know that over a period of time JavaScript has gained popularity for its versatility and it’s easy to use. Java remains a top choice for server-side programming due to its reliability, scalability, and large ecosystem.

Let us discuss about why choosing Java as a server-side programming language over JavaScript is a better choice. We will talk about different approaches to explain the strengths of Java. Firstly, we will discuss the stability and robustness of Java by using Spring boot. Then we will explain the scalability and performance of Java. At last, we will explain the advantage of server-side programming in Java using JSP. By undertesting, these approaches we hope our readers will know why Java is still a top choice for server-side programming.

Stability and Robustness in using Java

In this programming example we will see the Stability and Robustness in Using Java.

Example

// Todo.java
public class Todo 
{
   private String title;
   private boolean completed;
}
// Control.java
@RestController
public class Control 
{
   private List<Todo> todos = new ArrayList<>();
   // API endpoint to retrieve all todos
   @GetMapping("/todos")
   public List<Todo> getTodos() {
      return todos;
   }
   // API endpoint to add a new todo
   @PostMapping("/todos")
   public Todo addTodo(@RequestBody Todo newTodo) {
      todos.add(newTodo);
      return newTodo;
   }
}
// Appli.java
@SpringBootApplication
public class Appli 
{
   public static void run(String args []) 
{
      SpringApplication.run (Application.class, args);
   }
}

It is a very simple program. Here we have defined a class named Todo and inside the class we have declared some member variables like “title” which is string type and “completed” which is Boolean type. After that we have declared another class named Control and we have used API endpoint to retrieve all todos by writing public List<Todo> getTodos (

Now we have run the springBootApplication and create another class named Appli. Inside this class we have executed the run method.

Choose Java for scalable server-side programming

We will now discuss scalability and performance of Java with the help of a concurrent task processing programming example.

Regarding scalability of java here we will give a simple example in this particular programming example.

Example

// Task.java
public interface Task {
   void execute();
}
// Taskmake.java
public class Taskmake implements Runnable {
   private BlockingQueue<Task> taskQueue;
   public Taskmake(BlockingQueue<Task> taskQueue) {
      this.taskQueue = taskQueue;
   }
   @Override
   public void run() {
      while (true) {
         try {
            Task task = taskQueue.take();
            task.execute();
         } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            break;
         }
      }
   }
}
// Runn.java
public class Runn
 {
   public static void runn (String args []) 
{
      BlockingQueue<Task> taskQueue = new LinkedBlockingQueue<>();
      // Create and start task threads we take 4 threads
      int totThread = 4;
      ExecutorService executor = Executors.newFixedThreadPool(totThread);
      for (int i = 0; i < totThread; i++) {
         executor.execute(new Taskmake(taskQueue));
      }
      // Submit tasks to the task queue
      taskQueue.add(() -> System.out.println("Task 1 executed"));
      taskQueue.add(() -> System.out.println("Task 2 executed"));
      taskQueue.add(() -> System.out.println("Task 3 executed"));
      // Shutdown threads
      executor.shutdown ();
   }
}

First, we have defined an interface named Task and inside this interface we have called a function named execute ()

Then a class named TaskMake implemented the runnable interface and have done some important coding.

private BlockingQueue<Task> taskQueue;
   public Taskmake(BlockingQueue<Task> taskQueue)

After that we have executed the run () method and execute the runn.java file. Here we have declared a class named Runn and inside this class we will call the run () function.

BlockingQueue<Task> taskQueue = new LinkedBlockingQueue<>();

Now we have created and started task threads we take 4 threads by writing

ExecutorService executor = Executors.newFixedThreadPool(totThread);

After that we have submited tasks to the task queue to completed all the task.

taskQueue.add(() -> System.out.println("Task 1 executed"));
taskQueue.add(() -> System.out.println("Task 2 executed"));
taskQueue.add(() -> System.out.println("Task 3 executed"));

And shut down the all threads by calling a function named executor.shutdown ().

Server-Side Programming in Java using JSP

The Java Server Page (JSP) is a very useful tool that allows us to create dynamic web pages by embedding Java code within HTML templates. It is commonly used for server-side programming in Java web applications. By using Java as a server-side programming language provides many advantages over JavaScript. We will show a basic programming example to understand server-side programming in Java using JSP.

In this particular programming example, we will create a html page and has done some html code to design the page for the user in a systematic manner.

Example

<html>
<head>
<title>Register please!! </title>
</head>
<body>
<h1>Register here</h1>
<form method="post" action="RegistrationServlet">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required><br>
<label for="passwd">Passwd:</label>
<input type="passwd" id="passwd" name="passwd" required><br>
<label for="mailid">Mailid:</label>
<input type="mailid" id="mailid" name="mailid" required><br>
<input type="submit" value="Register">
</form>
</body>
</html>
** RegistrationServlet.java page **
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class RegistrationServlet extends HttpServlet
 {
   protected void doPost (HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException 
{
      String name = request.getParameter("name");
      String passwd = request.getParameter("passwd");
      String mailid = request.getParameter("mailid");
      response.sendRedirect("confirmation.jsp");
   }
}
** confirmation.jsp program**
<html>
<head>
<title>Success !!</title>
</head>
<body>
<h1>Registration Successful!</h1>
</body>
</html>

Here we have used all the important html tags like head, body, label, method, input type etc. After completing all the design with the help of html we have created a page named RegistrationServlet.java page and import some important packages. After that we have defined a class named RegistrationServlet which extends Httpservlet and initialize some values to the name, password, mailed etc. Then we have confirmed the jsp program and successfully registered the page to the server.

Conclusion

Describing all the programming example on the above regarding server side programming it is clear to us that usefulness of java and java script are totally different. It is totally depending on the requirement of the user which type of software will be needed. Both have the same priority regarding the immaculate software experience.

Updated on: 04-Oct-2023

55 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements