其他分享
首页 > 其他分享> > ServletResponse

ServletResponse

作者:互联网

13.ServletResponse

13.1.介绍
13.2.常用API
@WebServlet("/response1")
public class ResponseServlet1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置响应状态码
        response.setStatus(404);
        //设置响应头
        response.setHeader("Content-Type", "text/html");
        response.setHeader("Server", "Tomcat 8.5");
        //设置响应体
        response.getWriter().println("<h1 style='color:red'>File Not Found</h1>");
    }
}
13.3.定时刷新
@WebServlet("/refresh")
public class RefreshServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //refresh用法一共有两种
        //用法一:值只有一个数字,表示每隔多少秒刷新当前页面,实现页面时时刻刻刷新当前的时间
        //转成YYYY-MM-dd HH:mm:ss
        String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        response.getWriter().println(time);
        //response.setHeader("refresh", "1");
        //用法二:值里面有一个数字,并且还有一个url,表示的是经过多少秒之后跳转至url,然后结束
		//response.setHeader("refresh", "3;url=" + request.getContextPath() + "/1.html");
        response.setHeader("refresh", "3;url=http://www.cskaoyan.com");
    }
}
13.4.重定向
@WebServlet("/redirect")
public class RedirectServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//response.setStatus(302);
        
		//第一种写法
		//response.setHeader("Location", request.getContextPath() + "/1.html");
        
		//第二种写法        
		//response.setHeader("Location", "http://www.cskaoyan.com");
        
        //我们上面是根据重定向的定义,自己去写了对应的实现
        
        //服务器给我们提供了一个更为简便的API
        //第三种写法
        response.sendRedirect(request.getContextPath() + "/1.html");
    }
}
13.5.几种页面跳转方式的比较
13.6.乱码的解决
//响应体设置一个编码格式,同时将编码格式告诉给浏览器,两边统一即可
//1.可以通过响应头告知
@WebServlet("/response1")
public class ResponseServlet1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置响应状态码
        response.setStatus(404);
        //设置响应头
        //response.setHeader("Content-Type", "text/html");
        response.setHeader("Server", "Tomcat 8.5");
        //设置响应体
        //response.getWriter().println(new Date());
        //response.setCharacterEncoding("gbk");
        //因为该行代码由两层含义:1.设置响应体编码格式2.发送一个含有编码格式的响应头
        response.setHeader("Content-Type", "text/html;charset=utf-8");
        response.getWriter().println("<h1 style='color:red'>File Not Found</h1>");
        response.getWriter().println("<h1 style='color:red'>文件未找到</h1>");
    }
}


//2.发送一个含有编码格式的响应头
@WebServlet("/response1")
public class ResponseServlet1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置响应状态码
        response.setStatus(404);
        //设置响应头
        //response.setHeader("Content-Type", "text/html");
        response.setHeader("Server", "Tomcat 8.5");
        //设置响应体
        //response.getWriter().println(new Date());
        //response.setCharacterEncoding("gbk");
        //因为该行代码由两层含义:1.设置响应体编码格式2.发送一个含有编码格式的响应头
        //response.setHeader("Content-Type", "text/html;charset=utf-8");
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().println("<h1 style='color:red'>File Not Found</h1>");
        response.getWriter().println("<h1 style='color:red'>文件未找到</h1>");
    }
}


//3.通过响应体告知
//3.1.设置编码格式  3.2.告知对方我使用的编码格式
@WebServlet("/response2")
public class ResponseServlet2 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置响应状态码
        response.setStatus(404);
        //设置响应头
        response.setHeader("Server", "Tomcat 8.5");
        //设置响应体
        response.setCharacterEncoding("utf-8");

        response.getWriter().println("<!DOCTYPE html>\n" +
                "<html lang=\"en\">\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <title>Title</title>\n" +
                "</head>\n" +
                "<body>");
        response.getWriter().println("<h1 style='color:red'>File Not Found</h1>");
        response.getWriter().println("<h1 style='color:red'>文件未找到</h1>");
        response.getWriter().println("\n" +
                "</body>\n" +
                "</html>");
    }
}
13.7.输出字节数据
@WebServlet("/stream")
public class OutputStreamServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //可以设置,也可以不设置,如果不设置的话浏览器会自动帮我们设置好。
        response.setContentType("image/jpg");
        ServletOutputStream outputStream = response.getOutputStream();
        //可以把这个当作FileOutputStram来看待,但是呢,行为应该是相同的,输出的目的地不同
        //需要你将本地部署根目录下面的1.jpg 输出到客户端
            //硬盘上面的文件,读取到内存中  FileInputStream来处理这一个过程
            //部署根目录下面1.jpg的输入流拿到
        //先拿到文件的绝对路径
        String realPath = getServletContext().getRealPath("1.jpg");
        FileInputStream fileInputStream = new FileInputStream(realPath);
        //输出到response的响应体即可
        int length = 0;
        byte[] bytes = new byte[1024];
        while ((length = fileInputStream.read(bytes)) != -1){
            outputStream.write(bytes, 0, length);
        }
        fileInputStream.close();
        //输出流可以关闭,也可以不关闭,如果不关闭,那么tomcat会在响应的时候关闭他们
        outputStream.flush();
        outputStream.close();
    }
}
13.8.下载
@WebServlet("/download")
public class DownloadServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置响应头
        response.setHeader("Content-Disposition","attachment;filename=1.jpg");
        response.setContentType("image/jpg");
        ServletOutputStream outputStream = response.getOutputStream();
        
        //可以把这个当作FileOutputStram来看待,但是行为应该是相同的,输出的目的地不同
        //需要你将本地部署根目录下面的1.jpg 输出到客户端
        	//硬盘上面的文件,读取到内存种  FileInputStream来处理这一个过程
        	//部署根目录下面1.jpg的输入流拿到
        //先拿到文件的绝对路径
        String realPath = getServletContext().getRealPath("1.jpg");
        FileInputStream fileInputStream = new FileInputStream(realPath);
        //输出到response的响应体即可
        int length = 0;
        byte[] bytes = new byte[1024];
        while ((length = fileInputStream.read(bytes)) != -1){
            outputStream.write(bytes, 0, length);
        }
        fileInputStream.close();
        //输出流可以关闭,也可以不关闭,如果不关闭,那么tomcat会在响应的时候关闭
        outputStream.flush();
        outputStream.close();
    }
}
13.9.同一个Servlet响应多个请求
13.10.Response的一些细节

标签:响应,void,request,HttpServletResponse,ServletResponse,IOException,response
来源: https://blog.csdn.net/lsoxvxe/article/details/116563383