application对象
application对象负责提供应用程序在服务器中运行时的一些全局信息,常用的方法有getMimeType和getRealPath等。
运用实例:网页访问计数器。
<%@ 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 'Counter.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>
<%
if(application.getAttribute("counter") == null)
{
application.setAttribute("counter", "1");
}
else
{
String strnum = null;
strnum = application.getAttribute("counter").toString();
int icount = 0;
icount = Integer.valueOf(strnum).intValue();
icount++;
application.setAttribute("counter", Integer.toString(icount));
}
%>
您是第<%=application.getAttribute("counter") %>位访问者!
</body>
</html>
运行结果就是访问到该页面之后显示你是第几位访客,刷新之后数目会增加,更换浏览器或者更换客户端地址都会使其访问值正常递增。
application的存活范围比request和session都要大。
只要服务器没有关闭,application对象中的数据就会一直存在,在整个服务器的运行过程当中,application对象只有一个,它会被所有的用户共享。
其中getRealPath这个方法可以获取资源在服务器上的物理路径(绝对路径),常用来获取上传文件时要存储文件的路径。
Cookie
Cookie是存储在客户机的文本文件,它们保存了大量轨迹信息。在servlet技术基础上,JSP显然能够提供对HTTP cookie的支持。
通常有三个步骤来识别回头客:
- 服务器脚本发送一系列cookie至浏览器。比如名字,年龄,ID号码等等。
- 浏览器在本地机中存储这些信息,以备不时之需。
- 当下一次浏览器发送任何请求至服务器时,它会同时将这些cookie信息发送给服务器,然后服务器使用这些信息来识别用户或者干些其它事情。
本章节将会传授您如何去设置或重设cookie的方法,还有如何访问它们及如何删除它们。
JSP Cookie 处理需要对中文进行编码与解码,方法如下:
String str = java.net.URLEncoder.encode("中文","UTF-8"); //编码 String str = java.net.URLDecoder.decode("编码后的字符串","UTF-8"); // 解码
Cookie 剖析
Cookie通常在HTTP信息头中设置(虽然JavaScript能够直接在浏览器中设置cookie)。在JSP中,设置一个cookie需要发送如下的信息头给服务器:
HTTP/1.1 200 OK
Date: Fri, 04 Feb 2015 21:03:38 GMT
Server: Apache/1.3.9 (UNIX) PHP/4.0b3
Set-Cookie: name=runoob; expires=Friday, 04-Feb-07 22:03:38 GMT;
path=/; domain=runoob.com
Connection: close
Content-Type: text/html
正如您所见,Set-Cookie信息头包含一个键值对,一个GMT(格林尼治标准)时间,一个路径,一个域名。键值对会被编码为URL。有效期域是个指令,告诉浏览器在什么时候之后就可以清除这个cookie。
如果浏览器被配置成可存储cookie,那么它将会保存这些信息直到过期。如果用户访问的任何页面匹配了cookie中的路径和域名,那么浏览器将会重新将这个cookie发回给服务器。浏览器端的信息头长得就像下面这样:
GET / HTTP/1.0 Connection: Keep-Alive User-Agent: Mozilla/4.6 (X11; I; Linux 2.2.6-15apmac ppc) Host: zink.demon.co.uk:1126 Accept: image/gif, */* Accept-Encoding: gzip Accept-Language: en Accept-Charset: iso-8859-1,*,utf-8 Cookie: name=xyz
JSP脚本通过request对象中的getCookies()方法来访问这些cookie,这个方法会返回一个Cookie对象的数组。
Servlet Cookie 方法
下表列出了Cookie对象中常用的方法:
使用JSP设置cookie包含三个步骤:使用JSP设置Cookie
(1)创建一个Cookie对象: 调用Cookie的构造函数,使用一个cookie名称和值做参数,它们都是字符串。
Cookie cookie = new Cookie("key","value");
请务必牢记,名称和值中都不能包含空格或者如下的字符:
[ ] ( ) = , " / ? @ : ;
(2) 设置有效期:调用setMaxAge()函数表明cookie在多长时间(以秒为单位)内有效。下面的操作将有效期设为了24小时。
cookie.setMaxAge(60*60*24);
(3) 将cookie发送至HTTP响应头中:调用response.addCookie()函数来向HTTP响应头中添加cookie。
response.addCookie(cookie);
实例演示
main.jsp 文件代码如下所示:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.net.*" %>
<%
// 编码,解决中文乱码
String str = URLEncoder.encode(request.getParameter("name"),"utf-8");
// 设置 name 和 url cookie
Cookie name = new Cookie("name",
str);
Cookie url = new Cookie("url",
request.getParameter("url"));
// 设置cookie过期时间为24小时。
name.setMaxAge(60*60*24);
url.setMaxAge(60*60*24);
// 在响应头部添加cookie
response.addCookie( name );
response.addCookie( url );
%>
<html>
<head>
<title>设置 Cookie</title>
</head>
<body>
<h1>设置 Cookie</h1>
<ul>
<li><p><b>网站名:</b>
<%= request.getParameter("name")%>
</p></li>
<li><p><b>网址:</b>
<%= request.getParameter("url")%>
</p></li>
</ul>
</body>
</html>
以下是一个简单的 HTML 表单通过GET方法将客户端数据提交到 main.jsp 文件中,并设置 cookie:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> </head> <body> <form action="main.jsp" method=GET> 站点名: <input type="text" name="name"> <br /> 网址: <input type="text" name="url" /> <input type="submit" value="提交" /> </form> </body> </html>


浙公网安备 33010602011771号