Servlet中的请求转发和重定向

跳转和重定向

有的时候客户端请求到达服务端后需要对请求重新转发到其它Servlet甚至别的服务器,这就需要跳转重定向

区别

一般来说,跳转是服务器内部跳转,例如将请求从一个Servlet转发给另外一个Servlet处理,是内部之间的转发。而重定向是告诉客户端去别的地方请求资源。举个例子,某人需要去找人事部门打印收入证明,而收入证明需要去财务部盖章。跳转的处理方式是:人事部门的人告诉你,你等一下,我去把收入证明打印出来,然后我再去找财务部盖章,所有资料准备好后,我再全部交给你。重定向的处理方式是:人事部门把收入流水给你打印出来,然后告诉你说,他们的工作已经做完了,现在需要你自己去找财务部盖章。

这就是跳转和重定向的本质区别,服务器跳转的时候,客户端地址栏不改变,而客户端重定向的时候,地址栏改变。

跳转示例

跳转需要用到RequestDispacher对象,该对象可以从HttpRequest或者ServletContext中获取。

下面是从HttpRequest对象中调用getRequestDispacher()方法获取RequestDispacher并跳转示例代码:

protected void doPost(HttpServletRequest req, HttpServletResponse resp)

       RequestDispatcher rd = null;

       String region = req.getParameter("region");

       System.out.println("region: " + region);

       switch (region)

       {

              case "asia":

              {

                     System.out.println("asia~~~");

                     rd = req.getRequestDispatcher("/asiaHandler.jsp");

                     break;

              }

              case "emea":

              {

                     System.out.println("emea~~~");

                     rd = req.getRequestDispatcher("/emeaHandler.jsp");

                     break;

              }

              case "ams":

              {

                     System.out.println("ams~~~");

                     rd = req.getRequestDispatcher("/amsHandler.jsp");

                     break;

              }

              default:

              {

                     System.out.println("default~~~");

                     rd = req.getRequestDispatcher("/html/error.html");

                     break;

              }

       }

      

       try

       {

              rd.forward(req, resp);

       } catch (ServletException | IOException e)

       {

              // TODO Auto-generated catch block

              e.printStackTrace();

       }

      

      

}

 

下面是从ServletContext对象中调用getNamedDispacher()方法获取RequestDispacher对象并跳转示例代码:

protected void doPost(HttpServletRequest req, HttpServletResponse resp)

       RequestDispatcher rd = null;

       ServletContext ctx = this.getServletContext();

       String region = req.getParameter("region");

       System.out.println("region: " + region);

       switch (region)

       {

              case "asia":

              {

                     rd = ctx.getNamedDispatcher("AsiaHandler");

                     break;

              }

              case "emea":

              {

                     rd = ctx.getNamedDispatcher("EmeaHandler");

                     break;

              }

              case "ams":

              {

                     rd = ctx.getNamedDispatcher("AMSHandler");

                     break;

              }

       }

      

       try

       {

              rd.forward(req, resp);

       } catch (ServletException | IOException e)

       {

              // TODO Auto-generated catch block

              e.printStackTrace();

       }

}

 

下面演示了用ServletContext对象调用getRequestDispacher()方法获取RequestDispacher对象,然后实现跳转:

protected void doPost(HttpServletRequest req, HttpServletResponse resp)

       RequestDispatcher rd = null;

       ServletContext ctx = this.getServletContext();

       String region = req.getParameter("region");

       System.out.println("region: " + region);

       switch (region)

       {

              case "asia":

              {

                     rd = req.getRequestDispatcher("/asiaHandler.jsp");

                     break;

              }

              case "emea":

              {

                     rd = req.getRequestDispatcher("/emeaHandler.jsp");

                     break;

              }

              case "ams":

              {

                     rd = req.getRequestDispatcher("/amsHandler.jsp");

                     break;

              }

 

       }

      

       try

       {

              rd.forward(req, resp);

       } catch (ServletException | IOException e)

       {

              // TODO Auto-generated catch block

              e.printStackTrace();

       }

}

 

请求重定向是服务器发给浏览器一个新的地址,让浏览器去请求新的地址。重定向需要用到HttpResponse对象调用sendRedirect()方法。

重定向示例:

protected void doPost(HttpServletRequest req, HttpServletResponse resp)

{

       try

       {

              resp.sendRedirect("http://www.bing.com");

       } catch (IOException e)

       {

              // TODO Auto-generated catch block

              e.printStackTrace();

       }

}

 

posted on 2016-09-10 11:24  kuillldan  阅读(3346)  评论(0编辑  收藏  举报