- What response.getWriter() returns is PrintWriter, which is a print output stream.
- response.getWriter() .print(), not only can print output in text format (including html tags), but also convert an object into binary byte output with default encoding
- response.getWriter().writer(), can only print out text format (including html tags), not objects.
Client demo code
<script type="text/javascript" src="js/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("#button1").click(function(){ $.get("AJAXServer",null,function(data){ alert(data); $("#info").html(data); }); }); }) </ script > </ head > < body > < input type = "button" id = "button1" value = "Click me to get server response information" onclick = "a()" ; /> < div id = "infd
- 1
Output object server-side code:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Person person = new Person(); person.setName("sjl"); response.getWrier().print(person);}
Running result:
server.Person@5bd5ab
If you use response.getWriter().writer(), the code will report an error
Server-side code that demonstrates text information:
response.getWriter().write("this is <b>server</b> response"); /**response.getWriter().print("this is<b>server2</b>response")*/;
Output result
this is server response
The output of the commented code is the same