Notes for counters in Jsp tag JavaBean attribute drop

Source: Internet
Author: User

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 ..




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.