问题:后台代码如下:
public class HelloServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("222222"); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String currentTime = dateFormat.format(new Date()); req.setAttribute("currentTime",currentTime); req.getRequestDispatcher("/WEB-INF/jsp/hello.jsp").forward(req,resp); } }
前端代码:
<%@ page pageEncoding="UTF-8" %> <html> <head> <title>hello</title> </head> <body> <h1>Hello</h1> <h2>当前时间 : ${currentTime}</h2> </body> </html>
输出为:
Hello
当前时间 : ${currentTime}
问题原因:EL表达式不能够正确运行、解析。只需要加入:
<%@ page isELIgnored="false" %>
就正常可以访问到值