java servlet 几种页面跳转的方法
来源:互联网 发布:最短路问题的算法例题 编辑:程序博客网 时间:2024/05/21 09:32
Servlet:
当然,在servlet中,一般跳转都发生在doGet, doPost等方法里面。
1) redirect 方式
response.sendRedirect("/a.jsp");
页面的路径是相对路径。sendRedirect可以将页面跳转到任何页面,不一定局限于本web应用中,如:
response.sendRedirect("http://www.jb51.net");
跳转后浏览器地址栏变化。
这种方式要传值出去的话,只能在url中带parameter或者放在session中,无法使用request.setAttribute来传递。
2) forward方式
RequestDispatcher dispatcher = request.getRequestDispatcher("/a.jsp");
dispatcher .forward(request, response);
页面的路径是相对路径。forward方式只能跳转到本web应用中的页面上。
跳转后浏览器地址栏不会变化。
使用这种方式跳转,传值可以使用三种方法:url中带parameter,session,request.setAttribute
JSP:
1) response.sendRedirect();
和servlet的response.sendRedirect()方式一样。
此语句前不允许有out.flush(),如果有,会有异常:
java.lang.IllegalStateException: Can't sendRedirect() after data has committed to the client.
at com.caucho.server.connection.AbstractHttpResponse.sendRedirect(AbstractHttpResponse.java:558)
...
跳转后浏览器地址栏变化
如果要跳到不同主机下,跳转后,此语句后面的语句会继续执行,如同新开了线程,但是对response的操作已经无意义了;
如果要跳到相同主机下,此语句后面的语句执行完成后才会跳转;
2) response.setHeader("Location","");
此语句前不允许有out.flush(),如果有,页面不会跳转。
跳转后浏览器地址栏变化
此语句后面的语句执行完成后才会跳转
转自脚本之家:http://www.jb51.net/article/35550.htm
- java servlet 几种页面跳转的方法
- java servlet 几种页面跳转的方法
- java servlet 几种页面跳转的方法
- servlet跳转页面的几种方法
- servlet跳转页面的几种方法
- servlet跳转页面的几种方法
- servlet跳转页面的几种方法
- servlet跳转页面的几种方法
- servlet跳转页面的几种方法
- servlet跳转页面的几种方法
- servlet跳转页面的几种方法
- servlet跳转页面的几种方法
- servlet跳转页面的几种方法
- Servlet跳转到jsp页面的几种方法
- Servlet跳转到jsp页面的几种方法
- Servlet跳转到jsp页面的几种方法
- Servlet跳转页面的几种方法总结
- Servlet几种页面跳转的区别
- Win7迅雷影音播放器右键菜单的字幕选项是灰色的解决方法
- cipher工具彻底删除硬盘文件方法介绍
- docker简单操作
- MySQL的语句执行顺序
- protel四层板及内电层分割入门
- java servlet 几种页面跳转的方法
- C++多态:深入CRTP,理解编译期的多态
- VS2015---不允许 dllimport 静态数据成员的定义
- 【Python】python逆向入门
- Redis介绍以及安装(Linux)
- 打不开Anaconda Navigator:could not find or load the QT platform plugin "window in"简单解决方法
- 抓包工具之Charles
- XAMPP允许远程访问的方法
- SpringAOP处理通知中的参数