在服务器端编程中,选择 JavaScript 而不是 Java 是否合理,为什么?
简介
我们知道,随着时间的推移,JavaScript 凭借其多功能性和易用性获得了普及。由于其可靠性、可扩展性和庞大的生态系统,Java 仍然是服务器端编程的首选。
让我们讨论一下,为什么选择 Java 作为服务器端编程语言而不是 JavaScript 是一个更好的选择。我们将讨论不同的方法来解释 Java 的优势。首先,我们将使用 Spring Boot 讨论 Java 的稳定性和健壮性。然后我们将解释 Java 的可扩展性和性能。最后,我们将解释使用 JSP 在 Java 中进行服务器端编程的优势。通过了解这些方法,我们希望读者能够了解为什么 Java 仍然是服务器端编程的首选。
使用 Java 的稳定性和健壮性
在这个编程示例中,我们将看到使用 Java 的稳定性和健壮性。
示例
// 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); } }
这是一个非常简单的程序。在这里,我们定义了一个名为 Todo 的类,并在类中声明了一些成员变量,例如“title”(字符串类型)和“completed”(布尔类型)。之后,我们声明了另一个名为 Control 的类,并使用 API 端点通过编写 public List<Todo> getTodos ( 来检索所有待办事项。
现在我们运行 springBootApplication 并创建另一个名为 Appli 的类。在这个类中,我们执行了 run 方法。
Learn JavaScript in-depth with real-world projects through our JavaScript certification course. Enroll and become a certified expert to boost your career.
选择 Java 进行可扩展的服务器端编程
我们现在将借助并发任务处理编程示例来讨论 Java 的可扩展性和性能。
关于 Java 的可扩展性,我们将在本编程示例中提供一个简单的示例。
示例
// 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 (); } }
首先,我们定义了一个名为 Task 的接口,并在该接口中调用了一个名为 execute() 的函数。
然后,一个名为 TaskMake 的类实现了 Runnable 接口并完成了一些重要的编码。
private BlockingQueue<Task> taskQueue; public Taskmake(BlockingQueue<Task> taskQueue)
之后,我们执行了 run() 方法并执行了 runn.java 文件。在这里,我们声明了一个名为 Runn 的类,并在该类中调用 run() 函数。
BlockingQueue<Task> taskQueue = new LinkedBlockingQueue<>();
现在我们创建并启动了任务线程,通过编写代码创建了 4 个线程。
ExecutorService executor = Executors.newFixedThreadPool(totThread);
之后,我们将任务提交到任务队列以完成所有任务。
taskQueue.add(() -> System.out.println("Task 1 executed")); taskQueue.add(() -> System.out.println("Task 2 executed")); taskQueue.add(() -> System.out.println("Task 3 executed"));
并通过调用名为 executor.shutdown() 的函数关闭所有线程。
使用 JSP 的 Java 服务器端编程
Java Server Page (JSP) 是一种非常有用的工具,它允许我们通过在 HTML 模板中嵌入 Java 代码来创建动态网页。它通常用于 Java Web 应用程序中的服务器端编程。使用 Java 作为服务器端编程语言比 JavaScript 提供了许多优势。我们将展示一个基本的编程示例来了解使用 JSP 的 Java 服务器端编程。
在这个特定的编程示例中,我们将创建一个 html 页面,并编写一些 html 代码以系统的方式为用户设计页面。
示例
<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>
在这里,我们使用了所有重要的 html 标签,如 head、body、label、method、input type 等。在使用 html 完成所有设计后,我们创建了一个名为 RegistrationServlet.java 的页面并导入了一些重要的包。之后,我们定义了一个名为 RegistrationServlet 的类,它扩展了 Httpservlet 并将一些值初始化为 name、password、mailed 等。然后我们确认了 jsp 程序并成功地将页面注册到服务器。
结论
描述了上面关于服务器端编程的所有编程示例,我们清楚地认识到 Java 和 JavaScript 的用途完全不同。用户需要哪种类型的软件完全取决于用户的需求。两者在提供完美的软件体验方面具有相同的优先级。