Web应用开发: JSP语法编程实践(四):EL编程实践

一、实验内容

1、EL表达式的熟练使用
创建一个Java Web项目,使用EL表达式获取访问此项目的绝对地址。
2、EL表达式的熟练使用
(1)在一个Servlet中创建一个对象集合,例如:List,将此对象集合类存入request对象属性中,请求转发到listIterator.jsp;
(2)在listIterator.jsp中遍历并使用EL表达式输出Student对象的属性值。

二、实验要求

源代码和测试截图(均直接输入到答题框中)

三、实验代码

思路:利用request对象方法获取内容,再将字符串拼接出来

实验一:

//location.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>EL表达式的熟练使用</title>
</head>
<body>

<!-- 
request对象方法简介:
request.getSchema() 可以返回当前页面使用的协议,http 或是 https;

request.getServerName() 可以返回当前页面所在的服务器的名字;

request.getServerPort() 可以返回当前页面所在的服务器使用的端口,就是80;

request.getContextPath() 可以返回当前页面所在的应用的名字; -->

<!-- 该项目所在的绝对地址(字符串拼接出来) -->
<P>该项目所在的绝对地址:${pageContext.request.scheme}${'://'}${pageContext.request.serverName}${':'}${pageContext.request.serverPort}${pageContext.request.contextPath}</p>
<!-- 取得请求的URL,不包括参数字符串 -->
<%-- <p>此项目请求的URL:${pageContext.request.requestURL}</p> --%>
</body>
</html>

Web应用开发: JSP语法编程实践(四):EL编程实践

实验二:

思路:构建一个Objects.java的sevlet用来构建数据和请求转发;创建Student.java用作javabean(逻辑模型);构建listIterator.jsp用来显示。

//Objects.java
package topus;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Objects
 */
@WebServlet("/Objects")
public class Objects extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Objects() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		List<Student>list=new ArrayList<>();
		for(int i=0;i<100;i++){
		Student stu=new Student();
		stu.setNum(i+1);
		list.add(stu);
		}
		
		//将学生列表设置到requet范围中  
		request.setAttribute("list", list);
		//转发,转发是在服务器端转发的,客户端是不知道的  
		request.getRequestDispatcher("/listIterator.jsp").forward(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//doGet(request, response);
	}

}
//Student.java
package topus;


public class Student {
private String name;
	
	private int num;

	public Student(){
		super();
	}
	
	public Student(String name, int num) {
		super();
		this.name = name;
		this.num = num;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getNum() {
		return num;
	}

	public void setNum(int num) {
		this.num = num;
	}
	
	
}
//listIterator.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib prefix="c" 
           uri="http://java.sun.com/jsp/jstl/core" %>
<!-- 标签库导入教程http://www.runoob.com/jsp/jsp-jstl.html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- 这些标签封装了Java中的for,while,do-while循环。学习链接http://www.runoob.com/jsp/jstl-core-foreach-tag.html -->
<%-- <p>(测试用)获取request属性值: ${requestScope.list[1].num}</p> --%>

<p>获取得到的学生序号:</p>
<c:forEach var="i" begin="0" end="99">
   Student序号 <c:out value="${requestScope.list[i].num}"/><p>
</c:forEach>
</body>
</html>

Web应用开发: JSP语法编程实践(四):EL编程实践

注意其中这段代码:

<c:forEach var="i" begin="0" end="99">
   Student序号 <c:out value="${requestScope.list[i].num}"/><p>
</c:forEach>

关于foreach:(http://www.runoob.com/jsp/jstl-core-foreach-tag.html)
这段代码属于JSP 标准标签库(JSTL)的内容,需要在lib里面导入标签库包,以及在xml里面建立映射,然后在在jsp声明中导入taglib 。过程很繁琐(教程链接:http://www.runoob.com/jsp/jsp-jstl.html),也可以直接使用jsp脚本的for循环来显示。