Writer using response (ix)

Source: Internet
Author: User
Tags tomcat

PrintWriter getwriter()

Gets a character stream that, through the write(string s) method of a character stream, sets the string to the response buffer, and then Tomcat assembles the contents of the response buffer into an HTTP response back to the browser side.

About the problem of setting Chinese garbled characters

Cause: The default encoding for the response buffer is iso8859-1, there is no Chinese in this Code table, charset encoding can be set by response setcharacterencoding(String response)

But we found that the client was still unable to display the text correctly

Cause: We set the response buffer encoding to UTF-8, but the browser's default encoding is the local system encoding, because we are all Chinese system, so the client browser's default encoding is GBK, we can manually modify the browser's encoding is UTF-8.

We can also specify the encoding of the browser parsing page in the code,

Specifies that the encoding for page parsing is UTF-8 by response's setcontenttype(String type) method

Response.setcontenttype ("Text/html;charset=utf-8");

The above code can not only specify the browser parsing page encoding, but also contains setcharacterencoding functions, so in the actual development as long as you write response.setcontenttype ("Text/html;charset =utf-8 "), you can solve the page output Chinese garbled problem.

Writer is writing to the response buffer. The Tomcat engine then takes data from the response buffer, which makes up the HTTP response.

Package com.ken.content;

Import java.io.IOException;
Import Java.io.PrintWriter;

Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;

public class Textservlet extends HttpServlet {
	private static final long serialversionuid = 1L;

	protected void doget (HttpServletRequest request, httpservletresponse response)
			throws Servletexception, IOException {

		PrintWriter writer = Response.getwriter ();
		Writer.write ("Hello response");
		Writer.write ("China");
	}

	protected void DoPost (HttpServletRequest request, httpservletresponse response)
			throws Servletexception, IOException {
		doget (request, response);
	}
}

Response directly write Chinese, will produce garbled.

Writer writes toward the response buffer. The response buffer itself has an encoding. Our string is encoded according to the Code table, and the encoded content has a buffer. The contents of the buffer are then given to the client, which is decoded using the Code table on the client. Response Default Code table is iso-8859-1, this Code table is not Chinese. To inform response to check UTF-8. Note that the Setup Code table is before you get to writer.


Package com.ken.content;

Import java.io.IOException;
Import Java.io.PrintWriter;

Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;

public class Textservlet extends HttpServlet {
	private static final long serialversionuid = 1L;

	protected void doget (HttpServletRequest request, httpservletresponse response)
			throws Servletexception, IOException {

		//Set response query's code table
		response.setcharacterencoding ("UTF-8");
		PrintWriter writer = Response.getwriter ();
		Writer.write ("Hello response");
		Writer.write ("Hello");
	}

	protected void DoPost (HttpServletRequest request, httpservletresponse response)
			throws Servletexception, IOException {
		doget (request, response);
	}
}

There are also garbled characters here. is because the decoding of the client is not specified. Most clients check the encoding in the local encoding format.



Solution:

Package com.ken.content;

Import java.io.IOException;
Import Java.io.PrintWriter;

Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;

public class Textservlet extends HttpServlet {
	private static final long serialversionuid = 1L;

	protected void doget (HttpServletRequest request, httpservletresponse response)
			throws Servletexception, IOException {

		//Set response query's code table
		response.setcharacterencoding ("UTF-8");

		Through a header Content-type tells the client what code table to encode
		response.setheader ("Content-type", "Text/html;charset=utf-8");

		PrintWriter writer = Response.getwriter ();
		Writer.write ("Hello response");
		Writer.write ("China");
	}

	protected void DoPost (HttpServletRequest request, httpservletresponse response)
			throws Servletexception, IOException {
		doget (request, response);
	}
}

In the development, we can omit the first sentence in the above two sentences. Only the second row is required, and Tomcat automatically sets the encoding for response to Utf-8.

Finally, we can also simplify and replace SetHeader with the setContentType () method.

package com.ken.content;
Import java.io.IOException;

Import Java.io.PrintWriter;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;

Import Javax.servlet.http.HttpServletResponse;

	public class Textservlet extends HttpServlet {private static final long serialversionuid = 1L;

		protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {

		Set the code table for the response query//response.setcharacterencoding ("UTF-8");
		Through a header Content-type tells the client what code table to encode//Response.setheader ("Content-type", "text/html;charset=utf-8");

		Response.setcontenttype ("Text/html;charset=utf-8");
		PrintWriter writer = Response.getwriter ();
		Writer.write ("Hello response");
	Writer.write ("China"); } protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, Ioexceptio
	n {doget (request, response); }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.