深入体验JavaWeb开发内幕之Response对象操作实例之通过Response实现重定向和刷新跳转并跳转页面

来源:互联网 发布:像拍立得滤镜软件 编辑:程序博客网 时间:2024/06/10 02:39

通过Response对象实现重定向的两种方式:

index.jsp

[java] view plain copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>My JSP 'index.jsp' starting page</title>  
  13.     <meta http-equiv="pragma" content="no-cache">  
  14.     <meta http-equiv="cache-control" content="no-cache">  
  15.     <meta http-equiv="expires" content="0">      
  16.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  17.     <meta http-equiv="description" content="This is my page">  
  18.     <!--  
  19.     <link rel="stylesheet" type="text/css" href="styles.css">  
  20.     -->  
  21.   </head>  
  22.     
  23.   <body>  
  24.     首页 <br>  
  25.   </body>  
  26. </html>  

page.jsp

[java] view plain copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>My JSP 'page.jsp' starting page</title>  
  13.       
  14.     <meta http-equiv="pragma" content="no-cache">  
  15.     <meta http-equiv="cache-control" content="no-cache">  
  16.     <meta http-equiv="expires" content="0">      
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  18.     <meta http-equiv="description" content="This is my page">  
  19.     <!--  
  20.     <link rel="stylesheet" type="text/css" href="styles.css">  
  21.     -->  
  22.   
  23.   </head>  
  24.     
  25.   <body>  
  26.   
  27.     <%=application.getAttribute("content") %>  
  28.   </body>  
  29. </html>  


[java] view plain copy
  1. redirect.java  
  2.   
  3.    
  4.   
  5. package net.csdn.servlet;  
  6.   
  7.    
  8.   
  9. import java.io.IOException;  
  10.   
  11. import java.io.PrintWriter;  
  12.   
  13.    
  14.   
  15. importjavax.servlet.ServletException;  
  16.   
  17. importjavax.servlet.http.HttpServlet;  
  18.   
  19. importjavax.servlet.http.HttpServletRequest;  
  20.   
  21. importjavax.servlet.http.HttpServletResponse;  
  22.   
  23.    
  24.   
  25. public class redirect extends HttpServlet {  
  26.   
  27.    
  28.   
  29.        publicvoid doGet(HttpServletRequest request, HttpServletResponse response)  
  30.   
  31.                      throwsServletException, IOException {  
  32.   
  33.               redirect1(response);  
  34.                direct2(response);  
  35.   
  36.        }  
  37.   publicvoid doPost(HttpServletRequest request, HttpServletResponse response)<p>                     throwsServletException, IOException {</p><p>              doGet(request,response);</p><p>       }</p><p> </p><p>}</p>  

方式一:

 

[java] view plain copy
  1. privatevoid redirect1(HttpServletResponse response) throws IOException {  
  2.   
  3.              PrintWriterout = response.getWriter();  
  4.   
  5.              out.println("servlet");  
  6.   
  7.              response.setStatus(302);  
  8.   
  9.              response.setHeader("location","http://www.baidu.com");  
  10.   
  11.       }  
效果如图:


 方式二:


    

[java] view plain copy
  1. privatevoid direct2(HttpServletResponse response) throws IOException {  
  2.   
  3.             response.getWriter().write("登陆!");  
  4.   
  5.             response.sendRedirect("http://www.baidu.com");  
  6.   
  7.      }  
效果亦如图:

      

 

通过Response对象实现刷新并跳转页面的几种方式:

