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>