码迷,mamicode.com
首页 > Web开发 > 详细

jsp - forward指令

时间:2015-02-03 19:15:34      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:

  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="<%=basePath%>">
    
    <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="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="<%=basePath%>">
    
    <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="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="<%=basePath%>">
    
    <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="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指令

标签:

原文地址:http://www.cnblogs.com/listened/p/4270485.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!