[java] view plain copy
  1. RefreshServlet .java  
  2.   
  3. package net.csdn.servlet;  
  4.   
  5. import java.io.IOException;  
  6. import java.io.PrintWriter;  
  7. import java.util.Date;  
  8.   
  9. import javax.servlet.ServletException;  
  10. import javax.servlet.http.HttpServlet;  
  11. import javax.servlet.http.HttpServletRequest;  
  12. import javax.servlet.http.HttpServletResponse;  
  13.   
  14. public class RefreshServlet extends HttpServlet {  
  15.   
  16.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  17.             throws ServletException, IOException {  
  18.           
  19.      refreshRequestForword(request, response);  
  20.         refreshToPage(response);  
  21.        refreshOneSecond(response);  
  22.         refreshOneSecondToPage(response);  
  23.     }  
  24. <strong> public void doPost(HttpServletRequest request, HttpServletResponse response)  
  25.             throws ServletException, IOException {  
  26.         doGet(request, response);  
  27.     }  
  28.   
  29. }</strong>  
  30.    

效果一:


 

[java] view plain copy
  1. private void refreshRequestForword(HttpServletRequest request,  
  2.          HttpServletResponse response) throws ServletException, IOException {  
  3.      String content = "<meta http-equiv='refresh' content='3;url = http://www.sina.com'>登陆成功,此页面3秒钟后将自动跳转,如果无法正常跳转,请点击<a href ='/WebProject/page.jsp'>超链接</a>!";  
  4.      this.getServletContext().setAttribute("content", content);  
  5.      this.getServletContext().getRequestDispatcher("/page.jsp").forward(request, response);  
  6.  }  



效果二:

[java] view plain copy
  1. private void refreshOneSecondToPage(HttpServletResponse response)  
  2.          throws IOException {  
  3.      response.getWriter().write(new Date().toLocaleString());  
  4.      response.setHeader("refresh""1;url = http://www.baidu.com");  
  5.  }  



   效果三:


[java] view plain copy
  1. private void refreshOneSecond(HttpServletResponse response)  
  2.            throws IOException {  
  3.        response.getWriter().write(new Date().toLocaleString());  
  4.        response.setHeader("refresh""1");  
  5.    }  




  效果四:

[java] view plain copy
  1. private void refreshToPage(HttpServletResponse response) throws IOException {  
  2.       response.setCharacterEncoding("UTF-8");  
  3.       response.setContentType("text/html;charset = UTF-8");  
  4.       response.setHeader("refresh","3;url = /WebProject/index.jsp");//3;url = http://www.sina.com  
  5.       response.getWriter().write("登陆成功,此页面3秒后跳转,如果跳转失败,请点击<a href='aaa'>超链接</a>!");  
  6.   
  7.   }  



有时候我们经常看到当你购买了某件商品之后显示几秒之后跳转到某个页面,当然大多数情况下我们都是采用框架去实现这杨的一些动态的效果,但是如果我们初级的步入javaEE学习的人,想实现这样的效果,要如何去是实现呢,这里有一点小技巧

private void buy(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {         String bookId = request.getParameter("bookId");         //得到一本书         Book book = s.findBookById(bookId);         HttpSession session = request.getSession();         //一种设计模式           //在购物车设计的时候要注意几个问题           //1:对于第一次设计购物车的时候,肯定是没有购物车的,但是又不能每一次请求都要建立一个购物车           //所以只是在第一次购买书籍的时候给他建立一个购物车,以后就只是网购物车里边添加数据就可以了         Cart cart = (Cart)session.getAttribute(Constant.httpsession_cart);         if(cart==null)         {              cart = new Cart();              session.setAttribute(Constant.httpsession_cart, cart);         }         //将书籍添加到购物车里边去         cart.addBook(book);//         //这个就是一个利用response对象进行页面跳转的实例,这个地方就是我们在设计购物车小案例的时候需要经常使用的         //转向一个购买成功的页面          response.getWriter().write("<font color='green' size='25'>购买成功 5秒之后发生跳转到主页....</font>");          response.setHeader("Refresh", "5;URL="+request.getContextPath());    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

最共实现的效果如图所示:

这里写图片描述

这里呢,图片和书籍有点不搭调,但是就这么一张图片,所以大家不要见笑

这里写图片描述

这里写图片描述


阅读全文
1 0
原创粉丝点击