jsp和servlet中的页面跳转示例摘要

本文的示例总结了在jsp和servlet中实现页面跳转的方法:

假设你想跳到test2.jsp test1.jsp

1。jsp中的跳转:

1。使用requestdispatcher.forward转发的方法


<
RequestDispatcher RD = getServletContext()。GetRequestDispatcher( /测试/ test2。JSP );
rd.forward(请求、响应);
%>




2。response.sendredirect重定向




<
response.sendredirect(test2。JSP );
%>



三.使用前向标记

复制代码代码如下所示:

4中的元标记。HTML标记

复制代码代码如下所示:

5。使用response.setheader


<
int滞留= 0;
字符串url =test2。JSP ;
字符串内容=滞留+;url =+网址;
response.setheader(刷新
%>




6。使用response.setheader和response.setstatus发送重定向请求


<
response.setstatus(httpservletresponse。sc_moved_permanently);
串新=test2。JSP ;
response.setheader(位置
%>




7。使用javascript脚本



窗口的位置。href =test2。JSP ;





两。跳转中的servlet:

假设你从servlet test2.jsp

1。向前地


ServletContext SC = getServletContext();
RequestDispatcher RD = sc.getrequestdispatcher( /测试/ test2。JSP ) / /定向页;
rd.forward(请求、响应);
公共课forwardservlet扩展HttpServlet {
public void doGet(HttpServletRequest请求,HttpServletResponse响应)
抛出IOException {返回,
字符串ID = request.getparameter(ID);
response.setcontenttype(文本/ HTML;字符集GB2312);
ServletContext SC = getServletContext();
RequestDispatcher RD = sc.getrequestdispatcher( /测试/ test2。JSP ) / /定向页;
rd.forward(请求、响应);
}
public void doPost(HttpServletRequest请求,HttpServletResponse响应)
抛出IOException {返回,
DoGet(请求、响应);
}
}




2。不调用


包com.yanek.test;
进口;
进口javax.servlet.requestdispatcher;
进口javax.servlet.servletcontext;
进口javax.servlet.servletexception;
进口javax.servlet.http.httpservlet;
进口javax.servlet.http.httpservletrequest;
进口javax.servlet.http.httpservletresponse;
公共课redirectservlet扩展HttpServlet {
public void doGet(HttpServletRequest请求,HttpServletResponse响应)
抛出IOException {返回,
字符串ID = request.getparameter(ID);
response.setcontenttype(文本/ HTML;字符集GB2312);
response.sendredirect(测试/ test2。JSP );
}
public void doPost(HttpServletRequest请求,HttpServletResponse响应)
抛出IOException {返回,
DoGet(请求、响应);
}
}




希望本文能对jsp程序设计有所帮助。