首先答案是:
會的。
本地測試流程:
兩個相同的應用,代碼完全相同;只是部署在兩個不同的tomcat;域名都是localhost
應用A:部署在http://localhost:8087/
應用B:部署在http://localhost:8089/
在intelj idea中很簡單,建立兩個不同的運行配置即可:
步驟1:清空所有cookie
步驟2、在8087發送請求,用chrome開發工具查看
可以看到,生成了cookie。
用我的cookie擴展(名字叫editThisCookie),可以看到該cookie貌似沒有區分端口號。
步驟3、在8089上測試一下
結論:可以看到,在請求8089端口的應用時,把8087應用返回的cookie帶過去了,所以標題的答案就是:
同域名情況下,cookie同名同路徑的話,會被覆蓋(路徑不同是否會覆蓋沒測試,有興趣的同學自己測下)
參考資料
http://blog.csdn.net/wangjun5159/article/details/52399497
附項目文件如下:
web.xml
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> <!-- 登錄過濾器 --> <filter> <filter-name>sessionFilter</filter-name> <filter-class>SessionFilter</filter-class> </filter> <filter-mapping> <filter-name>sessionFilter</filter-name> <url-pattern>*</url-pattern> </filter-mapping> </web-app>
import org.springframework.web.filter.OncePerRequestFilter; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; /** * Email: caokunliang@sgrcr.com * Date: 2017/4/6 * desc:登錄頁面不進該filter */ public class SessionFilter extends OncePerRequestFilter { protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException { String uri = httpServletRequest.getRequestURI(); System.out.println(uri); if (uri.contains("login.jsp") || uri.contains("index.jsp") || uri.contains("session")){ filterChain.doFilter(httpServletRequest,httpServletResponse); return; } HttpSession session = httpServletRequest.getSession(false); if (session == null){ //第一次訪問,沒帶sessionid;下面的操作會默認創建一個session空間,並寫cookie:jsessionid session = httpServletRequest.getSession(true); }else { //不是第一次訪問的話,已經有session空間了 System.out.println(session.getAttribute("SESSION")); } //判斷session空間中有沒有登錄標識 Boolean isLogin = (Boolean) session.getAttribute("isLogin"); if (isLogin != null && isLogin.equals(Boolean.TRUE)){ filterChain.doFilter(httpServletRequest,httpServletResponse); }else { //未登錄 httpServletResponse.sendRedirect("/login.jsp"); } } }
servlet:
import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; // tag::class[] @WebServlet("/session") public class SessionServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String attributeName = req.getParameter("attributeName"); String attributeValue = req.getParameter("attributeValue"); HttpSession session = req.getSession(false); if (session == null){ session = req.getSession(true); } System.out.println("SessionServlet" + session.getAttribute("SESSION")); if (attributeName.equals("ckl") && attributeValue.equals("123456")){ session.setAttribute("isLogin", Boolean.valueOf(true)); resp.sendRedirect(req.getContextPath() + "/index.jsp"); }else { resp.sendRedirect(req.getContextPath() + "/login.jsp"); } } private static final long serialVersionUID = 2878267318695777395L; }
index.jsp
<!DOCTYPE html> <html lang="en"> <title>Session Attributes</title> <style type="text/css"> body { padding: 1em; } </style> <head></head> <body> <div class="container"> <h1>Description</h1> <p>index.jsp</p> <h1>Try it</h1> <div>登錄成功后主頁</div> <hr/> <table class="table table-striped"> <thead> <tr> <th>Attribute Name</th> <th>Attribute Value</th> </tr> </thead> <tbody> </tbody> </table> </div> </body> </html>
login.jsp:
<!DOCTYPE html> <html lang="en"> <title>Session Attributes</title> <style type="text/css"> body { padding: 1em; } </style> <head></head> <body> <div class="container"> <h1>Description</h1> <p>login.jsp</p> <h1>Try it</h1> <form class="form-inline" role="form" action="./session" method="post"> <label for="attributeName">name</label> <input id="attributeName" type="text" name="attributeName"/> <label for="attributeValue">password</label> <input id="attributeValue" type="text" name="attributeValue"/> <input type="submit" value="Set Attribute"/> </form> <hr/> <table class="table table-striped"> <thead> <tr> <th>name</th> <th>password</th> </tr> </thead> <tbody> </tbody> </table> </div> </body> </html>