header和headerValues:
header 储存用户浏览器和服务端用来沟通的数据
例:要取得用户浏览器的版本,可以使用${header["User-Agent"]}。
另外在鲜少机会下,有可能同一标头名称拥有不同的值,此时必须改为使用headerValues 来取得这些值。
[]与.运算符:
EL 提供.和[]两种运算符来存取数据。
当要存取的属性名称中包含一些特殊字符,如.或?等并非字母或数字的符号,就一定要使用 []。
例如:
${user.My-Name}应当改为${user["My-Name"] }
如果要动态取值时,就可以用[]来做,而.无法做到动态取值。例如:
${sessionScope.user[data]}中data 是一个变量
变量:
EL存取变量数据的方法很简单,例如:${username}。它的意思是取出某一范围中名称为username的变量。因为我们并没有指定哪一个范围的username,所以它会依序从Page、Request、Session、Application范围查找。假如途中找到username,就直接回传,不再继续找下去,但是假如全部的范围都没有找到时,就回传null。
属性范围在EL中的名称
Page pageScope
Request requestScope
Session sessionScope
Application applicationScope
EL隐含对象:
1.与范围有关的隐含对象
与范围有关的EL 隐含对象包含以下四个:
pageScope、requestScope、sessionScope 和applicationScope;
它们基本上就和JSP的pageContext、request、session和application一样;
在EL中,这四个隐含对象用来取得范围属性值,即getAttribute(String name).
例如:我们要取得session中储存一个属性username的值,可以利用下列方法:
session.getAttribute("username") 取得username的值,
在EL中则使用下列方法
${sessionScope.username}
2.与输入有关的隐含对象
与输入有关的隐含对象有两个:param和paramValues,它们是EL中比较特别的隐含对象。
例如我们要取得用户的请求参数时,可以利用下列方法:
request.getParameter(String name)
request.getParameterValues(String name)
在EL中则可以使用param和paramValues两者来取得数据。
${param.name}
${paramValues.name}
其他隐含对象:
cookie
JSTL并没有提供设定cookie的动作,
例:要取得cookie中有一个设定名称为userCountry的值,可以使用${cookie.userCountry}来取得它。
header和headerValues
header 储存用户浏览器和服务端用来沟通的数据
例:要取得用户浏览器的版本,可以使用${header["User-Agent"]}。
另外在鲜少机会下,有可能同一标头名称拥有不同的值,此时必须改为使用headerValues 来取得这些值。
书上的例子:
<c:forEach items="${pageContext.request.cookies}" var="c">
<b><c:out value="${c.name}" /></b>:
<c:out value="${c.value}" /><br>
</c:forEach>
cookie:
javax.servlet.http
类 Cookie
java.lang.Object
javax.servlet.http.Cookie
包含getName和getValue方法
Example 8-3. Request information (reqinfo.jsp):
<%@ page contentType="text/html" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Request Info</title>
</head>
<body bgcolor="white">
The following information was received:
<ul>
<li>Request Method:
<c:out value="${pageContext.request[method]}" />
<li>Request Protocol:
<c:out value="${pageContext.request[protocol]}" />
<li>Context Path:
<c:out value="${pageContext.request.contextPath}" />
<li>Servlet Path:
<c:out value="${pageContext.request.servletPath}" />
<li>Request URI:
<c:out value="${pageContext.request.requestURI}" />
<li>Request URL:
<c:out value="${pageContext.request.requestURL}" />
<li>Server Name:
<c:out value="${pageContext.request.serverName}" />
<li>Server Port:
<c:out value="${pageContext.request.serverPort}" />
<li>Remote Address:
<c:out value="${pageContext.request.remoteAddr}" />
<li>Remote Host:
<c:out value="${pageContext.request.remoteHost}" />
<li>Secure:
<c:out value="${pageContext.request.secure}" />
<li>Cookies:<br>
<c:forEach items="${pageContext.request.cookies}" var="c">
<b><c:out value="${c.name}" /></b>:
<c:out value="${c.value}" /><br>
</c:forEach>
<li>Headers:<br>
<c:forEach items="${headerValues}" var="h">
<b><c:out value="${h.key}" /></b>:
<c:forEach items="${h.value}" var="value">
<br>
<c:out value="${value}" />
</c:forEach>
<br>
</c:forEach>
</ul>
</body>
</html>
pageContext隐含对象对应于javax.servlet.jsp.PageContext型态之对象,
隐含对象都自动的被加入至pageContext中,您可以藉由它来取得与JSP相关的隐含对象对应之Servlet对象
request:请求作用域,就是客户端的一次请求。
Request对象代表了客户端的请求信息(如请求的来源、
cookies和请求相关的参数值等等),
主要用于接受客户端通过HTTP协议传送给服务器端的数据。
accessed through the implicit pageContext variable's request property.
The request property is an instance
of a class named javax.servlet.http.HttpServletRequest
Properties for javax.servlet.http.HttpServletRequest | |||
Property name | Java type | Access | Description |
authType | String | Read | The name of the authentication scheme protecting the request |
characterEncoding | String | Read | The request body character encoding, or null if unknown |
contentLength | int | Read | The request body length, or -1 if unknown |
contentType | String | Read | The request body MIME type |
contextPath | String | Read | The context path for the request |
cookies | javax.servlet.http.Cookie[] | Read | The cookies received with the request |
locale | java.util.Locale | Read | The client's preferred locale |
locales | java.util.Enumeration | Read | A list of all client locales in order of preference |
method | String | Read | The request method (e.g., GET, POST) |
protocol | String | Read | The protocol name and version, e.g., HTTP/1.1 |
remoteAddr | String | Read | The client's IP address |
remoteHost | String | Read | The client's hostname or IP address if not known |
remoteUser | String | Read | The username used to make the request if the page is protected, otherwise null |
requestURI | String | Read | The request URI, e.g., /app/page.jsp |
requestURL | StringBuffer | Read | The request URL, e.g., http://server/app/page.jsp |
scheme | String | Read | The scheme, e.g., http or https |
servletPath | String | Read | The context-relative path for the request, e.g., /page.jsp |
serverName | String | Read | The name of the server the request was sent to |
serverPort | int | Read | The port the request was sent to |
secure | boolean | Read | true if the request was made over a secure channel (e.g., SSL) |
userPrincipal | java.security.Principal | Read | The Principal representing the user making the request if the page is protected, otherwise null |
对比代码:
<%
out.println("Protocol: " + request.getProtocol() + "<br>");
out.println("Scheme: " + request.getScheme() + "<br>");
out.println("Server Name: " + request.getServerName() + "<br>" );
out.println("Server Port: " + request.getServerPort() + "<br>");
out.println("Protocol: " + request.getProtocol() + "<br>");
out.println("Remote Addr: " + request.getRemoteAddr() + "<br>");
out.println("Remote Host: " + request.getRemoteHost() + "<br>");
out.println("Character Encoding: " + request.getCharacterEncoding() + "<br>");
out.println("Content Length: " + request.getContentLength() + "<br>");
out.println("Content Type: "+ request.getContentType() + "<br>");
out.println("Auth Type: " + request.getAuthType() + "<br>");
out.println("HTTP Method: " + request.getMethod() + "<br>");
out.println("Path Info: " + request.getPathInfo() + "<br>");
out.println("Path Trans: " + request.getPathTranslated() + "<br>");
out.println("Query String: " + request.getQueryString() + "<br>");
out.println("Remote User: " + request.getRemoteUser() + "<br>");
out.println("Session Id: " + request.getRequestedSessionId() + "<br>");
out.println("Request URI: " + request.getRequestURI() + "<br>");
out.println("Servlet Path: " + request.getServletPath() + "<br>");
out.println("Accept: " + request.getHeader("Accept") + "<br>");
out.println("Host: " + request.getHeader("Host") + "<br>");
out.println("Referer : " + request.getHeader("Referer") + "<br>");
out.println("Accept-Language : " + request.getHeader("Accept-Language") + "<br>");
out.println("Accept-Encoding : " + request.getHeader("Accept-Encoding") + "<br>");
out.println("User-Agent : " + request.getHeader("User-Agent") + "<br>");
out.println("Connection : " + request.getHeader("Connection") + "<br>");
out.println("Cookie : " + request.getHeader("Cookie") + "<br>");
%>