| |

VerySource

 Forgot password?
 Register
Search
View: 719|Reply: 3

jdk5 + tomcat5.5 session lost

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-2 20:10:01
| Show all posts |Read mode
<% @ page language = "java" pageEncoding = "UTF-8"%>
<html>
  <head>
    <title> </ title>

  </ head>
  
  <body>
<%
Ranch
if ((String) session.getAttribute ("data") == "123");
{
System.out.println ("The value of data in the current session is 123");
}
else
{
session.setAttribute ("data", "123");
System.out.println ("The current session is empty, and the value of the set data is 123");
}
%>
Ranch
  </ body>
</ html>



The first time the result is:
The current session is empty, and the value of the set data is 123


If you refresh or press f5.
The result is still
The current session is empty, and the value of the set data is 123



why. Why is the session value lost?


No one else seems to be ... Is there a problem with tomcat? Or is there a problem with my code?
Who knows ..... ????
Reply

Use magic Report

0

Threads

12

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-1-3 23:24:01
| Show all posts
(String) session.getAttribute ("data") == "123";
String comparison cannot use equality, == compares whether the same object.
To (String) session.getAttribute ("data"). Equals ("123");
Reply

Use magic Report

0

Threads

39

Posts

23.00

Credits

Newbie

Rank: 1

Credits
23.00

 China

Post time: 2020-1-4 13:36:01
| Show all posts
if ("123" .equals ((String) session.getAttribute ("data")));
{
System.out.println ("The value of data in the current session is 123");
}
else
{
session.setAttribute ("data", "123");
System.out.println ("The current session is empty, and the value of the set data is 123");
}
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-7 00:03:01
| Show all posts
First study the difference between equals and "=="
The foundation is the most important
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