el和jstl如何获取普通对象,数组对象,list对象,hashset集合,hashmap集合

先把准备工作做好,把jstl.jar   standard.jar都放入lib下面方便写jstl时不再顾虑

1.先创建一个jsp

此处要提示一下如果jstl.jar用的是1.1应写为这样

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" >
<title>Insert title here</title>
</head>
<body>
	<%=request.getAttribute("score")%>
	${score }
	${address }
	<br/>
	${student.id}${student.userName}${student.mobile}${student.address}<!-- 获取普通对象 -->
	
	<br/>
	${names[0] }${names[1] }${names[2] }
	<br/>
	<c:forEach var="name" items="${names }">
		${name }
	</c:forEach>
	<br/>
	${list[0] }${list[1] }${list[2] }
	<br/>
	<c:forEach var="l" items="${list }">
		${l }
	</c:forEach>
	<br/>
	<c:forEach var="s" items="${set }"><!-- 获取hashset对象时,不能用${key[]}方式 -->
		${s }
	</c:forEach>
		<c:forEach var="score" items="${scores }">
			${score.key }------${score.value }<br/><!--hashMap集合中数据是无序的,所有就无法使用set集合的方法遍历了 -->
		</c:forEach>
	
</body>
</html>

2.创建一个DataServlet.java

package yxy;

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

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

public class DataServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String name=request.getParameter("name");
		System.out.println(name);
		request.setAttribute("score",100);
		request.setAttribute("address","河南工业大学");//基本数据类型和String类型${key}
		request.setAttribute("student",new Student("110", "yxy", "123456", "河南工业大学"));//对象${key.属性}
		request.setAttribute("names", new String[]{"Lily","Tom","Kate"});//数组:${key[0]}  JSTL<c :forEach>
		List<Integer> list=new ArrayList<Integer>();
		list.add(90);
		list.add(1);
		list.add(2);
		request.setAttribute("list", list);
		Set<Integer> set=new HashSet<Integer>();
		set.add(90);
		set.add(1);
		set.add(2);
		request.setAttribute("set", set);
                Map<String,Integer> map =new HashMap<String, Integer>();
		map.put("Tom", 10);
		map.put("July", 100);
		map.put("Suny", 101);
		map.put("Lily", 102);
		request.setAttribute("scores", map);
		
		request.getRequestDispatcher("target.jsp").forward(request, response);
	}

}

此处要创建一个Student的封装类

package yxy;

public class Student {
	private String id;
	private String userName;
	private String mobile;
	private String address;
	public Student(String id, String userName, String mobile, String address) {
		super();
		this.id = id;
		this.userName = userName;
		this.mobile = mobile;
		this.address = address;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getMobile() {
		return mobile;
	}
	public void setMobile(String mobile) {
		this.mobile = mobile;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	
}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值