- package org.ml.eldemo.vo;
- public class Dept {
- private int deptno;
- private String dname;
- private String loc;
- public int getDeptno() {
- return deptno;
- }
- public void setDeptno( int deptno) {
- this.deptno = deptno;
- }
- public String getDname() {
- return dname;
- }
- public void setDname(String dname) {
- this.dname = dname;
- }
- public String getLoc() {
- return loc;
- }
- public void setLoc(String loc) {
- this.loc = loc;
- }
- }
- <%@ page language="java" contentType="text/html; charset=GBK"
- pageEncoding="GBK"%>
- <%@page import="org.ml.eldemo.vo.*" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=GBK">
- <title>Insert title here</title>
- </head>
- <body>
- <%
- Dept dept = new Dept();
- dept.setDeptno(10);
- dept.setDname("实践助学部");
- dept.setLoc("贵州大学北区");
- request.setAttribute("deptinfo",dept);
- %>
- <h3>部门编号:${deptinfo.deptno}</h3>
- <h3>部门名称:${deptinfo.dname}</h3>
- <h3>部门位置:${deptinfo.loc}</h3>
- </body>
- </html>
- package org.ml.servlet;
- import java.io.IOException;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.ml.eldemo.vo.Dept;
- public class ELServlet extends HttpServlet {
- private static final long serialVersionUID = 1L;
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- Dept dept = new Dept();
- dept.setDeptno(10);
- dept.setDname("实践助学部");
- dept.setLoc("贵州大学北区");
- request.setAttribute("deptinfo",dept);
- request.getRequestDispatcher("print_MVC_vo.jsp").forward(request, response);
- }
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- this.doGet(request, response);
- }
- }
- <%@ page language="java" contentType="text/html; charset=GBK"
- pageEncoding="GBK"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=GBK">
- <title>Insert title here</title>
- </head>
- <body>
- <h3>部门编号:${deptinfo.deptno}</h3>
- <h3>部门名称:${deptinfo.dname}</h3>
- <h3>部门位置:${deptinfo.loc}</h3>
- </body>
- </html>
- <servlet>
- <servlet-name>el</servlet-name>
- <servlet-class>
- org.ml.servlet.ELServlet
- </servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>el</servlet-name>
- <url-pattern>/ELServlet</url-pattern>
- </servlet-mapping>
此时在地址栏中输入http://localhost:8080/MVC_${}/ELServlet,会得到下面的结果(结果中的地址栏看起来有不认识的码,是因为{}在页面运行时进行的编码【MVC_${}是工程名】):
- package org.ml.servlet;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.ml.eldemo.vo.Dept;
- public class ELServlet_List extends HttpServlet {
- private static final long serialVersionUID = 1L;
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- List<Dept> all = new ArrayList<Dept>();
- Dept dept = null;
- dept = new Dept();
- dept.setDeptno(10);
- dept.setDname("实践助学部");
- dept.setLoc("贵州大学北校区");
- all.add(dept);
- dept = new Dept();
- dept.setDeptno(10);
- dept.setDname("纪律检查部");
- dept.setLoc("贵州大学南校区");
- all.add(dept);
- dept = new Dept();
- dept.setDeptno(10);
- dept.setDname("信息中心");
- dept.setLoc("贵州大学北校区");
- all.add(dept);
- request.setAttribute("alldept",all);
- request.getRequestDispatcher("print_MVC_List_vo.jsp").forward(request, response);
- }
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- this.doGet(request, response);
- }
- }
- <%@ page language="java" contentType="text/html; charset=GBK"
- pageEncoding="GBK"%>
- <%@ page import="java.util.*" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=GBK">
- <title>Insert title here</title>
- </head>
- <body>
- <%
- List all = (List)request.getAttribute("alldept");
- if(all!=null){
- %>
- <table border="2" align="center" width="50%">
- <tr>
- <th>部门编号</th>
- <th>部门名称</th>
- <th>部门地址</th>
- </tr>
- <%
- Iterator iter = all.iterator();
- while(iter.hasNext()){
- pageContext.setAttribute("deptinfo",iter.next());
- %>
- <tr>
- <td>${deptinfo.deptno}</td>
- <td>${deptinfo.dname}</td>
- <td>${deptinfo.loc}</td>
- </tr>
- <%
- }
- %>
- </table>
- <%
- }
- %>
- </body>
- </html>
- <servlet>
- <servlet-name>el_list</servlet-name>
- <servlet-class>
- org.ml.servlet.ELServlet_List
- </servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>el_list</servlet-name>
- <url-pattern>/ELServlet_List</url-pattern>
- </servlet-mapping>