jsp页面跳转方式

本文详细介绍了四种常用的网页跳转方法:response.setRedirect(), RequestDispatcher.forward(), jsp:forward指令及通过修改HTTP header的Location属性实现重定向,并对它们的特点进行了对比分析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.response.setRedirect()

response.sendRedirect("/index.jsp");

二. RequestDispatcher.forward()

 public void doPost(HttpServletRequest request,HttpServletResponse response)
                                throws ServletException,IOException{
  response.setContentType("text/html; charset=gb2312");
  ServletContext sc = getServletContext();
  RequestDispatcher rd = null;
  rd = sc.getRequestDispatcher("/index.jsp"); //定向的页面
  rd.forward(request, response);
  }

三.<jsp:forward page=”” />
它的底层部分是由RequestDispatcher来实现的,因此它带有RequestDispatcher.forward()方法的印记。
  如果在之前有很多输出,前面的输出已使缓冲区满,将自动输出到客户端,那么该语句将不起作用,这一点应该特别注意。
  注意:它不能改变浏览器地址,刷新的话会导致重复提交

四.修改HTTP header的Location属性来重定向

<%
  response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
  String newLocn = "/newpath/jsa.jsp";
  response.setHeader("Location",newLocn);
  %>

五.标签跳转

<meta http-equiv="refresh" content="300; url=target.jsp"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值