Today, when I was reading a book, I saw an example about Using Jsp labels to call JavaBean to implement the counter function. When I saw that the call sequence was obviously different from the final result, I did a small experiment, trace the call process in debug mode. The result proves that the call is wrong in the article. Well, there is not much nonsense. The following is the example code in the original article:
Counter. java
package cn.xd.model;public class Counter {private long counter;public long getCounter(){return counter;}public void setCounter(long counter){this.counter=counter+1;}}
Counter. jsp
<% if(session.isNew()){ long temp=counter.getCounter();
counter.setCounter(temp); } %> you are the
person.
First, let's talk about the problem after running. First, Let's explain the Jsp code. The main function is to Use Javabean to implement the counting function, when you use breakpoint debugging, you will find it in Counter. the getCounter method of java code reports an error, which is out of synch. After getCounter is executed, the program returns the error and does not execute counter in Jsp. setCounter (temp );
Now let's correct this code and try again.
Counter. java
package cn.xd.model;public class Counter {private long counter;public void setCounter(){counter++;}}
Counter. jsp
<% if(session.isNew()){ counter.setCounter(); } %> you are the
person.
This is the right result when you run it again.
Note: Firefox and Chrome are the browsers I use. When a single window is opened, no count is displayed. The reason is that the session lifecycle is also related to cookies. You must re-open the browser to count ..
Note:
In the web environment, you must pay attention to the bean scope and features when calling Javabean ..