先把准备工作做好,把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;
}
}