Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 606141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:13:22+00:00 2026-05-13T17:13:22+00:00

I have a question on code reuse in JSP. I have a JSP page

  • 0

I have a question on code reuse in JSP. I have a JSP page example.jsp that issues a call to a database and gets the results. I have a java class HelperClass.java that accepts a record and prints out the different fields

response.getWriter().println

Now my JSP page has HTML as well and the problem is the content printed out by the HelperClass appears before the content in the JSP page. E.g.

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

output is

secondline This is the first line

Is this a known issue. What is the best way to design an HelperClass for a JSP page that prints content out to the page. Any pointers would be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T17:13:22+00:00Added an answer on May 13, 2026 at 5:13 pm

    Just do not use a “HelperClass to print data”. This makes no sense. There you have EL for.

    ${bean.property}
    

    That’s all. Use a servlet to control, preprocess and postprocess requests. Use taglibs (e.g. JSTL) and EL to access and display backend data.

    Here’s a basic kickoff example of a Servlet which preprocesses the request before display in 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's available as `${persons}` in EL.
        request.getRequestDispatcher("/WEB-INF/persons.jsp").forward(request, response); // Forward to JSP for display.
    }
    

    Here, the Person is just a Javabean class which represents a real world entity.

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

    The PersonDAO#list() method just returns a List of Person objects from the DB:

    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;
    }
    

    Map the servlet in web.xml on an url-pattern of /persons. The JSP is hidden in /WEB-INF so that nobody can access it directly without requesting the servlet first (else one would get an empty table).

    Now, here’s how persons.jsp look like, it uses JSTL (just drop jstl-1.2.jar in /WEB-INF/lib) c:forEach to iterate over a List and it uses EL to access the backend data and bean properties. The servlet has put the List<Person> as request attribute with name persons so that it’s available by ${persons} in EL. Each iteration in c:forEach gives a Person instance back, so that you can display their proeprties with 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>
    

    Call it by http://example.com/contextname/persons. That’s all. No need for a “HelperClass to print data” 😉 To learn more about JSTL, check Java EE tutorial part II chapter 7 and to learn more about EL, check Java EE tutorial part II chapter 5. To learn more about stuff behind PersonDAO, check this article.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question about Object serialization and deserialization when class field changed. If
I have a Java web application that has a 'disconnected' Java Swing desktop app.
I have been developing and testing with inline jQuery. I am using CodeIgniter and
This must be a trivial question to some, but I haven't found any actual
I'm sorry for ambiguous title, but this question is hard for me to put
The top answer to this question describes a technique to implement an efficient XSLT
So I'm trying to install a web application and I stumbled upon this question:
I'm currently writing a PyGTK application and I'd like some advice as to the
Considering a hypothetical situation where an old, legacy presentation library has been maintained over
[Update - Sep 30, 2010] Since I studied a lot on this & related

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.