servlet构造response,以及获取RequestDispatcher对象
2011-08-25 11:37
246 查看
Constructing Responses
A response contains data passed between a server and the client. All responses implement the
ServletResponse interface. This interface defines methods that allow you to
■ Retrieve an output stream to use to send data to the client. To send character data, use the
PrintWriter returned by the response’s getWriter method. To send binary data in a
Multipurpose InternetMail Extensions (MIME) body response, use the
ServletOutputStream returned by getOutputStream. To mix binary and text data, as in a
multipart response, use a ServletOutputStream and manage the character sections
manually.
■ Indicate the content type (for example, text/html) being returned by the response with the
setContentType(String) method. This method must be called before the response is
committed. A registry of content type names is kept by the Internet AssignedNumbers
Authority (IANA) at http://www.iana.org/assignments/media-types/.
■ Indicate whether to buffer output with the setBufferSize(int) method. By default, any
content written to the output stream is immediately sent to the client. Buffering allows
content to be written before anything is sent back to the client, thus providing the servlet
Writing ServiceMethods
Chapter 10 • Java Servlet Technology 203
with more time to set appropriate status codes and headers or forward to another web
resource. The method must be called before any content is written or before the response is
committed.
■ Set localization information, such as locale and character encoding.
To invoke a resource available on the server that is running a web component, you must first
obtain a RequestDispatcher object by using the getRequestDispatcher("URL") method. You
can get a RequestDispatcher object from either a request or the web context; however, the two
methods have slightly different behavior. The method takes the path to the requested resource
as an argument. A request can take a relative path (that is, one that does not begin with a /), but
the web context requires an absolute path. If the resource is not available or if the server has not
implemented a RequestDispatcher object for that type of resource, getRequestDispatcher
will return null. Your servlet should be prepared to deal with this condition.
A response contains data passed between a server and the client. All responses implement the
ServletResponse interface. This interface defines methods that allow you to
■ Retrieve an output stream to use to send data to the client. To send character data, use the
PrintWriter returned by the response’s getWriter method. To send binary data in a
Multipurpose InternetMail Extensions (MIME) body response, use the
ServletOutputStream returned by getOutputStream. To mix binary and text data, as in a
multipart response, use a ServletOutputStream and manage the character sections
manually.
■ Indicate the content type (for example, text/html) being returned by the response with the
setContentType(String) method. This method must be called before the response is
committed. A registry of content type names is kept by the Internet AssignedNumbers
Authority (IANA) at http://www.iana.org/assignments/media-types/.
■ Indicate whether to buffer output with the setBufferSize(int) method. By default, any
content written to the output stream is immediately sent to the client. Buffering allows
content to be written before anything is sent back to the client, thus providing the servlet
Writing ServiceMethods
Chapter 10 • Java Servlet Technology 203
with more time to set appropriate status codes and headers or forward to another web
resource. The method must be called before any content is written or before the response is
committed.
■ Set localization information, such as locale and character encoding.
To invoke a resource available on the server that is running a web component, you must first
obtain a RequestDispatcher object by using the getRequestDispatcher("URL") method. You
can get a RequestDispatcher object from either a request or the web context; however, the two
methods have slightly different behavior. The method takes the path to the requested resource
as an argument. A request can take a relative path (that is, one that does not begin with a /), but
the web context requires an absolute path. If the resource is not available or if the server has not
implemented a RequestDispatcher object for that type of resource, getRequestDispatcher
will return null. Your servlet should be prepared to deal with this condition.
相关文章推荐
- 在Weblogic Web Service中获取 HttpServletRequest/Response 对象
- 使用拦截器获取HttpServletResponse 对象,HttpServletRequest对象
- Servlet:Servlet与JSP九大内置对象的对应关系以及通过Servlet获取表单数据
- Action中获取Servlet的其他对象-request.response,ActionMapping,context
- springmvc 从本地线程获取HttpServletRequest , HttpServletResponse 对象
- 获取HttpServletRequest / HttpSession / ServletContext / HttpServletResponse对象 .
- Struts 获取HttpServletRequest和HttpServletResponse的两种方式
- SpringMVC——使用RequestDispatcher.include()和HttpServletResponseWrapper动态获取jsp输出内容
- 在ServletContextListener实现类中获取spring注入对象
- 把对象内容以二进制流保存到本地,以及获取
- springmvc获取HttpServletRequest对象
- ServletConfig对象 【通过此对象获取到web.xml中的信息】
- AOP实现拦截对象以及获取切入目标方法和注解
- HttpServletResponse中获取PrintWriter需要注意的一个小问题
- 获取集合、数组后要判断为空的必要性以及根据构造器创建对象后不是null的证实
- jsp内置对象out、request、response、session、application五大常用对象以及Page、pageContext、exception、config
- struts2:JSON在struts中的应用(JSP页面中将对象转换为JSON字符串提交、JSP页面中获取后台Response返回的JSON对象)
- [Servlet]HttpServletResponse的二进制输出(获取应用程序中的资源再输出)、重定向、错误发送
- .ctor,.cctor 以及 对象的构造过程(上)
- 在Action中如何得到Servlet中的一些对象如,request,session,application,response