您的位置:首页 > 其它

重定向与转发 (附:压缩格式的本页显示)

2016-07-17 17:30 239 查看
重定向与转发的区别:

     


转发的代码:

     String path = "/jsps/res.jsp";
RequestDispatcher rd = req.getRequestDispatcher(path);
rd.forward(req, resp);重定向的代码:
  //重定向--法1
String path = "/helloWeb/jsps/res.jsp";
resp.sendRedirect(path);
//重定向---法2
resp.setStatus(302);//重定向
resp.setHeader("Location", path);注意:path路径的区别
   转发是servelt访问,根据项目的相对路径可以找到 或者
servelt地址映射,/在项目中指WebRoot下面

  重定向是网页请求资源,要通过servelt地址映射 或者 访问tomcat下的项目可访问文件,/指tomcat下的WebApps下面

gzip的本页发送

  <span style="color:#000000;">public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

String str="sdjkjewjw洁面湖南城市学院信息科学与工程学院南城市学院信息科学与工程学院南城市学院信息科学与工程学院南城市学院信息科学与工程学院南城市学院信息科学与工程学院kekw";
byte[] src = str.getBytes();
System.out.println("src-length:"+src.length);

//把字节数组src中的数据 压缩到 array内存流当中
ByteArrayOutputStream array = new ByteArrayOutputStream();
GZIPOutputStream gOut = new GZIPOutputStream(array);
gOut.write(src);
gOut.close();
//从内存流array中把压缩后的数据拿出来
byte[] dest = array.toByteArray();
System.out.println("dest-length:"+dest.length);

response.setContentType("text/html");
response.setHeader("Content-Encoding","gzip");//告诉浏览器,当前发送的是gzip格式的内容
//response.setContentLength(dest.length);//设内容长度---法1
response.setHeader("Content-Length", ""+dest.length);//设内容长度---法2

OutputStream out = response.getOutputStream();
//out.write(src);
out.write(dest);
out.flush();
out.close();
}</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息