Commit a7dc6914 by shj

解决了Cooikie不一致的问题:多个Cookie重名,原因是Cookie的唯一性由多个因素决定,一般包括名称、域名、路径等,删除某个cookie一般都是设置写…

解决了Cooikie不一致的问题:多个Cookie重名,原因是Cookie的唯一性由多个因素决定,一般包括名称、域名、路径等,删除某个cookie一般都是设置写入同名cookie并设置该cookie存活时间为0,这个过程中还要保证写入的cookie的路径、域名等属性与要删除的cookie一致
parent 8b0d1c3a
...@@ -50,10 +50,10 @@ public class BudgetHandler { ...@@ -50,10 +50,10 @@ public class BudgetHandler {
//以时间为标志,标志预算对象,用以鉴别不同用户 //以时间为标志,标志预算对象,用以鉴别不同用户
long sessionID = new Date().getTime(); long sessionID = new Date().getTime();
Cookie cookie = new Cookie("sessionID", Long.toString(sessionID)); /*Cookie cookie = new Cookie("sessionID", Long.toString(sessionID));
cookie.setMaxAge(Integer.MAX_VALUE); cookie.setPath(request.getContextPath());
cookie.setComment("会话鉴别,age=int_max,除非重新生成预算,否则长时间保持会话"); cookie.setComment("会话鉴别,age=int_max,除非重新生成预算,否则长时间保持会话");
response.addCookie(cookie); response.addCookie(cookie);*/
Budget budget = new Budget(); Budget budget = new Budget();
budget.setId(sessionID); budget.setId(sessionID);
...@@ -118,8 +118,8 @@ public class BudgetHandler { ...@@ -118,8 +118,8 @@ public class BudgetHandler {
serializeBudget(budget, filePath); serializeBudget(budget, filePath);
Cookie useridCookie = CookieUtil.getCookieByName(request.getCookies(), "userid"); Cookie useridCookie = CookieUtil.getCookieByName(request.getCookies(), "userid");
Integer userid = Integer.valueOf(useridCookie.getValue()); Integer userid = Integer.valueOf(useridCookie.getValue());
System.out.println("get userid "+userid); System.out.println("get userid " + userid);
userBudgetService.addUserBudget(userid,budget.getId()); userBudgetService.addUserBudget(userid, budget.getId());
//response.setHeader("content-disposition", "attachment;filename=Budget" + sessionID + ".csv"); //response.setHeader("content-disposition", "attachment;filename=Budget" + sessionID + ".csv");
System.out.println("ContextPath: " + request.getContextPath()); System.out.println("ContextPath: " + request.getContextPath());
...@@ -186,61 +186,66 @@ public class BudgetHandler { ...@@ -186,61 +186,66 @@ public class BudgetHandler {
} }
@RequestMapping("/Detail/{budgetId}") @RequestMapping("/Detail/{budgetId}")
public ModelAndView budgetDetailById(@PathVariable("budgetId") Long budgetId,HttpServletRequest request, HttpServletResponse response) { public ModelAndView budgetDetailById(@PathVariable("budgetId") Long budgetId, HttpServletRequest request, HttpServletResponse response) {
if (budgetId == null) return new ModelAndView("/"); if (budgetId == null) return new ModelAndView("/");
ModelAndView modelAndView = new ModelAndView(); ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("/budgetDetail.jsp"); modelAndView.setViewName("/budgetDetail.jsp");
modelAndView.addObject("budget", retrieveBudget(budgetId.toString())); modelAndView.addObject("budget", retrieveBudget(budgetId.toString()));
Cookie sessionID = CookieUtil.getCookieByName(request.getCookies(), "sessionID"); Cookie sessionID = CookieUtil.getCookieByName(request.getCookies(), "sessionID");
sessionID.setValue(budgetId.toString()); if (sessionID == null) {
sessionID = new Cookie("sessionID", budgetId.toString());
} else {
sessionID.setValue(budgetId.toString());
}
sessionID.setPath("/");
response.addCookie(sessionID); response.addCookie(sessionID);
return modelAndView; return modelAndView;
} }
@RequestMapping("/Delete/{budgetId}") @RequestMapping("/Delete/{budgetId}")
public void deleteBudgetById(@PathVariable("budgetId") Long budgetId,HttpServletRequest request, HttpServletResponse response) { public void deleteBudgetById(@PathVariable("budgetId") Long budgetId, HttpServletRequest request, HttpServletResponse response) {
if (budgetId == null) return ; if (budgetId == null) return;
Cookie useridCookie = CookieUtil.getCookieByName(request.getCookies(), "userid"); Cookie useridCookie = CookieUtil.getCookieByName(request.getCookies(), "userid");
Integer userid = Integer.valueOf(useridCookie.getValue()); Integer userid = Integer.valueOf(useridCookie.getValue());
//删除数据库记录 //删除数据库记录
userBudgetService.deleteUserBudget(userid,budgetId); userBudgetService.deleteUserBudget(userid, budgetId);
//删除文件 //删除文件
String filePath = getFilePath(budgetId.toString()); String filePath = getFilePath(budgetId.toString());
File budgetFile=new File(filePath); File budgetFile = new File(filePath);
if(budgetFile.delete()){ if (budgetFile.delete()) {
System.out.println("成功删除文件"); System.out.println("成功删除文件");
} }
} }
@RequestMapping("/HistoryPage") @RequestMapping("/HistoryPage")
public ModelAndView historyPage(HttpServletRequest request,HttpServletResponse response){ public ModelAndView historyPage(HttpServletRequest request, HttpServletResponse response) {
ModelAndView modelAndView=new ModelAndView(); ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("/history.jsp"); modelAndView.setViewName("/history.jsp");
return modelAndView; return modelAndView;
} }
@RequestMapping("/HistoryList") @RequestMapping("/HistoryList")
public void historyList(HttpServletRequest request,HttpServletResponse response){ public void historyList(HttpServletRequest request, HttpServletResponse response) {
try { try {
response.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8"); response.setContentType("text/html;charset=utf-8");
PrintWriter writer = response.getWriter(); PrintWriter writer = response.getWriter();
JSONObject object=new JSONObject(); JSONObject object = new JSONObject();
//String sessionID = BudgetHandler.getSessionID(request.getCookies()); //String sessionID = BudgetHandler.getSessionID(request.getCookies());
Cookie useridCookie = CookieUtil.getCookieByName(request.getCookies(), "userid"); Cookie useridCookie = CookieUtil.getCookieByName(request.getCookies(), "userid");
Integer userid = Integer.valueOf(useridCookie.getValue()); Integer userid = Integer.valueOf(useridCookie.getValue());
List<Long> budgetList=userBudgetService.getBudgetByUserid(userid); List<Long> budgetList = userBudgetService.getBudgetByUserid(userid);
List<JSONObject> list=new LinkedList<>(); List<JSONObject> list = new LinkedList<>();
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
for (Long budget : budgetList) { for (Long budget : budgetList) {
JSONObject obj=new JSONObject(); JSONObject obj = new JSONObject();
obj.put("id",budget); obj.put("id", budget);
Date date=new Date(budget); Date date = new Date(budget);
obj.put("date",format.format(date)); obj.put("date", format.format(date));
list.add(obj); list.add(obj);
} }
object.put("data",list); object.put("data", list);
writer.write(JSON.toJSONString(object)); writer.write(JSON.toJSONString(object));
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
...@@ -565,6 +570,7 @@ public class BudgetHandler { ...@@ -565,6 +570,7 @@ public class BudgetHandler {
/** /**
* 修改预算中的劳务费、规则中的劳务费 * 修改预算中的劳务费、规则中的劳务费
*
* @param mode * @param mode
* @param consultation * @param consultation
* @param nums * @param nums
......
...@@ -7,6 +7,7 @@ import org.springframework.ui.Model; ...@@ -7,6 +7,7 @@ import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.ModelAndView;
import service.IUserService; import service.IUserService;
import util.CookieUtil;
import javax.jws.soap.SOAPBinding; import javax.jws.soap.SOAPBinding;
import javax.servlet.http.Cookie; import javax.servlet.http.Cookie;
...@@ -33,8 +34,12 @@ public class LoginHandler { ...@@ -33,8 +34,12 @@ public class LoginHandler {
public void loginAction(User user,HttpServletRequest request, HttpServletResponse response) throws IOException { public void loginAction(User user,HttpServletRequest request, HttpServletResponse response) throws IOException {
if(userService.checkUser(user)) if(userService.checkUser(user))
{ {
response.addCookie(new Cookie("userid",user.getId().toString())); Cookie useridCookie = new Cookie("userid", user.getId().toString());
response.addCookie(new Cookie("username",user.getName())); useridCookie.setPath("/");
response.addCookie(useridCookie);
Cookie usernameCookie = new Cookie("username", user.getName());
usernameCookie.setPath("/");
response.addCookie(usernameCookie);
//response.sendRedirect(request.getContextPath()+"/"); //response.sendRedirect(request.getContextPath()+"/");
System.out.println("back to index page"); System.out.println("back to index page");
} }
...@@ -57,8 +62,12 @@ public class LoginHandler { ...@@ -57,8 +62,12 @@ public class LoginHandler {
return; return;
} }
userService.addUser(user); userService.addUser(user);
response.addCookie(new Cookie("userid",user.getId().toString())); Cookie useridCookie = new Cookie("userid", user.getId().toString());
response.addCookie(new Cookie("username",user.getName())); useridCookie.setPath("/");
response.addCookie(useridCookie);
Cookie usernameCookie = new Cookie("username", user.getName());
usernameCookie.setPath("/");
response.addCookie(usernameCookie);
System.out.println("register done"); System.out.println("register done");
} }
...@@ -70,13 +79,17 @@ public class LoginHandler { ...@@ -70,13 +79,17 @@ public class LoginHandler {
*/ */
@RequestMapping("/Logout") @RequestMapping("/Logout")
public void logoutAction(HttpServletRequest request,HttpServletResponse response) throws IOException { public void logoutAction(HttpServletRequest request,HttpServletResponse response) throws IOException {
Cookie cookie=new Cookie("userid",null); //Cookie cookie=new Cookie("userid",null);
Cookie cookie= CookieUtil.getCookieByName(request.getCookies(),"userid");
cookie.setMaxAge(0); cookie.setMaxAge(0);
cookie.setPath(request.getContextPath()); cookie.setPath("/");
System.out.println("Userid cookie:"+cookie.getPath());
response.addCookie(cookie); response.addCookie(cookie);
cookie=new Cookie("username",null); //cookie=new Cookie("username",null);
cookie=CookieUtil.getCookieByName(request.getCookies(),"username");
cookie.setMaxAge(0); cookie.setMaxAge(0);
cookie.setPath(request.getContextPath()); cookie.setPath("/");
System.out.println("Username cookie:"+cookie.getPath());
response.addCookie(cookie); response.addCookie(cookie);
response.sendRedirect(request.getContextPath()); response.sendRedirect(request.getContextPath());
} }
......
...@@ -59,7 +59,7 @@ ...@@ -59,7 +59,7 @@
<li><a href="${pageContext.request.contextPath}/Rule/">修改规则</a></li> <li><a href="${pageContext.request.contextPath}/Rule/">修改规则</a></li>
<li><a href="${pageContext.request.contextPath}/Budget/Download">导出最新预算</a></li> <li><a href="${pageContext.request.contextPath}/Budget/Download">导出最新预算</a></li>
<li><a href="${pageContext.request.contextPath}/Test">测试</a></li> <li><a href="${pageContext.request.contextPath}/Test">测试</a></li>
<li><a href="${pageContext.request.contextPath}/Logout">注销</a></li> <li><a href="${pageContext.request.contextPath}/usercenter.jsp">用户中心</a></li>
</ul> </ul>
</div> </div>
</div> </div>
......
...@@ -49,7 +49,7 @@ ...@@ -49,7 +49,7 @@
<li><a href="${pageContext.request.contextPath}/Rule/">修改规则</a></li> <li><a href="${pageContext.request.contextPath}/Rule/">修改规则</a></li>
<li><a href="${pageContext.request.contextPath}/Budget/Download">导出最新预算</a></li> <li><a href="${pageContext.request.contextPath}/Budget/Download">导出最新预算</a></li>
<li><a href="${pageContext.request.contextPath}/Test">测试</a></li> <li><a href="${pageContext.request.contextPath}/Test">测试</a></li>
<li><a href="${pageContext.request.contextPath}/Logout">注销</a></li> <li><a href="${pageContext.request.contextPath}/usercenter.jsp">用户中心</a></li>
</ul> </ul>
</div> </div>
</div> </div>
......
...@@ -30,7 +30,7 @@ ...@@ -30,7 +30,7 @@
<nav class="navbar navbar-default navbar-fixed-top" role="navigation"> <nav class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container-fluid"> <div class="container-fluid">
<div class="navbar-header"> <div class="navbar-header">
<a class="navbar-brand" href="#">预算辅助管理系统</a> <a class="navbar-brand" href="${pageContext.request.contextPath}/">预算辅助管理系统</a>
</div> </div>
<div> <div>
<ul class="nav navbar-nav"> <ul class="nav navbar-nav">
...@@ -40,7 +40,7 @@ ...@@ -40,7 +40,7 @@
<li><a href="${pageContext.request.contextPath}/Rule/">修改规则</a></li> <li><a href="${pageContext.request.contextPath}/Rule/">修改规则</a></li>
<li><a href="${pageContext.request.contextPath}/Budget/Download">导出最新预算</a></li> <li><a href="${pageContext.request.contextPath}/Budget/Download">导出最新预算</a></li>
<li><a href="${pageContext.request.contextPath}/Test">测试</a></li> <li><a href="${pageContext.request.contextPath}/Test">测试</a></li>
<li><a href="${pageContext.request.contextPath}/Logout">注销</a></li> <li><a href="${pageContext.request.contextPath}/usercenter.jsp">用户中心</a></li>
</ul> </ul>
</div> </div>
</div> </div>
......
...@@ -70,17 +70,21 @@ ...@@ -70,17 +70,21 @@
register:function () { register:function () {
console.log(this.user.name); console.log(this.user.name);
console.log(this.user.password); console.log(this.user.password);
this.$http.post("${pageContext.request.contextPath}/Register", if(this.user.name==="" || this.user.password===""){
{ document.getElementById("hint").innerText="用户名与密码不能为空";
name:this.user.name, }else{
password:this.user.password this.$http.post("${pageContext.request.contextPath}/Register",
}, {
{emulateJSON: true} name:this.user.name,
).then(function (value) { password:this.user.password
location.reload(); },
},function (reason) { {emulateJSON: true}
document.getElementById("hint").innerText="用户名已存在"; ).then(function (value) {
}); location.reload();
},function (reason) {
document.getElementById("hint").innerText="用户名已存在";
});
}
} }
} }
}); });
......
...@@ -50,7 +50,7 @@ ...@@ -50,7 +50,7 @@
<li class="active"><a href="${pageContext.request.contextPath}/Rule/">修改规则</a></li> <li class="active"><a href="${pageContext.request.contextPath}/Rule/">修改规则</a></li>
<li><a href="${pageContext.request.contextPath}/Budget/Download">导出最新预算</a></li> <li><a href="${pageContext.request.contextPath}/Budget/Download">导出最新预算</a></li>
<li><a href="${pageContext.request.contextPath}/Test">测试</a></li> <li><a href="${pageContext.request.contextPath}/Test">测试</a></li>
<li><a href="${pageContext.request.contextPath}/Logout">注销</a></li> <li><a href="${pageContext.request.contextPath}/usercenter.jsp">用户中心</a></li>
</ul> </ul>
</div> </div>
</div> </div>
......
...@@ -51,7 +51,7 @@ ...@@ -51,7 +51,7 @@
<li><a href="${pageContext.request.contextPath}/Rule/">修改规则</a></li> <li><a href="${pageContext.request.contextPath}/Rule/">修改规则</a></li>
<li><a href="${pageContext.request.contextPath}/Budget/Download">导出最新预算</a></li> <li><a href="${pageContext.request.contextPath}/Budget/Download">导出最新预算</a></li>
<li class="active"><a href="${pageContext.request.contextPath}/Test">测试</a></li> <li class="active"><a href="${pageContext.request.contextPath}/Test">测试</a></li>
<li><a href="${pageContext.request.contextPath}/Logout">注销</a></li> <li><a href="${pageContext.request.contextPath}/usercenter.jsp">用户中心</a></li>
</ul> </ul>
</div> </div>
</div> </div>
...@@ -99,6 +99,16 @@ ${pageContext.request.contextPath}<br> ...@@ -99,6 +99,16 @@ ${pageContext.request.contextPath}<br>
<p>{{num}}</p> <p>{{num}}</p>
</div> </div>
<button class="btn btn-danger" onclick="logout()">退出登录</button>
<script type="text/javascript">
function logout() {
if(confirm("确认退出登录?")){
window.location.href="${pageContext.request.contextPath}/Logout";
}
}
</script>
<script type="text/javascript"> <script type="text/javascript">
angular.module('myApp', []).controller('ctrl', function ($scope, $http) { angular.module('myApp', []).controller('ctrl', function ($scope, $http) {
......
<%--
Created by IntelliJ IDEA.
User: Song
Date: 2019/5/13
Time: 11:19
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>用户中心</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- 可选的 Bootstrap 主题文件(一般不用引入) -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap-theme.min.css"
integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"
integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<%--<script type="text/javascript" src="${pageContext.request.contextPath}/js/modifyDetail.js"></script>--%>
<style type="text/css">
body {
padding-top: 70px;
}
</style>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://cdn.staticfile.org/vue-resource/1.5.1/vue-resource.min.js"></script>
</head>
<body>
<nav class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="${pageContext.request.contextPath}/">预算辅助管理系统</a>
</div>
<div>
<ul class="nav navbar-nav">
<li><a href="${pageContext.request.contextPath}/">创建预算</a></li>
<li><a href="${pageContext.request.contextPath}/Budget/HistoryPage">历史预算</a></li>
<li><a href="${pageContext.request.contextPath}/Budget/Detail">修改预算</a></li>
<li><a href="${pageContext.request.contextPath}/Rule/">修改规则</a></li>
<li><a href="${pageContext.request.contextPath}/Budget/Download">导出最新预算</a></li>
<li><a href="${pageContext.request.contextPath}/Test">测试</a></li>
<li class="active"><a href="${pageContext.request.contextPath}/usercenter.jsp">用户中心</a></li>
</ul>
</div>
</div>
</nav>
<div>
<h1>你好!<label id="username" style="color:green;"></label></h1>
<button class="btn btn-danger" onclick="logout()">退出登录</button>
</div>
<script type="text/javascript">
var username = get_cookie("username");
document.getElementById("username").innerText = username;
function logout() {
if (confirm(username + "确认退出登录?")) {
window.location.href = "${pageContext.request.contextPath}/Logout";
}
}
function get_cookie(Name) {
var search = Name + "="//查询检索的值
var returnvalue = "";//返回值
if (document.cookie.length > 0) {
sd = document.cookie.indexOf(search);
if (sd != -1) {
sd += search.length;
end = document.cookie.indexOf(";", sd);
if (end == -1)
end = document.cookie.length;
//unescape() 函数可对通过 escape() 编码的字符串进行解码。
returnvalue = unescape(document.cookie.substring(sd, end))
}
}
return returnvalue;
}
</script>
</body>
</html>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment