| |

VerySource

 Forgot password?
 Register
Search
View: 702|Reply: 5

cookie application problem

[Copy link]

1

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-21 17:40:01
| Show all posts |Read mode
Initially contacted cookies, and wrote a small program according to the example in the book, but why does n’t cookie-AccessTimes reset to 1 each time I reset resetCookie.jsp? (Now accumulates 1 for each refresh)

The code is very simple, I will post it here in full.
resetCookie.jsp:
<% @ page language = "java"%>
<% @ page pageEncoding = "GB2312"%>
<html>
    <head>
        <title> jsp session tracking </ title>
    </ head>
    <body>
        <%
        response.addCookie (new Cookie ("AccessTimes", "1"));
        %>
        <jsp: forward page = "useCookie.jsp" />
    </ body>
</ html>

useCookie.jsp:
<% @ page pageEncoding = "GB2312" language = "java"%>
<html>
    <head>
        <title> cookie demo </ title>
    </ head>
    <body>
        <%
        Cookie [] cookies = request.getCookies ();
        String counter_S = null;
        int counter;
        if (cookies! = null)
        {
                for (int i = 0; i <cookies.length; i ++)
                {
                        if (cookies [i] .getName (). equals ("AccessTimes"))
                                counter_S = cookies [i] .getValue ();
                }
                try
                {
                                counter = Integer.parseInt (counter_S) +1;
                }
                catch (Exception ex)
                {
                        counter = 1;
                }
                response.addCookie (new Cookie ("AccessTimes", new Integer (counter) .toString ()));
                out.println ("You have visited the site for" + counter + "times!");
        }
        %>
    </ body>
</ html>
Reply

Use magic Report

0

Threads

12

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-1-31 14:00:02
| Show all posts
jsp: forward page = "useCookie.jsp";
The problem here is that the forward method will forcibly flush the buffer of the output stream, that is, the header set before and the output performed are not performed normally.
Reply

Use magic Report

1

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-2-9 16:00:02
| Show all posts
Looking at the documentation, the statement following jsp: forward will not be executed.
Moreover, there is no output statement before jsp: forward, response.addCookie is not an output. The document says that there will be an exception if there is an output before, but there is no exception in mine.
Reply

Use magic Report

1

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-2-9 20:15:01
| Show all posts
After searching on the Internet, there are indeed statements that reflect jsp: forward cannot be executed
Can you explain the prawns
Reply

Use magic Report

1

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-2-17 22:30:01
| Show all posts
The previous statement of jsp: forward could not be executed
Reply

Use magic Report

0

Threads

63

Posts

42.00

Credits

Newbie

Rank: 1

Credits
42.00

 China

Post time: 2020-2-20 22:00:01
| Show all posts
The simplest, change the redirect
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list