HttpServletResponse的设置

来源:互联网 发布:手机电话变音软件 编辑:程序博客网 时间:2024/06/17 00:56

HttpServletResponse的设置

response是负责返回响应报文给浏览器的.

例如可以设置响应报文里面的状态码,响应头和数据等等内容.

1.设置状态码

 @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp)  throws ServletException, IOException { resp.sendError(404, "资源存在,就是不让你看到");}

2.设置响应头实现重定向.

package com.example.servlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class MyServlet extends HttpServlet {    @Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {         resp.setStatus(302);         resp.setHeader("Location", "/hello3/b.servlet");}}package com.example.servlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class SecondServlet extends HttpServlet {    @Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {         resp.setContentType("text/html;charset=utf-8"); resp.getWriter().print("hello,你已经完成了资源重定向.你现在访问的是b.sevlet");}}

3.利用响应头实现定时刷新,定时刷新实际上是定时重定向

package com.example.servlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class MyServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html;charset=utf-8");   resp.getWriter().print("登陆成功,5秒钟后将会自动跳转."); resp.setHeader("Refresh", "5;URL=/hello3/b.servlet");}}package com.example.servlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class SecondServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html;  charset=utf-8");   resp.getWriter().print("hello,你已经完成了定时资源重定向.你现在访问的是b.sevlet");}}

4.利用响应头实现禁用浏览器缓存

resp.setContentType("text/html;charset=utf-8"); resp.getWriter().print("现在该文档不要缓存."); resp.setHeader("Cache-Control", "no-cache"); resp.setHeader("pragma", "no-cache"); resp.setDateHeader("expires", -1);

5.如何发送响应体

response发送响应体有两种方式,分别是字节流和字符流,但是两者不可以同时使用.

 @Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp)  throws ServletException, IOException { //发送字节流响应数据,其实任何数据都可以转化为字节流发送.  //所以字节流实质上可以发送任何数据 String result="你好,世界";  resp.getOutputStream().write(result.getBytes());}

6.实现重定向的另外一个方式

package com.example.servlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class MyServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.sendRedirect("/hello3/b.servlet");}}

【本文为e安在线合作讲师“刘建恒”原创稿件,转载请联系e安在线】

0 0
原创粉丝点击