包含一个静态或动态文件
<jsp:include>与<%@ include file="路径"%>区别是前者是动态的,页面的内容可以改变,而后者是静态的,一经载入页面,将不能修改。
请看下例:
static.html
<html>
<body>
<form method=post action="jsp_include.jsp">
<table>
<tr>
<td>
please input your name:
</td>
</tr>
<tr>
<td>
<input type=text name=name>
</td>
</tr>
<tr>
<td>
input you password:
</td>
<td>
<input type=text name=password>
</td>
</tr>
<tr>
<td>
<input type=submit value=login>
</td>
</tr>
</table>
</body>
</html>
two.jsp
<%@ page contentType="text/html; charset=gb2312" language="java"%>
举例说明include的工作原理:
<br> this is a1=<%=request.getParameter("a1")%><br>
this is a2= <%=request.getParameter("a2")%> <br>
<% out.println("hello from two.jsp");%>
jsp_include.jsp
<%@ page contentType="text/html; charset=gb2312" language="java"%>
<html>
<body>
<%@ include file="static.html"%>
<%//只是把文件包含进来%>
<a href="two.jsp">goto two--></a><br>
this examples show include works
<jsp:include page="two.jsp" flush="true">
<jsp:param name="a1" value="<%=request.getParameter("name")%>" />
<jsp:param name="a2" value="<%=request.getParameter("password")%>" />
</jsp:include>
</body>
</html>
从浏览器里输入:
http://localhost:818/JspDemo/jsp_include.jsp
看到结果为:
下面介绍JavaBean的使用操作:
PersonBean.java
package com.bean;
public class PersonBean {
private String name;
private String password;
public void setName(String name) {
this.name = name;
}
public void setPass(String pass) {
this.password = pass;
}
public String getName() {
return this.name;
}
public String getPass() {
return this.password;
}
}
login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ page import="com.bean.*" errorPage="error.jsp"%>
<%
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>My JSP 'login.jsp' starting page</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>
<form action="bean.jsp">
name:
<input type="text" name="name">
<br>
pass:
<input type="password" name="password">
<br>
<input type="submit" value="login">
</form>
</body>
</html>
从浏览器里输入:http://localhost:818/JspDemo/login.jsp
结果为:
当点击login按钮,会跳到bean.jsp
bean.jsp
<jsp:useBean id="myPerson" scope="request" class="com.bean.PersonBean">
<jsp:setProperty name="myPerson" property="name" />
<jsp:setProperty name="myPerson" property="pass" param="password" />
</jsp:useBean>
name:
<jsp:getProperty name="myPerson" property="name" />
pass:
<jsp:getProperty name="myPerson" property="pass" />
运行的结果为:
注意:
JavaBean的属性存取是通过setXXX(),getXXX()方法来执行,而不是根据属性的名字来存取的。
JavaBean的scope="page | request | session | application"
page在包含“<jsp:useBean>”元素的JSP文件以及该文件中所有静态包含文件中使用这个Bean,直到页面执行完毕向客户端发回响应或转到另一个文件为止。
request 在任何执行相同请求的Jsp文件中使用这个Bean,直到页面执行完毕向客户端发回响应或转到另一个文件为止。能够使用Request对象访问这个Bean,比如request.getAttribute(beanInstanceName)。
session - 从创建Bean开始,就能在任何使用相同session的jsp文件中使用这个Bean。这个Bean存在于整个Session生存周期内,任何在分享此Session的Jsp文件都能使用同一Bean。注意在创建这个Bean的Jsp文件中“<% @ page %>”指令中必须指定“session="true"”。
application - 从创建Bean开始,就能在任何使用相同application的Jsp文件中使用Bean。这个Bean存在于整个application生存周期内,任何在分享此application的Jsp文件都能使用同一Bean.