Skip to content

Latest commit

 

History

History
56 lines (43 loc) · 1.98 KB

8.md

File metadata and controls

56 lines (43 loc) · 1.98 KB

JSP 带参数的include指令

原文: https://beginnersbook.com/2013/12/jsp-include-directive-with-parameters-example/

在上一个教程中,我们讨论了 JSP include参数的动作。这里我们将看到在使用 JSP include指令时如何传递参数。

在这个例子中,我们将三个字符串参数传递给包含的 JSP 页面。

index.jsp

<html>
<head>
<title>Passing Parameters to Include directive</title>
</head>
<body>
<%@ include file="file1.jsp" %>
<%!
String country="India"; 
String state="UP";
String city="Agra";
%>
<% 
session.setAttribute("co", country);
session.setAttribute("st", state);
session.setAttribute("ci", city);
%>
</body>
</html>

上面,我使用声明标签初始化字符串, scriptlet 用于在session对象中设置它们。由于 sciptlet 的使用被忽视了长背,或者你可以使用<c:set> JSTL 标签做同样的事 - 代码就像这样:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:set var="co" value="India" scope="session"/>
<c:set var="st" value="UP" scope="session"/>
<c:set var="ci" value="Agra" scope="session"/>
<%@ include file="file1.jsp" %>

file1.jsp

<%=session.getAttribute("co") %>
<%=session.getAttribute("st") %>
<%=session.getAttribute("ci") %>

输出:

include-directive-param

在上面的例子中,我使用session隐式对象传递了参数,但是您也可以使用请求,页面和application隐式对象传递它们。