### jsp传统语法 ###
#### Declaration(声明) ####
- 基本语法
- <%! %>
- <%= xxx %> 把xxx内容输出出来
- 说明:在此声明的变量、方法都会被保留成唯一的一份,直到jsp程序停止执行
例如:
````
<%!
int i;//全局变量,jsp初始化的时候就初始化了,只初始化一次
public void setName(){ ......}
%>
<%
int i;//局部变量,每次访问都会重新声明。
%>
````
#### Scrptlet ####
- 基本语法
- <%程序代码区%>
- 可以放入任何的java程序代码
例如:
````
<%
for(int i = 0;i<10;i++){}
%>
````
#### Ecpression ####
- 基本语法:<%=... ...%>
- =后面必须是字符串变量或者可以被转换成字符串的表达式
- 不需要以;结束
- 只有一行
例如:
````
<%="hello world"%>
<%=i+1%>
<%=request.getParameter("name")%>
````
#### Comment(注释) ####
- 注释格式
- <%--.. ..--%>
- <%//... ...%>
- <%/ *.. ..*/%>
#### Directives(编译指令) ####
- 相当于在编译期间的命令
- 格式
- <%@Drective 属性="属性值"%>
- 常见的Drective:
- page
- include
- taglib
##### page #####
指明与JSP container的沟通方式
基本格式
````
<%@page language="script language"| --只能写java
extends="className"| --从谁继承,一般不指定
import="importList"| --引入进来那些类
buffer="none|kb size"| --none:不缓冲,默认8kb
session="true|false"| --是否可以使用session。默认true
autoFlush="true|false"| --缓冲器是否自动清除。默认true
isThreadSafe="true|false"|
info="infoText"| --任何文件。很少用
errorPage="errorPageUrl"| --出错之后显示的页面
isErrorPage="true|false"
contentType="contentTypeInfo"|
%>
````
-isErrorPage
````
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page isErrorPage="true"%>
<html>
<body TEXT="red">
错误信息:<%= exception.getMessage()%>//获取错误信息
</body>
</html>
````
````
<%page errorPage="ErrPage.jsp"%>
<%
String s = "13ads"
int i = Integer.parseInt(s);
out.println("s = " + s + "i = " + i);
%>
````
##### include #####
- 将指定的jsp程序或者html文件包含进来
- 格式:<%include file="fileURL"%>
- jsp engine会在jsp程序的转换时期,先把file属性设定的文件包含进来,然后开始执行转换及编译的工作。
- 限制:不能向fileURL中传参数。比如:abc.jsp?user=fadsf
````
<%@page contentType="text/html;charset=gb2312"%>
<html>
<head>
<title>testBar.jsp</title>
</head>
<body>
<table>
<tr><td><%@ include file="titlebar.jsp"%></td></tr>
<tr><td><% out.println("<p>这是用户显示区</p>")%></td></tr>
</table>
</body>
</html>
````
#### Action动作指令 ####
- Action(动作指令)在运行期间的命令
- 常见的有
- jsp:useBean
- jsp:setProperty
- jsp:getProperty
- jsp:include
- jsp:forward
- jsp:param
- jsp:plugin //往网页中嵌入一段
##### jsp:include/jsp:param #####
- 用于动态包含jsp程序或html文件等
- 除非这个指令会被执行到,否则不会被tomcat等jsp engine编译
- 格式:
- <jsp:include page="URLSpec" flush="true" />
- <jsp:include page="URLSpec" flush="true"><jsp:param name="ParamName" value="paramValue" /></jsp:include>
- jsp:param用来设定include文件时的参数和对应的值
- 例如
例1
````
<html>
<head>
<title>include test</title>
</head>
<body bgcolor="white">
<font color="red">
the current date and time are
<%@ include file="date.jsp"%>
<jsp:include page="date.jsp" flush="true">
</font>
</body>
</html>
````
date.jsp
````
<%@ page import="java.util.*" %>
<%@= (new java.util.Date()).toLocaleString() %>//转换成本地字符串
````
例2
compute.html
````
<html>
<head>
<title>divide</title>
<meta http=equiv="Content-Type" content="text/html;charset=gb2312">
</head>
<body bgcolor="white">
<div>
<form method="post" action="compute.jsp">
<p>
选择要做的运算
<input type="radio" name="compute" value="division" checked>
除法
<input type="radio" name="compute" value="multiplication">
乘法</p>
<p>
被除数(被乘数)
<input type="text" name="value1">
除数(乘数)
<input type="text" name="value2">
</p>
<p>
<input type="submit" name="submit" value="计算结果">
</p>
</form>
</div>
</body>
</html>
````
compute.jsp
#### Declaration(声明) ####
- 基本语法
- <%! %>
- <%= xxx %> 把xxx内容输出出来
- 说明:在此声明的变量、方法都会被保留成唯一的一份,直到jsp程序停止执行
例如:
````
<%!
int i;//全局变量,jsp初始化的时候就初始化了,只初始化一次
public void setName(){ ......}
%>
<%
int i;//局部变量,每次访问都会重新声明。
%>
````
#### Scrptlet ####
- 基本语法
- <%程序代码区%>
- 可以放入任何的java程序代码
例如:
````
<%
for(int i = 0;i<10;i++){}
%>
````
#### Ecpression ####
- 基本语法:<%=... ...%>
- =后面必须是字符串变量或者可以被转换成字符串的表达式
- 不需要以;结束
- 只有一行
例如:
````
<%="hello world"%>
<%=i+1%>
<%=request.getParameter("name")%>
````
#### Comment(注释) ####
- 注释格式
- <%--.. ..--%>
- <%//... ...%>
- <%/ *.. ..*/%>
#### Directives(编译指令) ####
- 相当于在编译期间的命令
- 格式
- <%@Drective 属性="属性值"%>
- 常见的Drective:
- page
- include
- taglib
##### page #####
指明与JSP container的沟通方式
基本格式
````
<%@page language="script language"| --只能写java
extends="className"| --从谁继承,一般不指定
import="importList"| --引入进来那些类
buffer="none|kb size"| --none:不缓冲,默认8kb
session="true|false"| --是否可以使用session。默认true
autoFlush="true|false"| --缓冲器是否自动清除。默认true
isThreadSafe="true|false"|
info="infoText"| --任何文件。很少用
errorPage="errorPageUrl"| --出错之后显示的页面
isErrorPage="true|false"
contentType="contentTypeInfo"|
%>
````
-isErrorPage
````
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page isErrorPage="true"%>
<html>
<body TEXT="red">
错误信息:<%= exception.getMessage()%>//获取错误信息
</body>
</html>
````
````
<%page errorPage="ErrPage.jsp"%>
<%
String s = "13ads"
int i = Integer.parseInt(s);
out.println("s = " + s + "i = " + i);
%>
````
##### include #####
- 将指定的jsp程序或者html文件包含进来
- 格式:<%include file="fileURL"%>
- jsp engine会在jsp程序的转换时期,先把file属性设定的文件包含进来,然后开始执行转换及编译的工作。
- 限制:不能向fileURL中传参数。比如:abc.jsp?user=fadsf
````
<%@page contentType="text/html;charset=gb2312"%>
<html>
<head>
<title>testBar.jsp</title>
</head>
<body>
<table>
<tr><td><%@ include file="titlebar.jsp"%></td></tr>
<tr><td><% out.println("<p>这是用户显示区</p>")%></td></tr>
</table>
</body>
</html>
````
#### Action动作指令 ####
- Action(动作指令)在运行期间的命令
- 常见的有
- jsp:useBean
- jsp:setProperty
- jsp:getProperty
- jsp:include
- jsp:forward
- jsp:param
- jsp:plugin //往网页中嵌入一段
##### jsp:include/jsp:param #####
- 用于动态包含jsp程序或html文件等
- 除非这个指令会被执行到,否则不会被tomcat等jsp engine编译
- 格式:
- <jsp:include page="URLSpec" flush="true" />
- <jsp:include page="URLSpec" flush="true"><jsp:param name="ParamName" value="paramValue" /></jsp:include>
- jsp:param用来设定include文件时的参数和对应的值
- 例如
例1
````
<html>
<head>
<title>include test</title>
</head>
<body bgcolor="white">
<font color="red">
the current date and time are
<%@ include file="date.jsp"%>
<jsp:include page="date.jsp" flush="true">
</font>
</body>
</html>
````
date.jsp
````
<%@ page import="java.util.*" %>
<%@= (new java.util.Date()).toLocaleString() %>//转换成本地字符串
````
例2
compute.html
````
<html>
<head>
<title>divide</title>
<meta http=equiv="Content-Type" content="text/html;charset=gb2312">
</head>
<body bgcolor="white">
<div>
<form method="post" action="compute.jsp">
<p>
选择要做的运算
<input type="radio" name="compute" value="division" checked>
除法
<input type="radio" name="compute" value="multiplication">
乘法</p>
<p>
被除数(被乘数)
<input type="text" name="value1">
除数(乘数)
<input type="text" name="value2">
</p>
<p>
<input type="submit" name="submit" value="计算结果">
</p>
</form>
</div>
</body>
</html>
````
compute.jsp