【Java.Web】JSP —— 语法基础 —— 请求转发
来源:互联网 发布:linux 更改字符集 编辑:程序博客网 时间:2024/05/20 18:45
请求转发
JSP和Servlet一样,也能进行请求转发。
JSP采用<jsp:forward>标签来实现请求转发,转发的目标组件可以为HTML文件,JSP文件或者Servlet。
<jsp:forward>的语法为:
<jsp:forward page="转发的目标组件的绝对/相对URL" />
<jsp:forward>标签中的page属性既可以为相对路径(不以“/”开头),也可以为绝对路径(以“/”开头)。
- JSP与Servlet请求转发一样,JSP源组件和目标组件共享HttpServletRequest对象和HttpServletResponse对象;JSP源组件中所有输出数据都不会被发送到客户端。
- 对于Servlet源组件调用response.sendRedirect(String location)方法进行请求转发之后的代码也会被执行。但是,JSP源组件中<jsp:forward>标签之后的代码不会被执行。
转发源组件还可以通过<jsp:param>标签向转发目标组件传递额外的请求参数。在目标组件中通过request.getParameter(参数名)方法来读取源JSP传过来的请求参数。
在jsp中,同样可以使用在servlet中通过RequestDispatch的forward方法进行请求的转发。
示例:
源JSP:redirect_source.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Redirect Source JSP</title></head><body> <p> This is output of redirect_source.jsp before forward. </p> <% System.out.println("This is output of redirect_source.jsp before forward."); %> <%-- Redirect the request --%> <jsp:forward page="redirect_destination.jsp"> <jsp:param name="username" value="new user"/> <jsp:param name="password" value="new password"/> </jsp:forward> <p> This is output of redirect_source.jsp after forward. </p> <% System.out.println("This is output of redirect_source.jsp after forward."); %> </body></html>
目标JSP:redirect_destination.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; utf-8"><title>Redirect Destination JSP</title></head><body> <p> This is output of redirect_destination.jsp.<br/> The username is <%= request.getParameter("username") %> <br/> The password is <%= request.getParameter("password") %> <br/> </p></body></html>
在浏览器中请求如下的URL:
http://localhost:8080/base-webapp/jsp/redirect_source.jsp
在浏览器中显示如下:
在console中的输出如下:
This is output of redirect_source.jsp before forward.
对于response的输出,只有目标组件JSP中的内容被输出。并且只有<jsp:forward>之前的代码被执行,而后面的代码未被执行(第二个system.out.println为输出到console)。
修改1:
在源JSP中,使用RequestDispatch的forward方法来代替<jsp:forward>标签:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Redirect Source JSP</title></head><body> <p> This is output of redirect_source.jsp before forward. </p> <% System.out.println("This is output of redirect_source.jsp before forward."); %> <%-- Redirect the request --%> <%-- <jsp:forward page="redirect_destination.jsp"> <jsp:param name="username" value="new user"/> <jsp:param name="password" value="new password"/> </jsp:forward> --%> <% application.getRequestDispatcher("/jsp/redirect_destination.jsp").forward(request, response); %> <p> This is output of redirect_source.jsp after forward. </p> <% System.out.println("This is output of redirect_source.jsp after forward."); %> </body></html>
再次访问上面的URL:
浏览器显示为:
console中显示为:
This is output of redirect_source.jsp before forward.This is output of redirect_source.jsp after forward.
与在Servlet中请求转发的情形一样,forward方法后面的代码同样被执行。
0 0
- 【Java.Web】JSP —— 语法基础 —— 请求转发
- 【Java.Web】JSP —— 语法基础
- java web——JSP基础语法
- 【Java.Web】JSP —— 语法基础 —— 其他
- 【Java.Web】JSP —— 语法基础 —— 总结
- 【Java.Web】Servlet —— 请求的转发和包含
- jsp的基本语法——请求转发和重定向
- 【Java.Web】JSP —— 语法基础 —— JSP指令Directives(page include taglib)
- 【Java.Web】JSP —— 语法基础 —— JSP异常处理
- JSP——请求重定向与请求转发
- 【Java.Web】JSP —— 语法基础 —— 选择在web.xml配置访问JSP URL
- 【Java.Web】JSP —— 语法基础 —— 隐式对象implicit Objects
- 【Java.Web】JSP —— 语法基础 —— URL Paths
- 【Java.Web】JSP —— 语法基础 —— 包含include
- 【Java.Web】JSP —— 语法基础 —— PageContext抽象类
- 【Java.Web】JSP —— 语法基础 —— Action, Standard Action
- Java Web学习总结(14)——JSP基础语法
- JSP请求的转发与重定向——day10
- python urllib2
- poj2029
- Cocos2d-X 游戏中CCTextField的使用方法
- 上的风格的风格是的分公司
- 简单解析stm32F10x.h
- 【Java.Web】JSP —— 语法基础 —— 请求转发
- HDU 1302 The Snail【模拟】
- 汇编_获取数据_指令跳转
- 网络编程学习笔记(gethostbyname函数)
- Cocos2d-X 游戏编程之------游戏中精灵创建的方式
- wikioi 1540 银河英雄传说
- 1013. Battle Over Cities (25)
- 简单dfs:UVA 260 Il Gioco dell'X
- Java中的hashcode