十二、脚本元素、指令和预定义变量_MySQL
12.1 JSP脚本元素
JSP脚本元素用来插入Java代码,这些Java代码将出现在由当前JSP页面生成的Servlet中。脚本元素有三种格式:
表达式格式<%= expression %>:计算表达式并输出其结果。
Scriptlet格式<% code %>:把代码插入到Servlet的service方法。
声明格式<%! code %>:把声明加入到Servlet类(在任何方法之外)。
下面我们详细说明它们的用法。
12.1.1 JSP表达式
JSP表达式用来把Java数据直接插入到输出。其语法如下:
<%= Java Expression %>
计算Java表达式得到的结果被转换成字符串,然后插入到页面。计算在运行时进行(页面被请求时),因此可以访问和请求有关的全部信息。例如,下面的代码显示页面被请求的日期/时间:
Current time: <%= new java.util.Date() %>
为简化这些表达式,JSP预定义了一组可以直接使用的对象变量。后面我们将详细介绍这些隐含声明的对象,但对于JSP表达式来说,最重要的几个对象及其类型如下:
request:HttpServletRequest;
response:HttpServletResponse;
session:和request关联的HttpSession
out:PrintWriter(带缓冲的版本,JspWriter),用来把输出发送到客户端
下面是一个例子:
Your hostname: <%= request.getRemoteHost() %>
最后,如果使用XML的话,JSP表达式也可以写成下面这种形式:
<jsp:expression>
Java Expression
</jsp:expression>
请记住XML元素和HTML不一样。XML是大小写敏感的,因此务必使用小写。有关XML语法的说明,请参见《XML教程 》
12.1.2 JSP Scriptlet
如果你要完成的任务比插入简单的表达式更加复杂,可以使用JSP Scriptlet。JSP Scriptlet允许你把任意的Java代码插入Servlet。JSP Scriptlet语法如下:
<% Java Code %>
和JSP表达式一样,Scriptlet也可以访问所有预定义的变量。例如,如果你要向结果页面输出内容,可以使用out变量:
<%
String queryData = request.getQueryString();
out.println("Attached GET data: " + queryData);
%>
注意Scriptlet中的代码将被照搬到Servlet内,而Scriptlet前面和后面的静态HTML(模板文本)将被转换成println语句。这就意味着,Scriptlet内的Java语句并非一定要是完整的,没有关闭的块将影响Scriptlet外的静态HTML。例如,下面的JSP片断混合了模板文本和Scriptlet:
<% if (Math.random() < 0.5) { %>
Have a <B>nice</B> day!
<% } else { %>
Have a <B>lousy</B> day!
<% } %>
上述JSP代码将被转换成如下Servlet代码:
if (Math.random() < 0.5) {
out.println("Have a <B>nice</B> day!");
} else {
out.println("Have a <B>lousy</B> day!");
}
如果要在Scriptlet内部使用字符“%>”,必须写成“%\>”。另外,请注意<% code %>的XML等价表达是:
<jsp:scriptlet>
Code
</jsp:scriptlet>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











How to get items using commands in Terraria? 1. What is the command to give items in Terraria? In the Terraria game, giving command to items is a very practical function. Through this command, players can directly obtain the items they need without having to fight monsters or teleport to a certain location. This can greatly save time, improve the efficiency of the game, and allow players to focus more on exploring and building the world. Overall, this feature makes the gaming experience smoother and more enjoyable. 2. How to use Terraria to give item commands 1. Open the game and enter the game interface. 2. Press the "Enter" key on the keyboard to open the chat window. 3. Enter the command format in the chat window: "/give[player name][item ID][item quantity]".

How to execute .sh file in Linux system? In Linux systems, a .sh file is a file called a Shell script, which is used to execute a series of commands. Executing .sh files is a very common operation. This article will introduce how to execute .sh files in Linux systems and provide specific code examples. Method 1: Use an absolute path to execute a .sh file. To execute a .sh file in a Linux system, you can use an absolute path to specify the location of the file. The following are the specific steps: Open the terminal

Cutting is a video editing tool with comprehensive editing functions, support for variable speed, various filters and beauty effects, and rich music library resources. In this software, you can edit videos directly or create editing scripts, but how to do it? In this tutorial, the editor will introduce the method of editing and making scripts. Production method: 1. Click to open the editing software on your computer, then find the "Creation Script" option and click to open. 2. In the creation script page, enter the "script title", and then enter a brief introduction to the shooting content in the outline. 3. How can I see the "Storyboard Description" option in the outline?

The composite primary key in MySQL refers to the primary key composed of multiple fields in the table, which is used to uniquely identify each record. Unlike a single primary key, a composite primary key is formed by combining the values of multiple fields. When creating a table, you can define a composite primary key by specifying multiple fields as primary keys. In order to demonstrate the definition and function of composite primary keys, we first create a table named users, which contains three fields: id, username and email, where id is an auto-incrementing primary key and user

"Exploring Discuz: Definition, Functions and Code Examples" With the rapid development of the Internet, community forums have become an important platform for people to obtain information and exchange opinions. Among the many community forum systems, Discuz, as a well-known open source forum software in China, is favored by the majority of website developers and administrators. So, what is Discuz? What functions does it have, and how can it help our website? This article will introduce Discuz in detail and attach specific code examples to help readers learn more about it.

We've designed this Windows PowerShell scripting tutorial for beginners, whether you're a tech enthusiast or a professional looking to improve your scripting skills. If you have no prior knowledge of PowerShell scripting, this article will start with the basics and be tailored for you. We'll help you master the installation steps for a PowerShell environment and walk you through the main concepts and features of PowerShell scripts. If you're ready to learn more about PowerShell scripting, let's embark on this exciting learning journey together! What is WindowsPowerShell? PowerShell is a hybrid command system developed by Microsoft

Using Ajax to obtain variables from PHP methods is a common scenario in web development. Through Ajax, the page can be dynamically obtained without refreshing the data. In this article, we will introduce how to use Ajax to get variables from PHP methods, and provide specific code examples. First, we need to write a PHP file to handle the Ajax request and return the required variables. Here is sample code for a simple PHP file getData.php:

Python is widely used in a wide range of fields with its simple and easy-to-read syntax. It is crucial to master the basic structure of Python syntax, both to improve programming efficiency and to gain a deep understanding of how the code works. To this end, this article provides a comprehensive mind map detailing various aspects of Python syntax. Variables and Data Types Variables are containers used to store data in Python. The mind map shows common Python data types, including integers, floating point numbers, strings, Boolean values, and lists. Each data type has its own characteristics and operation methods. Operators Operators are used to perform various operations on data types. The mind map covers the different operator types in Python, such as arithmetic operators, ratio
