<%-- Document : table Created on : 27 Apr 2025, 7:37:17 pm Author : happy --%> <%@ page…
- User
<%-- Document : table Created on : 27 Apr 2025, 7:37:17 pm Author : happy --%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
var xhr = new XMLHttpRequest(); xhr.open("GET", "table.jsp?num=" + num + "&ajax=true", true); xhr.onreadystatechange = function() { if (xhr.readyState === 4 && xhr.status === 200) { document.getElementById("result").innerHTML = xhr.responseText; } }; xhr.send(); } </script><% String ajaxCall = request.getParameter("ajax"); if (ajaxCall != null && ajaxCall.equals("true")) { String numStr = request.getParameter("num"); int num = 0;
if (numStr != null && !numStr.trim().equals("")) { try { num = Integer.parseInt(numStr); } catch (NumberFormatException e) { out.print("Invalid number!"); return; } for (int i = 1; i <= 10; i++) { out.print(num + " x " + i + " = " + (num * i) + "<br>"); } } else { out.print("Please enter a number."); } return; // Very important to STOP processing further HTML }%>
<h2>Enter a Number to Get its Table:</h2> <input type="number" id="number"> <button onclick="getTable()">Get Table</button> <div id="result" style="margin-top:20px;"></div> Assistant
This JSP page demonstrates a dynamic multiplication table generator using AJAX with a single file. Here's a breakdown:
How It Works:
-
User Interface:
- Displays an input field and a button to submit a number.
- An empty
div(id="result") waits to display the table.
-
AJAX Request:
- When the user clicks "Get Table", the
getTable()JavaScript function:- Retrieves the input value.
- Sends an asynchronous GET request to the same JSP page with parameters
num(the number) andajax=true.
- When the user clicks "Get Table", the
-
Server-Side Handling:
- The JSP checks for the
ajax=trueparameter:- If present, it generates the multiplication table for
num(1× to 10×) and outputs only the result (no HTML). - Handles invalid inputs (non-numeric or empty) with error messages.
- Uses
returnto prevent rendering the full page again.
- If present, it generates the multiplication table for
- On normal page load (no
ajax=true), the form is displayed normally.
- The JSP checks for the
-
Dynamic Update:
- The response from the AJAX call (the table) is inserted into the
resultdiv, updating the page without reloading.
- The response from the AJAX call (the table) is inserted into the
Key Features:
- Single File: Combines form, logic, and AJAX response in one JSP.
- Efficiency: Avoids full-page reloads for instant updates.
- Validation: Handles invalid input gracefully.
Example Output:
If the user enters
5, the response would be:5 x 1 = 5<br>5 x 2 = 10<br>...<br>5 x 10 = 50Displayed in the
resultdiv.Improvements:
- Use
POSTfor data submission (more secure). - Add JavaScript error handling for network issues.
- Style the table with CSS for better readability.
This approach efficiently separates AJAX logic from UI using a single JSP file.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.