EL表示式語言(Servlet+jsp)
1:Servlet
package cn.mldn.lxh.vo;
import java.io.IOException;
import java.util.List;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@SuppressWarnings("serial")
public class ELListServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
List<Dept> all=new ArrayList<Dept>();
Dept dept=null;
dept=new Dept();
dept.setDeptno(101);
dept.setName("開發工程師");
dept.setLoc("北京市");
all.add(dept);
dept=new Dept();
dept.setDeptno(102);
dept.setName("前端工程師");
dept.setLoc("北京市");
all.add(dept);
dept.setDeptno(103);
dept.setName("運維工程師");
dept.setLoc("北京市");
all.add(dept);
request.setAttribute("alldept", all);
request.getRequestDispatcher("el/list_dept.jsp").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
2:jsp(El表示式應用)
<%@ page contentType="text/html" language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML >
<html>
<head>
<title></title>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
List all=(List)request.getAttribute("alldept");
if(all!=null)
{
%>
<div align="center">
<table border="1" width="50%">
<tr>
<td>部門編號</td>
<td>部門名稱</td>
<td>部門位置</td>
</tr>
<%
Iterator iter=all.iterator();
while(iter.hasNext())
{
request.setAttribute("dept", iter.next());
%>
<tr>
<td>${dept.deptno}</td>
<td>${dept.name}</td>
<td>${dept.loc}</td>
</tr>
<%
}
%>
</table>
</div>
<%
}
%>
</body>
</html>