Issue
According to the documentation, the setContentLength(int len)
method of the ServletResponse
interface sets the length of the content body in the response In HTTP servlets, this method sets the HTTP Content-Length header.
- Parameters:
len
- an integer specifying the length of the content being returned to the client; sets the Content-Length header
Here is the from the doGet()
method of the class which extends HttpServlet
:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/plain");
response.setContentLength(6);
PrintWriter out = response.getWriter();
out.write("What's your name? ");
out.write("" + response.isCommitted());
}
When I run this on the Chrome browser, the output is: What's your name? false
I am confusing what actually is the metric for calculating the length of the setContentLength()
here, whether it is in byte, character, or something, and if it sets the content length of the body, why response.isCommitted()
returns false
and the length of the text I want to output was not truncated somehow? Thanks.
Solution
I am confusing what actually is the metric for calculating the length of the setContentLength() here, whether it is in byte, character, or something
It's in byte
.
Check this or Section#14.13 Content-Length of the specification for details.
When I run this on the Chrome browser, the output is: What's your name? false
I doubt the validity of what you are saying.
Given below is the screenshot of my Chrome browser:
Given below is the screenshot of my Firefox browser:
Answered By - Arvind Kumar Avinash
Answer Checked By - Katrina (JavaFixing Volunteer)