首先我们在request域放入一些数据:
package ELServletPack; import java.io.IOException; import java.util.HashMap; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/ELServlet") public class ELServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); response.setContentType("text/html; charset=UTF-8"); request.setAttribute("name", "zs"); System.out.println("访问到了"); Map<String,String> map = new HashMap<>(); map.put("1", "lisi"); map.put("2", "ww"); map.put("3", "zl"); request.setAttribute("mapElements", map); request.getRequestDispatcher("El.jsp").forward(request, response);; } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
新建jsp文件,读取request域中数据
<%@ 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 charset="UTF-8"> <title>Insert title here</title> </head> <body> ----request域---<br/> ${requestScope.name }<br/> --------Map对象-------<br/> ${requestScope.mapElements["1"]}<br/> ${requestScope.mapElements["2"]}<br/> ${requestScope.mapElements["3"]}<br/> --JSTL---request <br/> <c:out value="${requestScope.name}"/> </body> </html>