EL表达式的作用是 显示字符串
@WebServlet(urlPatterns = "/Myservlet")
public class Myservlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setAttribute("username","itcast");
req.setAttribute("password","123");
req.getRequestDispatcher("/01myjsp.jsp").forward(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req,resp);
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
用户名:<%=request.getAttribute("username")%><br>
密码:<%=request.getAttribute("password")%><br>
============================================<br>
使用EL表达式<br>
用户名:${username}<br>
密码:${password}<br>
</body>
</html>
注意点:
当域对象中的值不存在时,如果我们使用EL表达式获取域对象里面的值时返回空字符串
使用Java方式获取时,如果返回值是null,会报空指针异常