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 8500121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:51:26+00:00 2026-06-11T00:51:26+00:00

getting an IOExcepiton on the PrintWriter when doing this in doPost method of servlet

  • 0

getting an IOExcepiton on the PrintWriter when doing this in doPost method of servlet class:

doPost(HttpServletRequest request,
        HttpServletResponse response){
    String replyMessage = "STATUS_ACCEPT";
    response.setStatus(HttpServletResponse.SC_OK);
    response.setContentType("text/plain; charset=utf-8");

    response.setContentLength(replyMessage.length());
    response.setIntHeader("content-length", replyMessage.length());
    response.getWriter().println(replyMessage);//getting IOException closed on calling this method
    response.flushBuffer();}

If I use the outputstream for sending data there is no error:

    ServletOutputStream outputStream = response.getOutputStream();
    byte[] result = "STATUS_ACCEPT".getBytes("UTF-8");

    response.setStatus(HttpServletResponse.SC_OK);
    response.setContentType("text/plain; charset=utf-8");

    response.setContentLength(result.length);
    response.setIntHeader("content-length", result.length);

    outputStream.write(httpOutData);

how should i use the printwriter for not getting the IOException?

  • 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-11T00:51:28+00:00Added an answer on June 11, 2026 at 12:51 am

    Most like the reason is this:

    response.setContentLength(replyMessage.length());
    // ...
    response.getWriter().println(replyMessage)
    

    Which means, that your response content is at least one byte longer than announced by setContentLenght: The trailing newline appended by println(). Perhaps the difference is two bytes: CR and LF.

    Note: If you fix this you will still have problems in the general case: You set the content length to the number of characters which in UTF-8 encoding is not always the number of bytes. setContentLength() wants the number of bytes.

    Therefore it would be better to simply delete all code fiddling with the content-length because if you don’t set it, it will be calculated automatically.

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

Sidebar

Related Questions

Getting this weird LINQ error. title = System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Xml.Linq.XElement,System.String Here is the code I have:
I am getting the subjected error, could you please help servlet public class FirstClass
I keep getting stuck on this. Here is my servlet: package HWpackage; import java.io.*;
I have written a Servlet something like this public class ServletUtil extends HttpServlet {
Getting red warning message that expected identifier for this statement CALayer = ImageView.layer; Using
Getting this out of the way; I don't frequently do much in php or
jsp file which contains username & password which is processed at LoginServlet...this servlet does
i am having trouble getting the page to work, what i am doing is
I have this code: public class cuemath extends HttpServlet{ /** * */ private static
I'm trying to make sure my data path -- a Tomcat servlet getting data

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.