首页 > 代码库 > jsp - forward指令
jsp - forward指令
forward指令 既可以指向静态的html页面,也可以转发到动态的jsp页面,并可以保留先前请求的参数.
例如,在web中新建一个Jsp_src.jsp的jsp页面:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="http://www.mamicode.com/"> <title>原始页面</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="http://www.mamicode.com/styles.css"> --> </head> <body> 原始页面. <br> <jsp:forward page="jsp_target.jsp"></jsp:forward> </body></html>
然后让页面Jsp_src.jsp转到jsp_target.jsp页面,jsp_target.jsp页面的代码为:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="http://www.mamicode.com/"> <title>jsp目标页面</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="http://www.mamicode.com/styles.css"> --> </head> <body> 目标页面主体. <br> <jsp:forward page="jsp_target2.jsp"></jsp:forward> </body></html>
然后,再次在jsp_target.jsp页面中转到新的jsp页面jsp_target2.jsp,代码为:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="http://www.mamicode.com/"> <title>jsp目标页面2</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="http://www.mamicode.com/styles.css"> --> </head> <body> 目标页面主体2. <br> req-char:<%=request.getCharacterEncoding() %> name=<%=request.getParameter("name") %> </body></html>
往浏览器中输入请求url: http://localhost:8080/ServletJSP/forward/Jsp_src.jsp?name=hi
可以看到,浏览器中请求地址依然不变,但内容也变为jsp_target2.jsp的页面的内容,而且,请求的参数name仍然可以保留.
jsp - forward指令
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。