Servlets & Jsp 实验10:表达式语言(EL)的使用
10 实验十 表达式语言的使用
一. 实验目的
1. 了解表达式语言的功能;
2. 掌握表达式语言的使用。
二. 实验内容
1. 表达式语言运算符的使用
下面的JSP页面operator.jsp演示了EL运算符的使用:
<%@ page contentType="text/html;charset=gb2312" %>
<html>
<head>
<title>JSP 2.0 Expression Language - Basic Arithmetic</title> </head>
<body>
<h1>JSP 2.0 表达式语言 - 基本算术运算符</h1>
<hr>
该例说明了基本的表达式语言的算术运算符的使用,其中包括加(+),减(-), 乘(*),除(/ 或 div), 取余 (% 或 mod)。
<br>
<blockquote>
<code>
<table border="1">
<thead>
<td><b>EL 表达式</b></td>
<td><b>结果</b></td>
</thead>
<tr><td>\${1}</td> <td>${1}</td> </tr>
<tr> <td>\${1 + 2}</td> <td>${1 + 2}</td> </tr>
<tr> <td>\${1.2 + 2.3}</td> <td>${1.2 + 2.3}</td> </tr>
<tr> <td>\${1.2E4 + 1.4}</td> <td>${1.2E4 + 1.4}</td> </tr> <tr> <td>\${-4 - 2}</td> <td>${-4 - 2}</td> </tr>
<tr> <td>\${21 * 2}</td> <td>${21 * 2}</td> </tr>
<tr> <td>\${3/4}</td> <td>${3/4}</td> </tr>
<tr> <td>\${3 div 4}</td> <td>${3 div 4}</td> </tr>
<tr> <td>\${3/0}</td> <td>${3/0}</td> </tr>
<tr> <td>\${10%4}</td> <td>${10%4}</td> </tr>
<tr> <td>\${10 mod 4}</td> <td>${10 mod 4}</td> </tr>
<tr> <td>\${(1==2) ? 3 : 4}</td> <td>${(1==2) ? 3 : 4}</td> </tr> </table>
</code>
</blockquote>
</body>
</html>
Servlets & Jsp 实验10:表达式语言(EL)的使用
2 访问作用域变量
编写一个名为EmployeeBean的JavaBean,其中包括3个属性eno表示雇员号、ename表示雇员名和ecompany表示雇员公司名。
【步骤1】EmployeeBean.java程序代码
package com.beans;
public class EmployeeBean {
private String eno = "";
private String ename = "";
private String ecompany = "";
public EmployeeBean() {
}
public void setEno(String eno){
this.eno = eno;
}
public void setEname(String ename){
this.ename = ename;
}
public void setEcompany(String ecompany){
this.ecompany = ecompany;
}
public String getEno(){
return eno;
}
public String getEname(){
return ename;
}
public String getEcompany(){
return ecompany;
}
Servlets & Jsp 实验10:表达式语言(EL)的使用
【步骤2】编写一个JSP页面,在其中通过表单输入雇员信息,将请求转发到一个Servlet。 <%@ page contentType="text/html;charset=gb2312"%>
<html>
<body>
请输入雇员信息:
<form action="employee.do" method="post">
<table>
<tr><td>雇员号:</td><td><input type="text" name="eno"></td></tr>
<tr><td>雇员名:</td><td><input type="text" name="ename"></td></tr>
<tr><td>公司名:</td><td><input type="text" name="ecompany"></td></tr> </table>
<input type="submit" value="提交">
</form>
</body>
</html>
【步骤3】下面的Servlet从JSP页面得到客户信息
package com.control;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.beans.EmployeeBean;
public class EmployeeServlet extends HttpServlet{
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException{
String eno = request.getParameter("eno");
String ename = request.getParameter("ename");
String ecompany = request.getParameter("ecompany");
EmployeeBean employee = new EmployeeBean();
employee.setEno(eno);
employee.setEname(ename);
employee.setEcompany(ecompany);
request.setAttribute("employee", employee);
RequestDispatcher view =
request.getRequestDispatcher("/displayEmployee.jsp");
view.forward(request, response);
}
}
Servlets & Jsp 实验10:表达式语言(EL)的使用
【步骤4】下面的JSP使用EL表达式显示用户的信息
<%@ page contentType="text/html;charset=gb2312"%>
<html><body>
雇员的信息如下:<br>
<ul>
<li>雇员号:${employee.eno}
<li>雇员名:${employee.ename}
<li>公司名:${employee.ecompany}
</ul>
</body></html>
3. 隐含对象的使用
下面的JSP页面implicit.jsp演示了EL隐含对象的使用。
<%@ page contentType="text/html;charset=gb2312" %>
<html>
<head>
<title>EL implicit objects</title>
</head>
<body>
<h1>JSP 2.0 表达式语言-隐含对象</h1>
<hr>
<blockquote>
<b>输入foo参数值</b>
<form action="implicit.jsp" method="GET">
foo= <input type="text" name="foo" value="${param["foo"]}">
<input type="submit">
</form>
<br>
<code>
<table border="1">
<thead>
<td><b>EL 表达式</b></td>
<td><b>结果</b></td>
</thead>
<tr>
<td>\${param.foo}</td>
<td>${param.foo} </td>
</tr>
<tr>
<td>\${param["foo"]}</td>
<td>${param["foo"]} </td>
</tr>
<tr>
<td>\${header["host"]}</td>
Servlets & Jsp 实验10:表达式语言(EL)的使用
<td>${header["host"]}</td>
</tr>
<tr>
<td>\${header["accept"]}</td>
<td>${header["accept"]}</td>
</tr>
<tr>
<td>\${header["user-agent"]}</td>
<td>${header["user-agent"]}</td>
</tr>
</table>
</code>
</blockquote>
</body>
</html>
Servlets & Jsp 实验10:表达式语言(EL)的使用
4. 在JSP页面中使用Java函数
设要在JSP页面中使用一个add()函数,实现两个整数的加法运算。在JSP中使用Java函数要经过如下3个步骤:
【步骤1】函数的定义,创建类文件Compute.java。它定义了要在JSP中使用的add()方法。 package com.demo;
public class Compute{
public static int add(String x,String y){
int a = 0;
int b = 0;
try{
a = Integer.parseInt(x);
b = Integer.parseInt(y);
}catch(Exception e){
System.err.println("Number format is illegal.");
}
return a+b;
}
}
【步骤2】创建标签库描述文件taglib.tld。它实现将每个Java方法与函数名相匹配。 <?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http:///xml/ns/j2ee"
xmlns:xsi="http:///2001/XMLSchema-instance"
xsi:schemaLocation="http:///xml/ns/j2ee
http:///xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<description>A Simple Taglib File.</description>
<tlib-version>1.0</tlib-version>
<short-name>Simple Tag Library</short-name>
<uri>http:///function</uri>
<function>
<description>Adding two numbers</description>
<name>add</name>
<function-class>http://pute</function-class>
<function-signature>
int add( http://ng.String, http://ng.String)
</function-signature>
</function>
</taglib>
注意:将该文件保存在Web应用程序WEB-INF\classes目录中。
【步骤3】编写JSP文件sum.jsp,使用标签库URI以及函数名调用Java函数,代码如下: <%@ page contentType="text/html;charset=gb2312" %>
<%@ taglib prefix="demo" uri="http:///function" %>
<html>
<head><title>Using Function </title></head>
<body>
<h3>计算两个整数之和</h3>
<p>
<form action="sum.jsp" method="post">
X = <input type="text" name="x" size="5" />
Y = <input type="text" name="y" size="5" />
<input type="submit" value="计算">
</form>
<p>
Servlets & Jsp 实验10:表达式语言(EL)的使用
两个整数的和为:${demo:add(param["x"],param["y"])}
</body>
</html>
该页面运行结果为:
图10.1 sum.jsp页面的运行结果
5. 下面在Functions类中定义了reverse()函数用来实现字符串反转、numVowels()函数用来实现统计字符串中元音字母个数、caps()函数用来将一个字符串转换成大写字母、palindrome()函数用来判断一个字符串是否是回文。
编译该文件,编写TLD文件定义这些函数,编写JSP页面使用这些函数。
package com.function;
import java.util.*;
public class Functions {
public static String oldString(String text)
{
return text;
}
public static String reverse( String text ) {
Servlets & Jsp 实验10:表达式语言(EL)的使用
return new StringBuffer( text ).reverse().toString();
}
public static int numVowels( String text ) {
String vowels = "aeiouAEIOU";
int result = 0;
for( int i = 0; i < text.length(); i++ ) {
if( vowels.indexOf( text.charAt( i ) ) != -1 ) {
result++;
}
}
return result;
}
public static String caps( String text ) {
return text.toUpperCase();
}
public static boolean palinDrome(String str){
StringBuffer sb = new StringBuffer(str);
if((sb.reverse().toString()).equals(str)){
return true;
}else{
return false;
}
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http:///xml/ns/j2ee"
xmlns:xsi="http:///2001/XMLSchema-instance"
xsi:schemaLocation="http:///xml/ns/j2ee
http:///xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<description>A Simple Taglib File.</description>
<tlib-version>1.0</tlib-version>
<short-name>Simple Tag Library</short-name>
<uri>http:///function</uri>
<function>
<description>output the old String</description>
<name>oldString</name>
<function-class>com.function.Functions</function-class>
<function-signature>http://ng.String
oldString(http://ng.String)</function-signature>
</function>
<function>
<description>Reverses the characters in the given String</description> <name>reverse</name>
<function-class>com.function.Functions</function-class>
<function-signature>http://ng.String
reverse(http://ng.String)</function-signature>
</function>
<function>
<description>Counts the number of vowels (a,e,i,o,u) in the given String</description>
<name>numVowels</name>
Servlets & Jsp 实验10:表达式语言(EL)的使用
<function-class>com.function.Functions</function-class>
<function-signature>http://ng.int
numVowels(http://ng.String)</function-signature>
</function>
<function>
<description>Converts the string to all caps</description>
<name>caps</name>
<function-class>com.function.Functions</function-class>
<function-signature>http://ng.String
caps(http://ng.String)</function-signature>
</function>
<function>
<description>Judge a string if a palinDrome</description>
<name>palinDrome</name>
<function-class>com.function.Functions</function-class>
<function-signature>http://ng.Boolean
palinDrome(http://ng.String)</function-signature>
</function>
</taglib>
<%@ page contentType="text/html;charset=gb2312" %>
<%@ taglib prefix="function" uri="http:///function" %> <html>
<head><title>Using Function </title></head>
<body>
<h3>函数功能</h3>
<p>
<form action="func.jsp" method="post">
text:<input type="text" name="text" size="20" />
<input type="submit" value="submit">
</form>
<p>
you entered: ${function:oldString(param["text"])}
<br>
After reversing: ${function:reverse(param["text"])}
<br>
After counting vowels: there are ${function:numVowels(param["text"])} vowels.
<br>
After exchanging:${function:caps(param["text"])}
<br>
After judging: the text weather is palinDrome?
${function:palinDrome(param["text"])}
</body>
</html>
Servlets & Jsp 实验10:表达式语言(EL)的使用
三. 思考题
1. 简述JSP表达式语言的主要功能有哪些?
2.简述在JSP页面中使用Java函数的步骤。
源代码:
1:
(1):input.jsp
<%@ page contentType="text/html;charset=gb2312"%>
<html>
<body>
请输入雇员信息:
<form action="employee.do" method="post">
<table>
<tr><td>雇员号:</td><td><input type="text" name="eno"></td></tr>
<tr><td>雇员名:</td><td><input type="text" name="ename"></td></tr>
<tr><td>公司名:</td><td><input type="text" name="ecompany"></td></tr>
</table>
<input type="submit" value="提交">
</form>
</body>
</html>
(2): EmployeeServlet.servlet
package com.control;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.beans.EmployeeBean;
public class EmployeeServlet extends HttpServlet{
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException{
String eno = request.getParameter("eno");
String ename = request.getParameter("ename");
Servlets & Jsp 实验10:表达式语言(EL)的使用
String ecompany = request.getParameter("ecompany");
EmployeeBean employee = new EmployeeBean();
employee.setEno(eno);
employee.setEname(ename);
employee.setEcompany(ecompany);
request.setAttribute("employee", employee);
RequestDispatcher view =
request.getRequestDispatcher("/displayEmployee.jsp");
view.forward(request, response);
}
}
(3):web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http:///xml/ns/javaee"
xmlns:xsi="http:///2001/XMLSchema-instance"
xsi:schemaLocation="http:///xml/ns/javaee
http:///xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name> <servlet-name>EmployeeServlet</servlet-name>
<servlet-class>com.control.EmployeeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>EmployeeServlet</servlet-name>
<url-pattern>/employee.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
(4):EmployeeBean.java
package com.beans;
public class EmployeeBean {
private String eno = "";
private String ename = "";
private String ecompany = "";
public EmployeeBean() {
Servlets & Jsp 实验10:表达式语言(EL)的使用
}
public void setEno(String eno){
this.eno = eno;
}
public void setEname(String ename){
this.ename = ename;
}
public void setEcompany(String ecompany){
this.ecompany = ecompany;
}
public String getEno(){
return eno;
}
public String getEname(){
return ename;
}
public String getEcompany(){
return ecompany;
}
}
(5):displayEmployee.jsp
<%@ page contentType="text/html;charset=gb2312"%>
<html><body>
雇员的信息如下:<br>
<ul>
<li>雇员号:${employee.eno}
<li>雇员名:${employee.ename}
<li>公司名:${employee.ecompany}
</ul>
</body></html>
4:
(1):Compute.java
package com.demo;
public class Compute{
public static int add(String x,String y){
int a = 0;
int b = 0;
try{
a = Integer.parseInt(x);
b = Integer.parseInt(y);
}catch(Exception e){
System.err.println("Number format is illegal.");
}
Servlets & Jsp 实验10:表达式语言(EL)的使用
return a+b;
}
}
(2)sum.jsp
<%@ page contentType="text/html;charset=gb2312" %>
<%@ taglib prefix="demo" uri="http:///function" %>
<html>
<head><title>Using Function </title></head>
<body>
<h3>计算两个整数之和</h3>
<p>
<form action="sum.jsp" method="post">
X = <input type="text" name="x" size="5" />
Y = <input type="text" name="y" size="5" />
<input type="submit" value="计算">
</form>
<p>
两个整数的和为:${demo:add(param["x"],param["y"])}
</body>
</html>
(3):taglib.tld
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http:///xml/ns/j2ee"
xmlns:xsi="http:///2001/XMLSchema-instance"
xsi:schemaLocation="http:///xml/ns/j2ee
http:///xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<description>A Simple Taglib File.</description>
<tlib-version>1.0</tlib-version>
<short-name>Simple Tag Library</short-name>
<uri>http:///function</uri>
<function>
<description>Adding two numbers</description>
<name>add</name>
<function-class>http://pute</function-class>
<function-signature>
int add(http://ng.String, http://ng.String)
</function-signature>
</function>
</taglib>
5.
(1)Functions.java
package com.function;
import java.util.*;
Servlets & Jsp 实验10:表达式语言(EL)的使用
public class Functions {
public static String oldString(String text)
{
return text;
}
public static String reverse( String text ) {
return new StringBuffer( text ).reverse().toString();
}
public static int numVowels( String text ) {
String vowels = "aeiouAEIOU";
int result = 0;
for( int i = 0; i < text.length(); i++ ) {
if( vowels.indexOf( text.charAt( i ) ) != -1 ) {
result++;
}
}
return result;
}
public static String caps( String text ) {
return text.toUpperCase();
}
public static boolean palinDrome(String str){
StringBuffer sb = new StringBuffer(str);
if((sb.reverse().toString()).equals(str)){
return true;
}else{
return false;
}
}
}
(2)func.tld
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http:///xml/ns/j2ee"
xmlns:xsi="http:///2001/XMLSchema-instance"
xsi:schemaLocation="http:///xml/ns/j2ee
http:///xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<description>A Simple Taglib File.</description>
<tlib-version>1.0</tlib-version>
<short-name>Simple Tag Library</short-name>
<uri>http:///function</uri>
Servlets & Jsp 实验10:表达式语言(EL)的使用
<function>
<description>output the old String</description>
<name>oldString</name>
<function-class>com.function.Functions</function-class>
<function-signature>http://ng.String oldString(http://ng.String)</function-signature> </function>
<function>
<description>Reverses the characters in the given String</description>
<name>reverse</name>
<function-class>com.function.Functions</function-class>
<function-signature>http://ng.String reverse(http://ng.String)</function-signature> </function>
<function>
<description>Counts the number of vowels (a,e,i,o,u) in the given String</description> <name>numVowels</name>
<function-class>com.function.Functions</function-class>
<function-signature>http://ng.int numVowels(http://ng.String)</function-signature> </function>
<function>
<description>Converts the string to all caps</description>
<name>caps</name>
<function-class>com.function.Functions</function-class>
<function-signature>http://ng.String caps(http://ng.String)</function-signature> </function>
<function>
<description>Judge a string if a palinDrome</description>
<name>palinDrome</name>
<function-class>com.function.Functions</function-class>
<function-signature>http://ng.Boolean palinDrome(http://ng.String)</function-signature> </function>
</taglib>
(3)
func.jsp
<%@ page contentType="text/html;charset=gb2312" %>
<%@ taglib prefix="function" uri="http:///function" %>
<html>
<head><title>Using Function </title></head>
<body>
Servlets & Jsp 实验10:表达式语言(EL)的使用
<h3>函数功能</h3>
<p>
<form action="func.jsp" method="post">
text:<input type="text" name="text" size="20" />
<input type="submit" value="submit">
</form>
<p>
you entered: ${function:oldString(param["text"])}
<br>
After reversing: ${function:reverse(param["text"])}
<br>
After counting vowels: there are ${function:numVowels(param["text"])} vowels. <br>
After exchanging:${function:caps(param["text"])}
<br>
After judging: the text weather is palinDrome?
${function:palinDrome(param["text"])}
</body>
</html>