读书笔记-Review: HttpServletResponse

本文介绍如何使用HttpServletResponse发送数据到客户端,包括设置内容类型、获取写入流、设置响应头等操作。此外还讨论了如何进行请求重定向,并区分了请求重定向与请求转发的区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. You use the Response to send data back to the client
  2. The most common methods you'll call on the response object(HttpServletResponse) are setContentType() and getWriter()
  3. Be careful-many developers assume the method is getPrintWriter(),but it's getWriter()
  4. The getWriter() method lets you do character I/O to write HTML(or something else) to the stream
  5. You can also use the response to set headers,send errors,and add cookies
  6. In the real world,you'll probably use a JSP to send most HTML response,but you may still use a response stram to send binary data(like a JAR file,perhaps) to the client
  7. The method you call on your response for getting a binary stream is getOutputStream()
  8. The setContentType() method tells the browser how to handle the data coming in with the response.Typical content types are "text/html","application/pdf",and "image/jpeg".
  9. You can set response headers using addHeader() or setHeader(). The difference depends on whether the header is already part of the response.If  it is,setHeader() will replace the value,but addHeader will add an additional value to the existing response.If the header is not already part of the response, the setHeader() and addHeader() behave in exactly the same way
  10. If you don't want to respond to a request,you can redirect the request to a different URL.The browser takes care fo sending the new request to the URL you provide
  11. To redirect a request,call sendRedirect(aStringURL) on the response.
  12. You cannot call sendRedirect() after the response is committed! In other words,if you have already written something to the stream,it is too late to do a redirect  
  13. A request redirect is different from a request dispatch.A request dispatch (covered more in another chapter) happens on the server,while a redirect happens on the client.A request dispatch hands the request to another component on the server,usually within the same web app.A request redirect simply tells the browser to go a different URL.

转载于:https://www.cnblogs.com/yql1986/archive/2011/04/01/2002232.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值