EL優化登錄界面?
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><base href="<%=basePath%>"><title>My JSP 'login.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body><form action="loginServlet" method="post">姓名:<input type="text" name="username"><br>密碼:<input type="password" name="userpassword"><button>登錄</button> <span style="color: red;font-size:12px">${msg}</span></form></body>
</html>
將<span style="color: red;font-size:12px"><%=request.getAttribute("msg") %></span>
改成<span style="color: red;font-size:12px">${msg}</span>
節省了代碼量也提高了效率
效果如下:
在msg為NULL的時候,msg并不會顯示。
EL獲取對象?
EL操作不了局部變量。
EL在操作域對象的時候一般是從小到大依次訪問。?
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><base href="<%=basePath%>"><title>My JSP 'EL.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body><%//設置page屬性pageContext.setAttribute("name","zhangsan");//設置request屬性request.setAttribute("name","lisi");//設置session屬性session.setAttribute("name","wangwu");//設置appication屬性application.setAttribute("name","zhaoliu");String str = "Hello ";%>//獲取局部變量${str}<br>//獲取域對象${name}<br></body>
</html>