Servlets & Jsp 实验10:表达式语言(EL)的使用
时间:2025-05-01
时间:2025-05-01
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>
下一篇:入学复习题-专升本-数学部分