JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Servlet/JSP 討論區  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友   
reply to postflat modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 Cookie取值,會取到上一個設定的值
ttnezpwinf





發文: 20
積分: 0
於 2018-07-25 06:55 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
各位好,我在練習實作Cookie取值時,遇到了會取得上一個Cookie設定的值的問題,想請教各位如何解決

前端一支html有一個文字輸入欄位,提交給Servlet處理(接收參數、並建立新的Cookie),接著調派請求給另外一支jsp,jsp會取Cookie值,並且將值顯示在頁面上,但我遇到了都會取到上一個Cookie設定的值。

我的程式如下:

Exercise.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Exercise.html</title>
  </head>
  <body>
    <form method="post" action="CustomerCookieServlet.do">
      <p><input type="text" name="userName" size="30" maxlength="50" /></p>
      <p>
        <input type="submit" />
      </p>
    </form>
  </body>
</html>


CustomerCookieServlet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.example.cookie;
 
import java.io.IOException;
 
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 CustomerCookieServlet extends HttpServlet {
  private static final long serialVersionUID = 1;
       
    public CustomerCookieServlet() {
        super();
    }
 
    @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      response.setContentType("text/html");
      String userName = request.getParameter("userName");
      Cookie cookie = new Cookie("userName", userName);
      cookie.setMaxAge(2*60);
      response.addCookie(cookie);
      
      request.getRequestDispatcher("/jsp/checkCookie.jsp").forward(request, response);
    }
 
    @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doGet(request, response);
  }
 
}


checkCookie.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>checkCookie.jsp</title>
  </head>
  <body>
    ${cookie.userName.value} <br>
    <%
      Cookie[] cookies = request.getCookies();
      if(cookies != null){
        for(int i=0; i<cookies.length; i++){
          if(cookies[i].getName().equals("userName")){
            out.println("userName:" + cookies[i].getValue());
            break;
          }
        }
      }
    %>
<%="<a href=\"" + response.encodeURL("checkCookie.do") + "\">check me</a>" %>
  </body>
</html>


剛剛試著在checkCookie.jsp下,進行URL重寫,交給另外一支Servlet取Cookie設定的值,就會取到當次的值。
CheckCookieServlet.java 跟 checkCookie.jsp 這兩支在取Cookie值的Code一樣,我很好奇為何在.jsp取Cookie值時,都取到上一個所設定的值呢!?

CheckCookieServlet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.example.cookie;
 
import java.io.IOException;
import java.io.PrintWriter;
 
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 CheckCookieServlet extends HttpServlet {
  private static final long serialVersionUID = 1;
       
    public CheckCookieServlet() {
        super();
    }
 
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      response.setContentType("text/html");
      PrintWriter out = response.getWriter();
      Cookie[] cookies = request.getCookies();
      if(cookies != null){
        for(int i=0; i<cookies.length; i++){
          Cookie cookie = cookies[i];
          if(cookie.getName().equals("userName")){
            String userName = cookie.getValue();
            out.println("userName:" + userName);
            break;
          }
        }
      }
    }
 
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doGet(request, response);
  }
 
}
 


ttnezpwinf edited on 2018-07-25 10:18
reply to postreply to post
話題樹型展開
人氣 標題 作者 字數 發文時間
204 Cookie取值,會取到上一個設定的值 ttnezpwinf 4143 2018-07-25 06:55
» JWorld@TW »  Servlet/JSP 討論區

reply to postflat modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8