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

  • SEARCH
  • Home
  • 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 8832873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:38:24+00:00 2026-06-14T08:38:24+00:00

For all you duplicate fanatics, there is a similar question on SO right here

  • 0

For all you “duplicate” fanatics, there is a similar question on SO right here. The difference is that I paint a vivid example that I can not understand the output of.

The documentation for JspWriter and PrintWriter says there are two differences: 1. JspWriter can throw exceptions, PrintWriter should not do so. 2. JspWriter uses a PrintWriter behind the scene, but since by default JSP pages are buffered, the PrintWriter won’t be created until the buffer is flushed – whatever that mean in the context of a JSP page. I’m not sure I’ve understood this last part. Consider this JSP page:

<%@page import="java.io.PrintWriter"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JspWriter versus PrintWriter</title>
    </head>
    <body>
        <p>I should be row one.</p>
        <%
            out.println("<p>JspWriter said: I should be the second row.</p>");

            PrintWriter pw = response.getWriter();
            pw.println("<p>PrintWriter said: I should be the third row.</p>");
        %>
        <p>I should be the fourth row.</p>
    </body>
</html>

It produces the following output:

PrintWriter said: I should be the third row.
I should be row one.
JspWriter said: I should be the second row.
I should be the fourth row.

As you can see, the JspWriter outputs his string to the browser were I expected it to. But PrintWriter outputs his string before all else is sent to the browser. If we examine the source code sent to the browser, the PrintWriter’s string is sent as the very first line, before the DOCTYPE declaration. So in the example above, exactly what happens?

  • 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-06-14T08:38:25+00:00Added an answer on June 14, 2026 at 8:38 am

    The explanation is in your own question:

    JspWriter uses a PrintWriter behind the scene, but since by default
    JSP pages are buffered, the PrintWriter won’t be created until the
    buffer is flushed

    This means that what is written to the JspWriter is buffered, and once this buffer is flushed (either because the buffer is full, or because the JSP has reached the end of its execution), the contents is written to the response’s PrintWriter.

    So the flow of your example is the following one:

    • static HTML code until the scriptlet: written to the in-memory buffer
    • out.println(…): written to the in-memory buffer
    • pw.println(…): written to the response
    • static HTML code until the end of the JSP: written to the in-memory buffer
    • flush of the in-memory buffer: all it contains is written to the response
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there anyway I can erase all the duplicate entries from a certain table
Possible Duplicate: PHP take all combinations I'm thinking of making something in PHP that
Possible Duplicate: How to get all of the IDs with jQuery? How can i
I want to find all duplicate codes in my android project so can anyone
How would I delete all duplicate data from a MySQL Table? For example, with
i am looking for the query, deletes the all duplicate values. Example Table: 1
we have a scenario here where we need to delete all the duplicate rows
First of all, I'd like to point out to all you duplicate question hunters
I'm attempting to build a query that will return all non duplicate (unique) records
I have the following Mysql select that returns all the duplicate email addresses in

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.