ServletContext、Session、Cookie都可以用于存储数据,不过三者存储数据的位置及作用域不同。
由上图我们可以看出
ServletContext存储于服务端,每个客户端都可以访问到,数据共享。
Session存储于服务端,每个客户端有自己独立的数据区域。
Cookie存储于客户端。
下面通过代码来看一下三者的用法
package com.gujin.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.MessageFormat;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ContextServlet extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse response)
throws ServletException, IOException
{
ServletContext context = this.getServletConfig()
.getServletContext();
Integer count = (Integer) context.getAttribute("visit_count");
if (count == null)
{
count = 1;
}
else
{
count++;
}
context.setAttribute("visit_count", count);
// 设置响应内容类型
response.setContentType("text/html;charset=UTF-8");
PrintWriter writer = response.getWriter();
// 想输出流写的内容就是客户端接收到的内容
writer.print("<html>");
writer.print("<head>");
writer.print("<meta charset='UTF-8'>");
writer.print("</head>");
writer.print("<body>");
writer.print(MessageFormat.format("<h1>访问次数:{0}</h1>", count));
writer.print("</body>");
writer.print("<html>");
writer.flush();
writer.close();
}
}
package com.gujin.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.MessageFormat;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class SessionServlet extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
String flag = request.getParameter("flag");
HttpSession session = request.getSession();
if ("login".equals(flag))
{
session.setAttribute("login", "login");
}
else if ("logout".equals(flag))
{
session.removeAttribute("login");
}
PrintWriter writer = response.getWriter();
// 想输出流写的内容就是客户端接收到的内容
writer.print("<html>");
writer.print("<head>");
writer.print("<meta charset='UTF-8'>");
writer.print("</head>");
writer.print("<body>");
writer.print(MessageFormat.format("<h1>{0}</h1>",
session.getAttribute("login") == null ? "未登录" : "已登录"));
writer.print("</body>");
writer.print("<html>");
writer.flush();
writer.close();
}
}
package com.gujin.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.MessageFormat;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class CookieServlet extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
// 设置响应内容类型
response.setContentType("text/html;charset=UTF-8");
PrintWriter writer = response.getWriter();
// 想输出流写的内容就是客户端接收到的内容
writer.print("<html>");
writer.print("<head>");
writer.print("<meta charset='UTF-8'>");
writer.print("</head>");
writer.print("<body>");
writer.print("<ul>");
// 获得所有Cookie
Cookie[] cookies = request.getCookies();
if (cookies != null)
{
for (Cookie cookie : cookies)
{
writer.print(MessageFormat.format("<li>{0}:{1}</li>",
cookie.getName(), cookie.getValue()));
}
}
Cookie cookie = new Cookie("CookieServlet", System.currentTimeMillis() + "");
//添加Cookie
response.addCookie(cookie);
writer.print("</ul>");
writer.print("</body>");
writer.print("<html>");
writer.flush();
writer.close();
}
}
然后在web.xml
中进行配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Servlet</display-name>
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>com.gujin.servlet.ContextServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>context</servlet-name>
<url-pattern>/context</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>session</servlet-name>
<servlet-class>com.gujin.servlet.SessionServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>session</servlet-name>
<url-pattern>/session</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>cookie</servlet-name>
<servlet-class>com.gujin.servlet.CookieServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cookie</servlet-name>
<url-pattern>/cookie</url-pattern>
</servlet-mapping>
</web-app>
时间: 2024-09-10 03:22:13