在jsp中,我们可以通过两种方式包含页面,简单点的方法是包含一个或者几个静态页面,稍微高级一点的方式是包含动态页面,甚至像动态页面传递参数,下面我们针对这两种方式分别进行介绍。
包含静态页面
这里所谓的静态页面其实有点不太合适,我们也可以包含jsp页面,但是无法传递参数。包含静态页面通过<%@include file="" %>
指令进行的。
需要包含的页面index.jsp
<%@include file="include.jsp" %>
被包含的页面include.jsp
<h1>Include Page</h1>
<h2><%=request.getParameter("included") %></h2>
我们可以看一下下面的输出页面,由于无法传递参数,所以我们在页面获取的request的参数值是null
包含动态页面并传递参数
继续我们刚才的代码,我们修改index.jsp代码如下
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" session="true" isThreadSafe="true" autoFlush="true" info="我是首页" errorPage="errorPage.jsp"%>
<!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>首页</title>
</head>
<body>
<form action="index.jsp" name="form1" method="post">
<input type="text" name="userName" id="UserName">
<input type="submit" value="提交">
<%
request.setCharacterEncoding("UTF-8");
String userName = request.getParameter("userName");
if(userName==null){
out.println("请输入用户名");
}else if(userName==""){
out.println("(づ ̄3 ̄)づ╭❤~,输入名称呀");
}
else{
out.println(userName);
}
%>
<%-- 注释
<%@include file="include.jsp" %>
--%>
<jsp:include page="include.jsp">
<jsp:param value="我被成功包含进来了" name="included"/>
</jsp:include>
</form>
</body>
</html>
ganxfenx感谢分享
thank you,谢谢