EL中提供了11个默认对象
4大范围,用于获取传递的数据【jsp不生产数据】
pageScope requestScope sessionScope applicationScope
<%
request.setAttribute("now",new Date());
pageContext.setAttribute("now",123456);
%>
${requestScope.now}
获取请求参数: param和paramValues对象
http://localhost:8080/pp/b1.jsp?name=yan&name=jun&name=zhangssan ${param.name}
${paramValues.name[1]}
${param.name}相当于request.getParameter("name"):String
${paramValues.name}相当于request.getParameterValues("name"):String[]
获取请求头参数:header和headerValues对象 相当于request.getHeader()
<%
Enumeration ee = request.getHeaderNames();
while (ee.hasMoreElements()) {
String name = ee.nextElement().toString();
String value=request.getHeader(name);
out.println(name+"---"+value+"<br/>");
}
%>
<hr/>
${header.host }<br/>
${header['user-agent']}
pageContext对象
pageContext对象是JSP中pageContext对象的引用。通过pageContext对象,您可以访问request对象
使用绝对路径问题:
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<base href="<%=basePath%>">
方法解析:
request.getScheme():String用于获取协议名称,例如http
request.getServerName():String 用于获取服务器名称,例如localhost
request.getServerPort():int 用于获取服务器的端口号,例如8080
request.getContextPath():String 用于应用部署后的路径名称,例如/pp
<base>标签:base元素指定当前页面中所有链接的基础路径。就是将base的href值添加到页面中所有相对路径之前
<A HREF="<%=request.getContextPath()%>/bb/ab/bb.jsp">bb</A>
<A HREF="${pageContext.request.contextPath}/bb/ab/bb.jsp">bb</A>
pageContext.request似乎是在调用pageContext的request属性,实际上是调用pageContext的getRequest()方法
cookie用于访问cookie
跟踪用户4大方法: 隐藏域 URL重写 Cookie session
${cookie.JSESSIONID.name}---${cookie.JSESSIONID.value}
initParam用于访问上下文初始化参数
web.xml配置上下文参数
<context-param>
<param-name>appName</param-name>
<param-value>周晨的裸照</param-value>
</context-param>
<%=application.getInitParameter("appName")%>
${initParam.appName}