Response. getwriter (). Write () produces garbled characters and the difference between response. getwriter (). Write and out. Print

Source: Internet
Author: User

If response. getwriter (). Write () is used in the code when return to the page through action in struts1, Chinese characters on the JSP page may become garbled characters. Someone on Baidu said it was changed to response. getwriter (). Print (), and the attempt was invalid. Add response. setcontenttype ("text/html; charset = UTF-8"); before response. getwriter (). Write. The contenttype written on the JSP page is used for response.
It is worth noting that when Ajax is used, if response is not used in the Action Method of the first page display. getwriter (). write (), so you do not need to write response. setcontenttype ("text/html; charset = UTF-8"); now. Because it is not submitted throughout the page, the return in the action is not returned to display the entire page.

 

 

 

Printwriter out = response. getwriter ();

Character output stream, displaying strings to HTML output;
Printwriter out = response. getwriter ();
Out. println ("Hello! ");
Httpsession session = request. getsession (true); defines a session in sefvlet, and then calls the getvalue () and setvalue () Methods of seeion to transmit information between pages, in JSP, both objects are automatically defined and can be called directly.

For example, you can get a pen through the httpservletresponse object, and then you can use the out. Print () method to write anything you want to display on the webpage.
Out. Print ("<HTML> <body> ");
Out. Print ("any content ");
Out. Print ("</body>

What is the relationship between out and response. getwriter in JSP?

The same JSP deployment in WebSphere and WebLogic has different effects.

<%
Out. println ("1 ");
Response. getwriter. Write ("2 ");
Out. println ("3 ");
123 is displayed in WebLogic, but 213 is displayed in WebSphere. I wonder if this is inconsistent with J2EE specifications?

Do you want to know if the out variable is obtained through response? Or is there no relationship at all, but when the page is output, the output content is included in the response? Thank you!

Of course, out comes from reponse.

Of course, there is no relationship at all. The out in JSP is
Pagecontext. getout (); instead of response. getwriter ()

After doing an experiment, you can see whether the two items are the same.
<% Out. println ("first JSP out ";
Response. getwriter (). println ("second response out"; %> check the result.

If the bottom layer is the same stream object, the output sequence should be the same. Besides, why do you need to link two different objects. You can either look at the source code of pagecontext, and the implementation of each producer is not exactly the same. Your problem lies in their implementation details.

Response. getwriter () is a servlet method call that is used to output the output stream. The out is an implicit object of JSP. It is automatically initialized and uses out. println () to output data streams. The implicit objects in Servlet and JSP are consistent.

The out in JSP is an instance of the jspwriterimpl class.
Printwrite is a Java. Io. printwriter class

Printwrite does not have ioexception in both the println () method and the print () method.
The method with the same name as the JSP out generates an ioexception when the buffer overflow switch is not automatically refreshed.

The other two are similar.
Note that the jspwriterimpl is generated by the container inherited from the standard interface.

Looking back at servlet recently, I found that some of my previous understandings are not very deep. Now I would like to ask you .!
Printwriter out = response. getwriter ();
In my understanding, the getwriter method of the response object is called to obtain the output of printwriter.
Can someone tell me more details?

I have always wondered: What is printwriter used?
Isn't jspwriter used for webpage output?

Printwriter is used to return the result to the client in HTML format.

Response. getwriter (). is called in servlet, while jspwriter () is used in JSP. For details about the differences between JSP and Servlet, refer to the document, including the background of Sun's JSP technology.
Printwriter out = response. getwriter (); When a servlet responds, It outputs the response information to the webpage through the out object. When the response ends, it is automatically disabled. So it can also be understood as: When we call response. the getwriter () object obtains the paint brush of the web page at the same time, and you can use this paint brush to draw anything you want to display on the web page.

Either of the response. getwriter () and response. getoutputstream () methods can be called only once and cannot be called repeatedly.

The out in JSP is an instance of the jspwriterimpl class.
Printwrite is a Java. Io. printwriter class

Out. Does println have line breaks?

Out. println ("1 ");
Out. println ("2 ");

The result of the above Code is as follows:
1
2

However, the result is:
1 2
A space is added.

Response. getwriter (). Print ("<br> ")
Cannot I wrap a line?

Out. println ("1 ");
Out. println ("2 ");
This is only a line feed of the HTML source code. The line feed you see must be output. <br>

 

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.