一、验证的问题:
父域设Cookie,子域取不到;
子域设Cookie,另一子域取不到;
子域Cookie,另一子域/父域取不到;
检验Cookie的作用域和路径属性在不同子域和相同域下的行为。
二、调用关系以及结果说明:
这些Java代码段代表一个用于处理HTTP请求的Servlet应用程序的部分。每个Java类(Servlet)和HTML页面都执行特定的功能,并且它们相互协作,主要用于测试和演示cookie的创建、获取和响应重定向。以下是每个类和页面的功能及其相互之间的调用关系:
Java Servlets (controller1
包)
Get
-
功能:获取当前请求中的所有cookie,并使用
CookieTools
工具类打印它们的名字和值。 -
调用关系:可以被直接访问。
PrintCookie
-
功能:获取请求参数
num
,打印所有cookie的名字和值,然后重定向到test<num>.html
。 -
调用关系:通过带参数的请求调用,
num
决定了重定向的目标页面。
PrintCookie7_1
, PrintCookie7_1_1
, PrintCookie_8_1
, PrintCookie_8_1_1
, PrintCookie_11
-
功能:这些类似似乎是创建cookie的尝试,但实际代码没有体现创建cookie的操作,只有重定向到
test7_8_9_10.html
。 -
调用关系:可以被直接访问。它们的URL模式是
/Save_7/1
、/Save_7/1/1
、/Save_8/1
、/Save_8/1/1
和/PrintCookie_11
。
Save_1
到 Save_10
-
功能:这些Servlet创建cookie,设置了不同的属性(如
domain
和path
),然后重定向到相应的HTML页面。例如,Save_1
没有设置domain
属性,而Save_2
设置了domain
为www.zc1.com
。 -
调用关系:每个Servlet都可以通过其URL模式直接访问。这些Servlet显示了不同
domain
和path
属性对cookie的影响。
Set
-
功能:创建一个名为
a1
值为a11
的cookie,设置最大存活时间为36000秒,没有重定向操作。 -
调用关系:可以被直接访问。
HTML页面
-
功能:提供指向不同Servlet的链接,用于触发cookie的创建、打印和重定向行为。
-
页面上的链接分别指向不同的
Save_x
和PrintCookie?num=x
URL,通过点击这些链接可以测试cookie的行为。
三、代码:
package controller1;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import tools.CookieTools;
import java.io.IOException;
/**
* @author ZhangChi
* @date 2023/11/16
*/
@WebServlet(urlPatterns = "/Get")
public class Get extends HttpServlet {//获取cookie
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
CookieTools cookieTools=new CookieTools(req,resp);
cookieTools.printNameValue();
}
}
package controller1;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import tools.CookieTools;
import java.io.IOException;
/**
* @author ZhangChi
* @date 2023/11/16
*/
@WebServlet(urlPatterns = "/PrintCookie")
public class PrintCookie extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String num=req.getParameter("num");
System.out.println("cookie1 PrintCookie run !");
CookieTools cookieTools=new CookieTools(req,resp);
cookieTools.printNameValue();
System.out.println();
resp.sendRedirect("test"+num+".html");
}
}
package controller1;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import tools.CookieTools;
import java.io.IOException;
/**
* @author ZhangChi
* @date 2023/11/16
*/
@WebServlet(urlPatterns = "/Save_7/1")
public class PrintCookie7_1 extends HttpServlet {//创建cookie
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("cookie1 PrintCookie run !");
CookieTools cookieTools=new CookieTools(req,resp);
cookieTools.printNameValue();
System.out.println();
resp.sendRedirect("http://zc1.8086/cookie1/test7_8_9_10.html");
}
}
package controller1;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import tools.CookieTools;
import java.io.IOException;
/**
* @author ZhangChi
* @date 2023/11/16
*/
@WebServlet(urlPatterns = "/Save_7/1/1")
public class PrintCookie7_1_1 extends HttpServlet {//创建cookie
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("cookie1 PrintCookie run !");
CookieTools cookieTools=new CookieTools(req,resp);
cookieTools.printNameValue();
System.out.println();
resp.sendRedirect("http://zc1.8086/cookie1/test7_8_9_10.html");
}
}
package controller1;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import tools.CookieTools;
import java.io.IOException;
/**
* @author ZhangChi
* @date 2023/11/16
*/
@WebServlet(urlPatterns = "/Save_8/1")
public class PrintCookie_8_1 extends HttpServlet {//创建cookie
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("cookie1 PrintCookie run !");
CookieTools cookieTools=new CookieTools(req,resp);
cookieTools.printNameValue();
System.out.println();
resp.sendRedirect("http://zc1.8086/cookie1/test7_8_9_10.html");
}
}
package controller1;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import tools.CookieTools;
import java.io.IOException;
/**
* @author ZhangChi
* @date 2023/11/16
*/
@WebServlet(urlPatterns = "/Save_8/1/1")
public class PrintCookie_8_1_1 extends HttpServlet {//创建cookie
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("cookie1 PrintCookie run !");
CookieTools cookieTools=new CookieTools(req,resp);
cookieTools.printNameValue();
System.out.println();
resp.sendRedirect("http://zc1.8086/cookie1/test7_8_9_10.html");
}
}
package controller1;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import tools.CookieTools;
import java.io.IOException;
/**
* @author ZhangChi
* @date 2023/11/16
*/
@WebServlet(urlPatterns = "/PrintCookie_11")
public class PrintCookie_11 extends HttpServlet {//创建cookie
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("cookie1 PrintCookie run !");
CookieTools cookieTools=new CookieTools(req,resp);
cookieTools.printNameValue();
System.out.println();
resp.sendRedirect("http://zc1.8086/cookie1/test7_8_9_10.html");
}
}
package controller1;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author ZhangChi
* @date 2023/11/16
*/
@WebServlet(urlPatterns = "/Save_1")
public class Save_1 extends HttpServlet {//创建cookie
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Cookie newCookie=new Cookie("a1","aa1");
newCookie.setMaxAge(36000);
resp.addCookie(newCookie);
//如果Cookie未设置domain,则Chrome会自动生成和其主机名(www)样的domain。
resp.sendRedirect("test1.html");
}
}
package controller1;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author ZhangChi
* @date 2023/11/16
*/
@WebServlet(urlPatterns = "/Save_2")
public class Save_2 extends HttpServlet {//创建cookie
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Cookie newCookie=new Cookie("a2","aa2");
newCookie.setDomain("www.zc1.com");
newCookie.setMaxAge(36000);
resp.addCookie(newCookie);
resp.sendRedirect("test2.html");
}
}
package controller1;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author ZhangChi
* @date 2023/11/16
*/
@WebServlet(urlPatterns = "/Save_3")
public class Save_3 extends HttpServlet {//创建cookie
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Cookie newCookie=new Cookie("a3","aa3");
newCookie.setDomain("zc1.com");
newCookie.setMaxAge(36000);
resp.addCookie(newCookie);
resp.sendRedirect("test3.html");
}
}
package controller1;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author ZhangChi
* @date 2023/11/16
*/
@WebServlet(urlPatterns = "/Save_4")
public class Save_4 extends HttpServlet {//创建cookie
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Cookie newCookie=new Cookie("a4","aa4");
newCookie.setDomain("blog.zc1.com");
newCookie.setMaxAge(36000);
resp.addCookie(newCookie);
resp.sendRedirect("test4.html");
}
}
package controller1;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author ZhangChi
* @date 2023/11/16
*/
@WebServlet(urlPatterns = "/Save_5")
public class Save_5 extends HttpServlet {//创建cookie
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Cookie newCookie=new Cookie("a5","aa5");
newCookie.setDomain("shop.zc1.com");
newCookie.setMaxAge(36000);
resp.addCookie(newCookie);
resp.sendRedirect("test5.html");
}
}
package controller1;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author ZhangChi
* @date 2023/11/16
*/
@WebServlet(urlPatterns = "/Save_6")
public class Save_6 extends HttpServlet {//创建cookie
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Cookie newCookie=new Cookie("a6","aa6");
newCookie.setDomain("user.shop.zc1.com");
newCookie.setMaxAge(36000);
resp.addCookie(newCookie);
resp.sendRedirect("test6.html");
}
}
package controller1;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author ZhangChi
* @date 2023/11/16
*/
@WebServlet(urlPatterns = "/Save_7")
public class Save_7 extends HttpServlet {//创建cookie
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Cookie newCookie=new Cookie("a7","aa7");
newCookie.setMaxAge(36000);
resp.addCookie(newCookie);
resp.sendRedirect("test7_8_9_10.html");
}
}
package controller1;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author ZhangChi
* @date 2023/11/16
*/
@WebServlet(urlPatterns = "/Save_8")
public class Save_8 extends HttpServlet {//创建cookie
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Cookie newCookie=new Cookie("a8","aa8");
newCookie.setPath("/");
newCookie.setMaxAge(36000);
resp.addCookie(newCookie);
resp.sendRedirect("test7_8_9_10.html");
}
}
package controller1;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author ZhangChi
* @date 2023/11/16
*/
@WebServlet(urlPatterns = "/Save_9")
public class Save_9 extends HttpServlet {//创建cookie
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Cookie newCookie=new Cookie("a9","aa9");
newCookie.setPath("/cookie1/Save_7");
newCookie.setMaxAge(36000);
resp.addCookie(newCookie);
resp.sendRedirect("test7_8_9_10.html");
}
}
package controller1;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author ZhangChi
* @date 2023/11/16
*/
@WebServlet(urlPatterns = "/Save_10")
public class Save_10 extends HttpServlet {//创建cookie
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Cookie newCookie=new Cookie("a10","aa10");
newCookie.setPath("/cookie1/Save_7/1");
newCookie.setMaxAge(36000);
resp.addCookie(newCookie);
resp.sendRedirect("test7_8_9_10.html");
}
}
package controller1;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author ZhangChi
* @date 2023/11/16
*/
@WebServlet(urlPatterns = "/Set")
public class Set extends HttpServlet {//创建cookie
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Cookie cookie=new Cookie("a1","a11");
cookie.setMaxAge(36000);
resp.addCookie(cookie);
System.out.println("创建Cookie完成");
}
}
package tools;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
/**
* @author ZhangChi
* @date 2023/11/16
*/
public class CookieTools {
private HttpServletRequest request;
private HttpServletResponse response;
public CookieTools(HttpServletRequest request, HttpServletResponse response) {
this.request = request;
this.response = response;
}
public void printNameValue() {
Cookie[] cookieArray = request.getCookies();
if (cookieArray != null) {
for (int i = 0; i < cookieArray.length; i++) {
String cookieName = cookieArray[i].getName();
String cookieValue = cookieArray[i].getValue();
String domain = cookieArray[i].getDomain();
String path = cookieArray[i].getPath();
System.out.println("cookieName=" + cookieName + "cookieValue=" + cookieValue);
// System.out.println("domain=" + domain + "path=" + path);
System.out.println();
}
}
// else {
// System.out.println("没有获得任何的Cookie");
// }
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<a href="http://zc1.com:8086/Save_1">http://zc1.com:8086/Save_1</a>
<br/>
<a href="http://www.zc1.com:8086/Save_1">http://www.zc1.com:8086/Save_1</a>
<br/>
<a href="http://blog.zc1.com:8086/Save_1">http://blog.zc1.com:8086/Save_1</a>
<br/>
<a href="http://shop.zc1.com:8086/Save_1">http://shop.zc1.com:8086/Save_1</a>
<br/>
<a href="http://user.shop.zc1.com:8086/Save_1">http://user.shop.zc1.com:8086/Save_1</a>
<br/>
<br/>
<br/>
<a href="http://zc1.com:8086/PrintCookie?num=1">http://zc1.com:8086/PrintCookie?num=1</a>
<br/>
<a href="http://www.zc1.com:8086/PrintCookie?num=1">http://www.zc1.com:8086/PrintCookie?num=1</a>
<br/>
<a href="http://blog.zc1.com:8086/PrintCookie?num=1">http://blog.zc1.com:8086/PrintCookie?num=1</a>
<br/>
<a href="http://shop.zc1.com:8086/PrintCookie?num=1">http://shop.zc1.com:8086/PrintCookie?num=1</a>
<br/>
<a href="http://user.shop.zc1.com:8086/PrintCookie?num=1">http://user.shop.zc1.com:8086/PrintCookie?num=1</a>
</body>
<!-- <body>-->
<!-- <a href="http://zc1.com:8086/cookie1/Save_1">http://zc1.com:8086/cookie1/Save_1</a>-->
<!-- <br/>-->
<!-- <a href="http://www.zc1.com:8086/cookie1/Save_1">http://www.zc1.com:8086/cookie1/Save_1</a>-->
<!-- <br/>-->
<!-- <a href="http://blog.zc1.com:8086/cookie1/Save_1">http://blog.zc1.com:8086/cookie1/Save_1</a>-->
<!-- <br/>-->
<!-- <a href="http://shop.zc1.com:8086/cookie1/Save_1">http://shop.zc1.com:8086/cookie1/Save_1</a>-->
<!-- <br/>-->
<!-- <a href="http://user.shop.zc1.com:8086/cookie1/Save_1">http://user.shop.zc1.com:8086/cookie1/Save_1</a>-->
<!-- <br/>-->
<!-- <br/>-->
<!-- <br/>-->
<!-- <a href="http://zc1.com:8086/cookie1/PrintCookie?num=1">http://zc1.com:8086/cookie1/PrintCookie?num=1</a>-->
<!-- <br/>-->
<!-- <a href="http://www.zc1.com:8086/cookie1/PrintCookie?num=1">http://www.zc1.com:8086/cookie1/PrintCookie?num=1</a>-->
<!-- <br/>-->
<!-- <a href="http://blog.zc1.com:8086/cookie1/PrintCookie?num=1">http://blog.zc1.com:8086/cookie1/PrintCookie?num=1</a>-->
<!-- <br/>-->
<!-- <a href="http://shop.zc1.com:8086/cookie1/PrintCookie?num=1">http://shop.zc1.com:8086/cookie1/PrintCookie?num=1</a>-->
<!-- <br/>-->
<!-- <a href="http://user.shop.zc1.com:8086/cookie1/PrintCookie?num=1">http://user.shop.zc1.com:8086/cookie1/PrintCookie?num=1</a>-->
<!-- </body>-->
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<a href="http://zc1.com:8086/Save_2">http://zc1.com:8086/Save_2</a>
<br/>
<a href="http://www.zc1.com:8086/Save_2">http://www.zc1.com:8086/Save_2</a>
<br/>
<a href="http://blog.zc1.com:8086/Save_2">http://blog.zc1.com:8086/Save_2</a>
<br/>
<a href="http://shop.zc1.com:8086/Save_2">http://shop.zc1.com:8086/Save_2</a>
<br/>
<a href="http://user.shop.zc1.com:8086/Save_2">http://user.shop.zc1.com:8086/Save_2</a>
<br/>
<br/>
<br/>
<a href="http://zc1.com:8086/PrintCookie?num=2">http://zc1.com:8086/PrintCookie?num=2</a>
<br/>
<a href="http://www.zc1.com:8086/PrintCookie?num=2">http://www.zc1.com:8086/PrintCookie?num=2</a>
<br/>
<a href="http://blog.zc1.com:8086/PrintCookie?num=2">http://blog.zc1.com:8086/PrintCookie?num=2</a>
<br/>
<a href="http://shop.zc1.com:8086/PrintCookie?num=2">http://shop.zc1.com:8086/PrintCookie?num=2</a>
<br/>
<a href="http://user.shop.zc1.com:8086/PrintCookie?num=2">http://user.shop.zc1.com:8086/PrintCookie?num=2</a>
</body>
<!-- <body>-->
<!-- <a href="http://ghy1.com:8085/cookie1/Save_2">http://ghy1.com:8085/cookie1/Save_2</a>-->
<!-- <br/>-->
<!-- <a href="http://www.ghy1.com:8085/cookie1/Save_2">http://www.ghy1.com:8085/cookie1/Save_2</a>-->
<!-- <br/>-->
<!-- <a href="http://blog.ghy1.com:8085/cookie1/Save_2">http://blog.ghy1.com:8085/cookie1/Save_2</a>-->
<!-- <br/>-->
<!-- <a href="http://shop.ghy1.com:8085/cookie1/Save_2">http://shop.ghy1.com:8085/cookie1/Save_2</a>-->
<!-- <br/>-->
<!-- <a href="http://user.shop.ghy1.com:8085/cookie1/Save_2">http://user.shop.ghy1.com:8085/cookie1/Save_2</a>-->
<!-- <br/>-->
<!-- <br/>-->
<!-- <br/>-->
<!-- <a href="http://ghy1.com:8085/cookie1/PrintCookie?num=2">http://ghy1.com:8085/cookie1/PrintCookie?num=2</a>-->
<!-- <br/>-->
<!-- <a href="http://www.ghy1.com:8085/cookie1/PrintCookie?num=2">http://www.ghy1.com:8085/cookie1/PrintCookie?num=2</a>-->
<!-- <br/>-->
<!-- <a href="http://blog.ghy1.com:8085/cookie1/PrintCookie?num=2">http://blog.ghy1.com:8085/cookie1/PrintCookie?num=2</a>-->
<!-- <br/>-->
<!-- <a href="http://shop.ghy1.com:8085/cookie1/PrintCookie?num=2">http://shop.ghy1.com:8085/cookie1/PrintCookie?num=2</a>-->
<!-- <br/>-->
<!-- <a href="http://user.shop.ghy1.com:8085/cookie1/PrintCookie?num=2">http://user.shop.ghy1.com:8085/cookie1/PrintCookie?num=2</a>-->
<!-- </body>-->
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head><!--由二而来,三替换二.计二十处-->
<body>
<a href="http://zc1.com:8086/Save_3">http://zc1.com:8086/Save_3</a>
<br/>
<a href="http://www.zc1.com:8086/Save_3">http://www.zc1.com:8086/Save_3</a>
<br/>
<a href="http://blog.zc1.com:8086/Save_3">http://blog.zc1.com:8086/Save_3</a>
<br/>
<a href="http://shop.zc1.com:8086/Save_3">http://shop.zc1.com:8086/Save_3</a>
<br/>
<a href="http://user.shop.zc1.com:8086/Save_3">http://user.shop.zc1.com:8086/Save_3</a>
<br/>
<br/>
<br/>
<a href="http://zc1.com:8086/PrintCookie?num=3">http://zc1.com:8086/PrintCookie?num=3</a>
<br/>
<a href="http://www.zc1.com:8086/PrintCookie?num=3">http://www.zc1.com:8086/PrintCookie?num=3</a>
<br/>
<a href="http://blog.zc1.com:8086/PrintCookie?num=3">http://blog.zc1.com:8086/PrintCookie?num=3</a>
<br/>
<a href="http://shop.zc1.com:8086/PrintCookie?num=3">http://shop.zc1.com:8086/PrintCookie?num=3</a>
<br/>
<a href="http://user.shop.zc1.com:8086/PrintCookie?num=3">http://user.shop.zc1.com:8086/PrintCookie?num=3</a>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head><!--由二而来,三替换二.计二十处-->
<body>
<a href="http://zc1.com:8086/Save_4">http://zc1.com:8086/Save_4</a>
<br/>
<a href="http://www.zc1.com:8086/Save_4">http://www.zc1.com:8086/Save_4</a>
<br/>
<a href="http://blog.zc1.com:8086/Save_4">http://blog.zc1.com:8086/Save_4</a>
<br/>
<a href="http://shop.zc1.com:8086/Save_4">http://shop.zc1.com:8086/Save_4</a>
<br/>
<a href="http://user.shop.zc1.com:8086/Save_4">http://user.shop.zc1.com:8086/Save_4</a>
<br/>
<br/>
<br/>
<a href="http://zc1.com:8086/PrintCookie?num=4">http://zc1.com:8086/PrintCookie?num=4</a>
<br/>
<a href="http://www.zc1.com:8086/PrintCookie?num=4">http://www.zc1.com:8086/PrintCookie?num=4</a>
<br/>
<a href="http://blog.zc1.com:8086/PrintCookie?num=4">http://blog.zc1.com:8086/PrintCookie?num=4</a>
<br/>
<a href="http://shop.zc1.com:8086/PrintCookie?num=4">http://shop.zc1.com:8086/PrintCookie?num=4</a>
<br/>
<a href="http://user.shop.zc1.com:8086/PrintCookie?num=4">http://user.shop.zc1.com:8086/PrintCookie?num=4</a>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head><!--由二而来,三替换二.计二十处-->
<body>
<a href="http://zc1.com:8086/Save_5">http://zc1.com:8086/Save_5</a>
<br/>
<a href="http://www.zc1.com:8086/Save_5">http://www.zc1.com:8086/Save_5</a>
<br/>
<a href="http://blog.zc1.com:8086/Save_5">http://blog.zc1.com:8086/Save_5</a>
<br/>
<a href="http://shop.zc1.com:8086/Save_5">http://shop.zc1.com:8086/Save_5</a>
<br/>
<a href="http://user.shop.zc1.com:8086/Save_5">http://user.shop.zc1.com:8086/Save_5</a>
<br/>
<br/>
<br/>
<a href="http://zc1.com:8086/PrintCookie?num=5">http://zc1.com:8086/PrintCookie?num=5</a>
<br/>
<a href="http://www.zc1.com:8086/PrintCookie?num=5">http://www.zc1.com:8086/PrintCookie?num=5</a>
<br/>
<a href="http://blog.zc1.com:8086/PrintCookie?num=5">http://blog.zc1.com:8086/PrintCookie?num=5</a>
<br/>
<a href="http://shop.zc1.com:8086/PrintCookie?num=5">http://shop.zc1.com:8086/PrintCookie?num=5</a>
<br/>
<a href="http://user.shop.zc1.com:8086/PrintCookie?num=5">http://user.shop.zc1.com:8086/PrintCookie?num=5</a>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head><!--由二而来,三替换二.计二十处-->
<body>
<a href="http://zc1.com:8086/Save_6">http://zc1.com:8086/Save_6</a>
<br/>
<a href="http://www.zc1.com:8086/Save_6">http://www.zc1.com:8086/Save_6</a>
<br/>
<a href="http://blog.zc1.com:8086/Save_6">http://blog.zc1.com:8086/Save_6</a>
<br/>
<a href="http://shop.zc1.com:8086/Save_6">http://shop.zc1.com:8086/Save_6</a>
<br/>
<a href="http://user.shop.zc1.com:8086/Save_6">http://user.shop.zc1.com:8086/Save_6</a>
<br/>
<br/>
<br/>
<a href="http://zc1.com:8086/PrintCookie?num=6">http://zc1.com:8086/PrintCookie?num=6</a>
<br/>
<a href="http://www.zc1.com:8086/PrintCookie?num=6">http://www.zc1.com:8086/PrintCookie?num=6</a>
<br/>
<a href="http://blog.zc1.com:8086/PrintCookie?num=6">http://blog.zc1.com:8086/PrintCookie?num=6</a>
<br/>
<a href="http://shop.zc1.com:8086/PrintCookie?num=6">http://shop.zc1.com:8086/PrintCookie?num=6</a>
<br/>
<a href="http://user.shop.zc1.com:8086/PrintCookie?num=6">http://user.shop.zc1.com:8086/PrintCookie?num=6</a>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<a href="http://zc1.com:8086/Save_7">http://zc1.com:8086/Save_7</a>
<br/>
<a href="http://zc1.com:8086/Save_8">http://zc1.com:8086/Save_8</a>
<br/>
<a href="http://zc1.com:8086/Save_9">http://zc1.com:8086/Save_9</a>
<br/>
<a href="http://zc1.com:8086/Save_10">http://zc1.com:8086/Save_10</a>
<br/>
<br/>
<a href="http://zc1.com:8086/Save_7/1">http://zc1.com:8086/Save_7/1</a>
<br/>
<a href="http://zc1.com:8086/Save_7/1/1">http://zc1.com:8086/Save_7/1/1</a>
<br/>
<a href="http://zc1.com:8086/Save_8/1">http://zc1.com:8086/Save_8/1</a>
<br/>
<a href="http://zc1.com:8086/Save_8/1/1">http://zc1.com:8086/Save_8/1/1</a>
<br/>
<a href="http://zc1.com:8086/PrintCookie_11">http://zc1.com:8086/PrintCookie_11</a>
</body>
</html>
四、总结
-
Save_x
类用于创建cookie并测试cookie的属性(如domain
和path
)。 -
PrintCookie
和类似命名的Servlets用于打印cookie的信息并执行重定向操作。 -
HTML页面提供了用户界面,用于通过点击链接来触发上述Servlet的操作。
测试目的
-
域名作用范围测试: 通过
Save_2
、Save_3
等Servlet设置不同的domain
属性,来测试cookie在不同子域下的行为。 -
路径作用范围测试: 通过
Save_9
和Save_10
设置不同的path
属性,来测试cookie在特定路径下的有效性。 -
跨域测试: 通过设置不同的
domain
属性并重定向到不同的域(如Save_2
),来测试浏览器如何处理跨域的cookie。 -
持久化测试: 通过
setMaxAge
来设置cookie的存活时间,测试cookie的持久化。 -
获取和打印cookie:
Get
和PrintCookie
等类用于测试获取当前请求中的cookie并打印它们的信息。
每个Servlet和页面都是测试cookie在web开发中不同方面的行为的一个组成部分。
根据代码中的不同网址分析,具体在测什么样的测试
根据代码和网址的不同,这个测试似乎在探索和演示以下几个方面:
- Cookie的域属性(Domain Attribute):
-
通过设置
domain
为www.zc1.com
,zc1.com
,blog.zc1.com
,shop.zc1.com
, 和user.shop.zc1.com
,代码在测试cookie如何在相同的顶级域名下的不同子域间共享。 -
例如,
Save_2
和Save_3
中的cookie将会在zc1.com
和它的所有子域中可用。
- Cookie的路径属性(Path Attribute):
-
通过指定
path
属性,代码在测试cookie的可见性是否限制于特定的路径。 -
例如,
Save_9
和Save_10
设置了路径/cookie1/Save_7
,这意味着这些cookie仅在该路径及其子路径下可用。
- Cookie的持久性(Persistence):
-
通过设置
setMaxAge
,测试可以设置cookie的生存周期,这里设置为36000秒(10小时)。 -
例如,
Save_1
到Save_10
中的cookie都设置了相同的最大年龄,测试了cookie的持久性。
- 跨域重定向(Cross-Domain Redirection):
- 代码在重定向时使用了完整的URL(例如,在
PrintCookie7_1
中http://zc1.8086/cookie1/test7_8_9_10.html
),这暗示了测试可能涉及跨域行为,尤其是如何处理cookie在重定向后的行为。
- Cookie的获取和打印(Retrieval and Output):
Get
,PrintCookie
, 和其他相关的Servlet类被用于测试系统如何检索当前请求的cookie并打印它们的名字和值。
- 测试网站的响应(Response Testing):
- 通过点击HTML页面上的链接,测试web应用如何响应并处理不同的GET请求,并据此设置cookie。
通过这些测试,开发者可以验证web应用在处理cookie时是否符合预期的安全性、可访问性和持久性。这些测试覆盖了cookie管理的基础知识,是web开发和维护中常见的实践。
五、疑难解答
为什么user.shop.zc1.com设置的cookie,shop.zc1.com可以获取到
域名:shop.zc1.com
但是blog.zc1.com设置的cookie,zc1.com获取不到呢
域名:blog.zc1.com
域名shop.zc1.com和blog.zc1.com相当于通配符*作用