EL:通过EL标签取数据和输出数据,简化代码,尽量在JSP中不要使用Java代码。
语法:${输出数据或者取对象} 可不能用它来搞for循环
一.el中的表达式
a)算术运算符
+、-、*、/和 %(或 mod)
b)关系运算符
==(或 eq)、!=(或 ne)、<(或 lt)、
>(或 gt)、<=(或 le)和 >=(或 ge)
c)逻辑运算符
&&(或 and)、||(或 or)和 !(或 not)
d)判空运算符
**empty**
<%String name="";
request.setAttribute("name",name);%>
如:${empty name}
测试:
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<base href="<%=basePath%>" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>邮箱登录</title>
<style type="text/css">
body{
font-size: 20px;
margin: 0px;
padding: 0px;
background: url("logo.gif");
background-repeat: no-repeat;
background-position: right,bottom;
background-color: #7DDC87;
}
input {
border: 1px solid yellow;
border-radius:20px;
line-height: 25px;
}
.btn:LINK;.btn:VISITED {
background-color: #F0F0F0;
}
.btn:HOVER {
background-color: #148D08;
}
</style>
</head>
<body>
<%=basePath %>
<%
String error = request.getParameter("error");
if(error!=null&&error.equals("")){
out.println(error);
}
%>
<center>
<!-- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";已经到了项目下边 -->
<form action="form/loginCL.jsp">
<table>
<tr><td>用户名:</td>
<td><input type="text" name="name"></td>
<td>@163.com</td></tr>
<tr><td>密 码:</td>
<td><input type="password" name="pwd"></td>
<td><a href="">忘记密码</a></td></tr>
</table>
<input type="submit" value="登录" class="btn">
<a href="register.html">注册邮箱</a>
</form>
</center>
</body>
</html>
loginCL.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>逻辑处理</title>
</head>
<body>
<%
//在jsp中这些都是可以直接用的 不需要再new出来
String name1 = request.getParameter("name");
String pwd = request.getParameter("pwd");
if("briup".equals(name1)&&"briup".equals(pwd)){
request.setAttribute("name","briup");
/* request.getDispatcherType("success").forward(request.response); */
%>
<!-- 动态引入 -->
<jsp:include page = "success.jsp"></jsp:include>
<%-- <jsp:param value="briup" name="name"/> --%>
<%
/* response.sendRedirect("success.jsp"); */
}else{
/* request.getRequestDispatcher("login.jsp").forward( request, response); */
%>
<jsp:forward page="login.jsp">
<jsp:param value="用户名密码不正确" name="error"/>
</jsp:forward>
<%
}
%>
</body>
</html>
success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录成功</title>
</head>
<body>
<%-- 恭喜<%=request.getParameter("name") %>登陆成功 --%>
恭喜${name}, 登陆成功
</body>
</html>
恭喜${name}, 登陆成功 中采用了el 表达式;
jsp中传值有三种方法:
1.jsp的动态传入
<jsp:include page = "success.jsp">
<jsp:param value="briup" name="name"/>
</jsp:include>
2.java代码传值 动态跳转
<% request.setAttribute("name","briup");%>
<jsp:include page = "success.jsp"></jsp:include>
3.纯java传值和跳转
<%
request.setAttribute("name","briup");
request.getRequestDispatcher("success.jsp").forward(request,response);
%>
二:表达式语言(EL)中可以使用的隐含对象:
1) pageContext: jsp页面的上下文,它提供了访问以下对象的方法,唯一一个既是jsp的隐含对象,又是el的隐含对象。通过.操作符来得到数据。
a) servletContext
${pageContext.servletContext}
等于
out.println(pageContext.getServletContext())
b) session
${pageContext.session.id}
等于
out.println(pageContext.getSession().getId())
c) request
${pageContext.request}
等于
out.println(pageContext.getRequest())
d) response
${pageContext.response}
等于
out.println(pageContext.getResponse())
2) param: 把请求中的参数和单个值进行映射
${param.name}或者${param["name"]}或者${param['name']}
等于
out.println(request.getParameter("name"))
3) paramValues: 把请求中的参数和一个array值进行映射,比如获取多选框的值。
${paramValues.hobby}
或者${paramValues["hobby"]}
或者${paramValues['hobby']}
等于
String[] array =
request.getParameterValues("hobby")
out.println(array);
${paramValues.hobby[0]}
4) cookie: 把请求中的Cookie和单个值进行映射
Cookie cookie = new Cookie("height","100");
Cookie cookie2 = new Cookie("width","200");
response.addCookie(cookie);
response.addCookie(cookie2);
在服务器端获得从客户端发送过来的cookie:
${cookie.height}: 输出一个Cookie的对象
${cookie.height.name}=${cookie.height.value}
分别输出Cookie的名称和值(height=100)
${cookie.width}: 同上
${cookie.width.name}=${cookie.width.value}: 同上
5) initParam: 把Web应用上下文的初始化参数和单个值进行映射
${initParam.name}
等于
String value = getServletContext().getInitParameter("name");
out.println(value);
6) pageScope: 把page范围中的key和value进行映射
pageContext.setAttribute("name","jack");
${pageScope.name}
等于
out.println(pageContext.getAttribute("name"));
7) requestScope: 把request范围中的key和value进行映射
request.setAttribute("name","jack");
${requestScope.name}
等于
out.println(request.getAttribute("name"));
8) sessionScope: 把session范围中的key和value进行映射
session.setAttribute("name","jack");
${sessionScope.name}
等于
out.println(session.getAttribute("name"));
9) applicationScope: 把application范围中的key和value进行映射
getServletContext().setAttribute("name","jack");
${applicationScope.name}
等于
out.println(getServletContext().getAttribute("name"));
注意: 如果没有指明任何的范围根据key来查找对应的value,默认从page、request、session和application从小到大的范围开始查找,若找到就不往更大的
范围去查找。
例如: ${name} ,分别从page request session 和 application中去查找name的值
(scope.getAttribute(“name”)),scope为上面四种范围。
测试超链接后谁能拿到值:
el1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>el表达式</title>
</head>
<body>
${4+5}
<br>
${6>4 }
<br>
<%
request.setAttribute("name", "rr");
pageContext.setAttribute("name1","pp");
session.setAttribute("name","ss");
application.setAttribute("name","aa");
%>
${ empty name} <br>
${name }
<br>
${pageContext.session.id}
<%out.println(pageContext.getSession().getId()); %>
<br>
page:${pageScope.name }
<br>
session:${sessionScope.name }
<br>
app:${applicationScope.name }
<br>
request:${requestScope.name }
<br>
name:${name }
<br>
<a href = "el2.jsp">跳转查传过去的能不能取到</a>
</body>
</html>
el2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
page:${pageScope.name }
<br>
session:${sessionScope.name }
<br>
app:${applicationScope.name }
<br>
request:${requestScope.name }
</body>
</html>
发现只是剩下了application和session
同时我们也发现,el是不能做循环的,所以就引入了jstl这是第三方的得引入架包,但是刚有架包还不行,还得在jsp页面中引进来
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix = "c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jstl</title>
</head>
<body>
<h1>c:out</h1>
<c:out value="briup"></c:out>
</body>
</html>
如果出现啥xml解析错误啥的,看一下你的引用中是不是有空格,。!等
关于jsti:
第一类:表达式控制标签
1、<c:out> 输出变量到JspWriter输出流中
- 语法1:<c:out value=“result”
[escapeXml]="{true|false}"
[default=“defaultValue”]/>
注意:a)如果result为null,则输出default中的值
<c:out value="${name }" default="tom" ></c:out>
b)escapeXml:设定是否转换特殊字符(如<、>等一些转
义字符),在默认值为true的情况下直接是输出<的,如果改为 false将会进行转义输出“<”等。
2) 语法2:<c:out value="result" [escapeXml]="{true|false}">default value</c:out>
例如: value可以使用el表达式
<c:out value="${student.address.country}"/>
实例:
Login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>login页面</title>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<base href="<%=basePath%>" />
</head>
<body>
<form action="login">
账号:<input type="text" name = "name"/><br>
密码:<input type="text" name="password"/><br>
<input type="submit" value="提交">
</form>
</body>
</html>
servlet
package com.briup.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;
import com.briup.bean.User;
public class UserLoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
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");
String name = (String)request.getParameter("name");
String password = (String)request.getParameter("password");
System.out.println(name);
User u = new User();
u.setName(name);
u.setPassword(password);
HttpSession session = request.getSession();
session.setAttribute("u",u);
request.getRequestDispatcher("success.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录成功</title>
</head>
<body>
账号:<c:out value="${u.name}" default="null"/><br>
密码:<c:out value="${u.password}" default="null" />
</body>
</html>