Search code examples
javahtmlprintwriter

JAVA - Printing user input into HTML


I want to acquire user input using Java and print it in a index.html form. The user input is in an arraylist. By the look of the code, you can tell it will print the HTML from #nbOfPieces times. How can I print the individual items from the arraylist and only print out the html code once?

for (int i = 0;i < nbOfPieces ;i++ ) {
                pw.println(
                    "<html>\n" +
                            "<head>\n" +
                            "    <title>Maria Shop</title>\n" +
                            "    <link rel=\"stylesheet\" href=\"style.css\">\n" +
                            "</head>\n" +
                            "<body>\n" +
                            "<center>\n" +
                            "<section id=\"portfolio\">\n" +
                            "        <h1>MARIA TEST</h1>\n" +
                            "            <div class=\"img-box\">\n" +
                            "            <a href=" + links.get(i) + "><img src=\"./img/"+subImg.get(i)+"\" alt=\"CPU image\"></a>\n" +
                            "            </div>\n" +
                            "            <div class=\"img-box\">\n" +
                            "                <a href=" + links.get(i) + "><img src=\"./img/"+subImg.get(i)+"\" alt=\"CPU image\"></a>\n" +
                            "            </div>\n" +
                            "            <div class=\"img-box\">\n" +
                            "                <a href=" + links.get(i) + "><img src=\"./img/"+subImg.get(i)+"\" alt=\"CPU image\"></a>\n" +
                            "            </div>\n" +
                            "</section>\n" +
                            "</center>\n" +
                            "</body>\n" +
                            "</html>"
                );
            }

Solution

  • Just split what you will print in three as this:

    pw.println(
       "<html>\n" +
        "<head>\n" +
        "    <title>Maria Shop</title>\n" +
        "    <link rel=\"stylesheet\" href=\"style.css\">\n" +
        "</head>\n" +
        "<body>\n" +
        "<center>\n" +
        "<section id=\"portfolio\">\n" +
        "        <h1>MARIA TEST</h1>"
    );
    //Repeated sectection
    for (int i = 0;i < nbOfPieces ;i++ ) {
        pw.println(
            "            <div class=\"img-box\">\n" +
            "            <a href=" + links.get(i) + "><img src=\"./img/"+subImg.get(i)+"\" alt=\"CPU image\"></a>\n" +
            "            </div>\n" +
            "            <div class=\"img-box\">\n" +
            "                <a href=" + links.get(i) + "><img src=\"./img/"+subImg.get(i)+"\" alt=\"CPU image\"></a>\n" +
            "            </div>\n" +
            "            <div class=\"img-box\">\n" +
            "                <a href=" + links.get(i) + "><img src=\"./img/"+subImg.get(i)+"\" alt=\"CPU image\"></a>\n" +
            "            </div>"
        );
    }
    pw.println(
        "</section>\n" +
        "</center>\n" +
        "</body>\n" +
        "</html>"
    );