JSP Helper Class for Printing Content

I have a question about code reuse in JSP. I have a JSP page example.jspthat calls a database call and gets the results. I have a java class HelperClass.javathat takes a record and displays various fields

response.getWriter().println

My JSP page now has HTML, and the problem is that the content printed by HelperClass appears before the content on the JSP page. For instance.

<body>
    This is the first line <br/>
    HelperClass.printdata("second line"); 
</body>

conclusion

secondline This is the first line

This is a known issue. What is the best way to create a HelperClassJSP page that displays content on the page. Any pointers would be greatly appreciated.

+3
source share
1

"HelperClass ". . EL.

${bean.property}

. , . (, JSTL) EL .

, JSP:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    List<Person> persons = personDAO.list(); // Get list of persons from DB.
    request.setAttribute("persons", persons); // So it available as `${persons}` in EL.
    request.getRequestDispatcher("/WEB-INF/persons.jsp").forward(request, response); // Forward to JSP for display.
}

Person - , .

public class Person {
    private Long id;
    private String name;
    private String email;
    private Integer age;
    // Add/generate getters and setters here.
}

PersonDAO#list() List Person :

public List<Person> list() throws SQLException {
    Connection connection = null;
    Statement statement = null;
    ResultSet resultSet = null;
    List<Person> persons = new ArrayList<Person>();

    try {
        connection = database.getConnection();
        statement = connection.createStatement("SELECT id, name, email, age FROM person");
        resultSet = statement.executeQuery();
        while (resultSet.next()) {
            Person person = new Person();
            person.setId(resultSet.getLong("id"));
            person.setName(resultSet.getString("name"));
            person.setEmail(resultSet.getString("email"));
            person.setAge(resultSet.getInteger("age"));
            persons.add(person);
        }
    } finally {
        if (resultSet != null) try { resultSet.close(); } catch (SQLException logOrIgnore) {}
        if (statement != null) try { statement.close(); } catch (SQLException logOrIgnore) {}
        if (connection != null) try { connection.close(); } catch (SQLException logOrIgnore) {}
    }

    return persons;
}

web.xml url-pattern /persons. JSP /WEB-INF, , ( ).

, persons.jsp, JSTL ( jstl-1.2.jar /WEB-INF/lib) c:forEach List EL bean. List<Person> persons, ${persons} EL. c:forEach Person, proeprties EL.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

...

<table>
    <c:forEach items="${persons}" var="person">
        <tr>
            <td>${person.name}</td>
            <td>${person.email}</td>
            <td>${person.age}</td>
        </tr>
    </c:forEach>
</table>

http://example.com/contextname/persons. . "HelperClass ";) JSTL, Java EE II, 7, EL, 2 5 Java EE. PersonDAO, .

+6

Source: https://habr.com/ru/post/1733168/


All Articles