Skip to main content

Posts

Showing posts from April, 2012

Pass JSP variable as parameter to Javascript function

Two points which need to pay attention when applying: (1)  Use single quote for parameter in Javascript function e.g.  foo is a Javascript fcuntion; value is a JSP variable. onclick="foo(' <%=value %>');" (2)  Use  following Javascript function to encode String containing  special characters (e.g. ";"  "\r") You have 3 options: escape()  will not encode:  @*/+ encodeURI()  will not encode:  ~!@#$&*()=:/,;?+' encodeURIComponent()  will not encode:  ~!*()' References: http://stackoverflow.com/questions/332872/how-to-encode-a-url-in-javascript http://stackoverflow.com/questions/10033555/pass-a-jsp-variable-as-parameter-to-javascript-function http://stackoverflow.com/questions/4803906/jsp-variable-accssing-in-javascript

Is volatile variable thread-safe in Java

Not my writing Access to volatile int in Java will be thread-safe. When I say access I mean the unit operation over it, like volatile_var = 10 or int temp = volatile_var (basically write/read with constant values). Volatile keyword in java ensures two things : 1) When reading you always get the value in main memory. Generally for optimization purposes JVM use registers or in more general terms  local memory  foe storing/access variables. So in multi-threaded environment each thread may see different copy of variable. But making it private makes sure that write to variable is flushed to main memory and read to it also happens from main memory and hence making sure that thread see at right copy of variable. 2) Access to the volatile is automatically synchronized. So JVM ensures an ordering while read/write to the variable. However Jon Skeet mentions rightly that in non atomic operations (volatile_var = volatile + 1) different threads may get unexpected result. Reference:  http: