Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Using JSP for Dynamic Web Content Generation

Tech 3

While Servlets can dynamically generate HTML responses, they require extensive string concatenation. Writting Java code that builds HTML strings is cumbersome and error-prone, as it involves handling HTML syntax, CSS styles, external JavaScript files, and various rseource paths.

JSP (JavaServer Pages) simplifies this process by allowing developers to embed Java code directly within HTML. This approach makes it easier to generate dynamic content. Below is an example that creates an HTML table based on user-provided parameters for rows and columns.

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Dynamic Table Generator</title>
    <style>
        .data-table { border: 2px solid #2c3e50; width: 60%; margin: 20px auto; }
        .data-cell { border: 1px solid #7f8c8d; padding: 8px; text-align: center; }
    </style>
</head>
<body>
    <% 
        String rowsParam = request.getParameter("rows");
        String colsParam = request.getParameter("cols");
        int numRows = Integer.parseInt(rowsParam);
        int numCols = Integer.parseInt(colsParam);
        StringBuilder tableBuilder = new StringBuilder();
        tableBuilder.append("<table class='data-table'>");
        for (int r = 1; r <= numRows; r++) {
            tableBuilder.append("<tr>");
            for (int c = 1; c <= numCols; c++) {
                tableBuilder.append("<td class='data-cell'>");
                tableBuilder.append("R" + r + "C" + c);
                tableBuilder.append("</td>");
            }
            tableBuilder.append("</tr>");
        }
        tableBuilder.append("</table>");
        out.print(tableBuilder.toString());
    %>
</body>
</html>

Embedding Java Logic in JSP

JSP allows Java code to be inserted using <% %> scriptlets. To output a variable's value direct into the HTML, use the <%= %> expression syntax. The following example generates a random score and displays its corresponding grade.

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Score Grade Evaluator</title>
</head>
<body>
    <% 
        int randomScore = (int)(Math.random() * 101);
    %>
    <p>Generated Score: <%= randomScore %></p>
    <p>Grade: 
    <% 
        int scoreCategory = randomScore / 10;
        switch(scoreCategory) {
            case 10:
            case 9:
    %>
                A
    <% 
                break;
            case 8:
    %>
                B
    <% 
                break;
            case 7:
    %>
                C
    <% 
                break;
            case 6:
    %>
                D
    <% 
                break;
            default:
    %>
                E
    <% 
        }
    %>
    </p>
</body>
</html>

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.