发布于 2015-08-17 14:45:34 | 579 次阅读 | 评论: 0 | 来源: 网络整理
本教程将给予基本概念上简单的语法(即元素)涉及JSP开发:
其中脚本可以包含任意数量的JAVA语言的语句,变量或方法声明或表达式是有效的网页脚本语言。
以下是脚本的语法:
<% code fragment %>
可以编写XML相当于上面的语法如下:
<jsp:scriptlet>
code fragment
</jsp:scriptlet>
任何文本,HTML标记,或者JSP元素你写必须在scriptlet的外面。下面是一个简单的和第一个例子的JSP:
<html>
<head><title>Hello World</title></head>
<body>
Hello World!<br/>
<%
out.println("Your IP address is " + request.getRemoteAddr());
%>
</body>
</html>
注意:假设Apache Tomcat的安装在C:apache-tomcat-7.0.2 和 环境设置为每个教程设置环境。
让我们继续上面的代码在JSP文件hello.jsp中,并把这个文件在C:apache-tomcat-7.0.2webappsROOT目录,并尝试通过给网址http://localhost:8080/hello.jsp浏览它。这将产生以下结果:
声明一个或多个变量,或者你可以在Java代码后在JSP文件中使用的方法。在JSP文件中使用它之前,必须声明的变量或方法。
以下是JSP声明的语法:
<%! declaration; [ declaration; ]+ ... %>
您可以编写XML相当于上面的语法如下:
<jsp:declaration>
code fragment
</jsp:declaration>
下面是一个简单的例子JSP声明:
<%! int i = 0; %>
<%! int a, b, c; %>
<%! Circle a = new Circle(2.0); %>
JSP表达式元素包含被评估的脚本语言表达,转化为字符串,并在表达出现在JSP文件中插入。
因为一个表达式的值转换为String,您可以在一行文本中使用表达式,它是否被标记为HTML,在JSP文件中。
表达式的元素可以包含根据Java语言规范是任何有效的表达式,但你不能用一个分号来结束的表达式。
以下是JSP表达式的语法:
<%= expression %>
您可以编写XML相当于上面的语法如下:
<jsp:expression>
expression
</jsp:expression>
下面是一个简单的例子JSP表达式:
<html>
<head><title>A Comment Test</title></head>
<body>
<p>
Today's date: <%= (new java.util.Date()).toLocaleString()%>
</p>
</body>
</html>
这将产生以下结果:
Today's date: 11-Sep-2010 21:24:25 |
JSP注释标记的文字或说明JSP容器应忽略。当你想要隐藏或“注释掉”你的JSP页面的一部分,一个JSP注释是很有用的。
以下是JSP注释的语法:
<%-- This is JSP comment --%>
下面是一个简单的例子JSP评论:
<html>
<head><title>A Comment Test</title></head>
<body>
<h2>A Test of Comments</h2>
<%-- This comment will not be visible in the page source --%>
</body>
</html>
这将产生以下结果:
A Test of Comments |
有少数特殊的结构可以用在各种情况下插入注释,否则将被特殊处理的字符。这里有一个总结:
语法 | 目的 |
---|---|
<%-- comment --%> | A JSP comment. Ignored by the JSP engine. |
<!-- comment --> | An HTML comment. Ignored by the browser. |
<% | Represents static <% literal. |
%> | Represents static %> literal. |
' | A single quote in an attribute that uses single quotes. |
" | A double quote in an attribute that uses double quotes. |
一个JSP指令影响Servlet类的整体结构。它通常具有以下形式:
<%@ directive attribute="value" %>
有三种类型的指令标签:
指令 | 描述 |
---|---|
<%@ page ... %> | Defines page-dependent attributes, such as scripting language, error page, and buffering requirements. |
<%@ include ... %> | Includes a file during the translation phase. |
<%@ taglib ... %> | Declares a tag library, containing custom actions, used in the page |
我们将解释JSP指令在单独的一章 JSP - 指令
JSP动作使用XML语法结构来控制Servlet引擎的行为。您可以动态地插入文件,重用JavaBeans组件,用户转发到另一个页面,或生成HTML的Java插件。
只有一个用于操作元素语法,因为它符合XML标准:
<jsp:action_name attribute="value" />
动作要素基本上都是预定义的功能,而且已有下面的JSP操作:
语法 | 目的 |
---|---|
jsp:include | Includes a file at the time the page is requested |
jsp:include | Includes a file at the time the page is requested |
jsp:useBean | Finds or instantiates a JavaBean |
jsp:setProperty | Sets the property of a JavaBean |
jsp:getProperty | Inserts the property of a JavaBean into the output |
jsp:forward | Forwards the requester to a new page |
jsp:plugin | Generates browser-specific code that makes an OBJECT or EMBED tag for the Java plugin |
jsp:element | Defines XML elements dynamically. |
jsp:attribute | Defines dynamically defined XML element's attribute. |
jsp:body | Defines dynamically defined XML element's body. |
jsp:text | Use to write template text in JSP pages and documents. |
我们将解释在单独的章节JSP动作 JSP 动作
JSP支持九种自动定义的变量,这也被称为隐式对象。这些变量是:
对象 | 描述 |
---|---|
request | This is the HttpServletRequest object associated with the request. |
response | This is the HttpServletResponse object associated with the response to the client. |
out | This is the PrintWriter object used to send output to the client. |
session | This is the HttpSession object associated with the request. |
application | This is the ServletContext object associated with application context. |
config | This is the ServletConfig object associated with the page. |
pageContext | This encapsulates use of server-specific features like higher performance JspWriters. |
page | This is simply a synonym for this, and is used to call the methods defined by the translated servlet class. |
Exception | The Exception object allows the exception data to be accessed by designated JSP. |
我们将解释在单独的一章JSP隐含对象JSP隐含对象.
JSP提供了Java的所有功能被嵌入在Web应用程序。可以使用所有的API和Java的构建模块在JSP程序,包括决策语句,循环等。
if...else 块开始时像一个普通的Scriptlet,但Scriptlet是在包括scriptlet标记之间的HTML文本的每一行封闭。
<%! int day = 3; %>
<html>
<head><title>IF...ELSE Example</title></head>
<body>
<% if (day == 1 | day == 7) { %>
<p> Today is weekend</p>
<% } else { %>
<p> Today is not weekend</p>
<% } %>
</body>
</html>
这将产生以下结果:
Today is not weekend |
查看以下语句 switch...case已经写了一个有点不同使用块out.println() 和内嵌 Scriptletas:
<%! int day = 3; %>
<html>
<head><title>SWITCH...CASE Example</title></head>
<body>
<%
switch(day) {
case 0:
out.println("It's Sunday.");
break;
case 1:
out.println("It's Monday.");
break;
case 2:
out.println("It's Tuesday.");
break;
case 3:
out.println("It's Wednesday.");
break;
case 4:
out.println("It's Thursday.");
break;
case 5:
out.println("It's Friday.");
break;
default:
out.println("It's Saturday.");
}
%>
</body>
</html>
这将产生以下结果:
It's Wednesday. |
您还可以使用三种基本类型的Java循环块:for, while,and do…while 块在JSP编程。
让我们看看下面的for循环的例子:
<%! int fontSize; %>
<html>
<head><title>FOR LOOP Example</title></head>
<body>
<%for ( fontSize = 1; fontSize <= 3; fontSize++){ %>
<font color="green" size="<%= fontSize %>">
JSP Tutorial
</font><br />
<%}%>
</body>
</html>
这将产生以下结果:
JSP Tutorial JSP Tutorial JSP Tutorial |
上面的例子中可以使用while循环如下这样写:
<%! int fontSize; %>
<html>
<head><title>WHILE LOOP Example</title></head>
<body>
<%while ( fontSize <= 3){ %>
<font color="green" size="<%= fontSize %>">
JSP Tutorial
</font><br />
<%fontSize++;%>
<%}%>
</body>
</html>
这也将产生以下结果:
JSP Tutorial JSP Tutorial JSP Tutorial |
JSP支持所有Java支持的逻辑和算术运算符。下表给出所有的运算符具有最高优先级的列表显示在表的顶部,最低的在底部。
在表达式中,优先级较高的运算符将首先计算。
分类 | 运算符 | 关联 |
---|---|---|
Postfix | () [] . (dot operator) | Left to right |
Unary | ++ - - ! ~ | Right to left |
Multiplicative | * / % | Left to right |
Additive | + - | Left to right |
Shift | >> >>> << | Left to right |
Relational | > >= < <= | Left to right |
Equality | == != | Left to right |
Bitwise AND | & | Left to right |
Bitwise XOR | ^ | Left to right |
Bitwise OR | | | Left to right |
Logical AND | && | Left to right |
Logical OR | || | Left to right |
Conditional | ?: | Right to left |
Assignment | = += -= *= /= %= >>= <<= &= ^= |= | Right to left |
Comma | , | Left to right |
JSP表达式语言定义了以下常量:
Boolean: true and false
Integer: as in Java
Floating point: as in Java
String: with single and double quotes; " is escaped as ", ' is escaped as ', and is escaped as .
Null: null