jsp包含页面的方法

Laughing
2017-11-13 / 0 评论 / 1,203 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2024年03月21日,已超过339天没有更新,若内容或图片失效,请留言反馈。

在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>  
1

评论 (0)

取消
  1. 头像
    along
    Windows 10 · Google Chrome

    ganxfenx感谢分享

    回复
  2. 头像
    多大的人
    MacOS · Safari

    thank you,谢谢

    回复