JavaWeb初识 Response(例题) 跳转(重定向Response,转发Request) |
您所在的位置:网站首页 › response重定向使用的方法是 › JavaWeb初识 Response(例题) 跳转(重定向Response,转发Request) |
服务器的请求无非就是一去一回:请求头、响应头(生命周期很短,一去一回就结束了)先由客户端请求到服务器 再由服务发送请求到客户端响应会发出相应的提示例如404 405等等(网上一找一大堆)搜索http响应代码 响应类型:如何制作点击就出现下载那?网页相应代码不一定是下载内容,照片或者其他这个是不一定的。 之所以用Servlet 是因为可以在服务器中写逻辑 怎样书写 响应下载格式首先要去找你要上传的相对应格式1.打开你的Tomcat 找到conf文件夹子2.找到web.xml文件 查找你所相对的文件后缀例如rar文件 方法说明代码 import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.OutputStream; import java.io.PrintWriter; @WebServlet("/XiXi") public class XiXi extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //说明了类型客户端就会根据你的类型来呈现,如果是不认识的内容的话默认就是下载 resp.setContentType("text/html;"); //这个方法告诉浏览器响应的是什么类型 // text/html;charset=UTF-8 响应类型text文本格式,按照html输出,字符集是UTF8 PrintWriter printWriter = resp.getWriter(); //向外输出文本 printWriter.println("嘻嘻哈哈"); printWriter.close(); OutputStream outputStream = resp.getOutputStream(); //二进制的 字节无损输出,下载东西无损 } }运用代码: import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.*; @WebServlet("/hell2") public class Hello2 extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String psw = req.getParameter("psw"); int sw = 0; if (psw.equals("123456")) { sw = 0; } else if (psw.equals("123")) { sw = 200; } else { sw = 300; } if (sw >=0) { OutputStream outputStream = resp.getOutputStream(); resp.setContentType("application/x-rar-compressed"); resp.setContentLengthLong(new File("c:/1.rar").length()); //响应头下载长度setContentLengthLong resp.setHeader("Content-Disposition", "attachment;filename=1.rar");// //响应头下载名称,好处就是能说明格式下载完成后他能识别后缀进行显示 //响应头响应内容命名 OutputStreamUtil.ouput("c:/1.rar", resp.getOutputStream(), sw); } else { resp.setContentType("text/html;charset=UTF-8"); PrintWriter printWriter1 = resp.getWriter(); printWriter1.print("不好意思你没有权限下载"); printWriter1.close(); } } }跳转:重定向:加入我们有两个Servlet实例对象A,B 假如浏览器访问A、A说要跳转到B 他就会给浏览器说准备重定向,浏览器接受请求后再由浏览器跳转到B实例对象.重定向是由浏览器进行跳转的(好处就是可以跳转别人的网站) public class B extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // resp.sendRedirect("A"); //resp.sendRedirect("URL");可以写本站地址,也可以写外站地址,外站地址需要加Http //重定向不是立即跳转的,只有响应信息到了浏览器,才会进行跳转 //这句代码无非就是设置了跳转信息配置,响应到了浏览器他才会进行跳转 try { Thread.sleep(5000); resp.sendRedirect("https://www.baidu.com");//定向到被人的Url //注意不要设置成死循环了A跳B、B在跳A\ //只能设置一次设置多了就会报错 } catch (InterruptedException e) { e.printStackTrace(); } } }转发 就是不需要浏览器配合 例如浏览器访问A,A中有去B的转发那访问的就是B,但是由于是每部执行所以浏览器不知道还以为你访问的是A 好处(唯一通道,只有转发可以转发到WEN-INF中去)地址没有变我们可以吧Jsp 或者其他处理页面文件放存放到 WEB-INF中转发可以转发WEB-INF中的文件转发操作是立即跳转 public class aa extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 转发是Request对象进行操作的 RequestDispatcher requestDispatcher = req.getRequestDispatcher("bb"); requestDispatcher.forward(req, resp); //这句代码的意识是执行到这里立即跳到下一个页面去 } } |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |