Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Between declaration tag and scriptlet which one is threadsafe?

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all
recently i attended an interview and the interviewer asked me :
Between declaration tag and scriptlet tag which one is threadsafe? and why?


thanks for your help!

Shankha
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I asked, Where will those scriplets go in the generated servlet?
 
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both may be threadsafe, or both may not be threadsafe - it depends on what the code does.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lester Burnham wrote:Both may be threadsafe, or both may not be threadsafe - it depends on what the code does.



How is it possible? Method local variable is Thread-Safe!?
 
shankhas sanyal
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Abimaran Kugathasan wrote:If I asked, Where will those scriplets go in the generated servlet?



as i know the system generated servlet has 3 methods:

init()

_JspService()

and
destroy()

this is what i think, please correct me if i am wrong

inside the init() method the declaration tag is declared.

inside the _JspService() method the scriptlet tag is declared.

my question now is: Is everything declared within the init() method threadsafe?


 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Abimaran Kugathasan wrote:How is it possible? Method local variable is Thread-Safe!?


Who's talking about method local variables? As I said, it depends on what the code does.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

shankhas sanyal wrote:
inside the init() method the declaration tag is declared.

inside the _JspService() method the scriptlet tag is declared.

my question now is: Is everything declared within the init() method threadsafe?



Declaration means, you are declaring it as a instance variable to that generated servlet. (It's also a Java Object!) And other thing, thread, which are created by the container for servicing won't access the init() method! It's there to initialize the servlet initialization parameters of the servlet, moreover it'll be invoked by the container.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lester Burnham wrote:
Who's talking about method local variables? As I said, it depends on what the code does.



Where will the scriplets land in the generated servlet?
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

shankhas sanyal wrote:my question now is: Is everything declared within the init() method threadsafe?


Not necessarily. But the init method is not generally of concern when it comes to thread-safety - it is executed just once at a time when requests are not yet being accepted for the resulting servlet to be run.

But neither code in the declaration tag nor code in scriptlets ends up in the init method, so it doesn't hold the answer to what you were asked by the interviewer, nor does it answer what Abimaran asked you.
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Abimaran Kugathasan wrote:

Lester Burnham wrote:
Who's talking about method local variables? As I said, it depends on what the code does.



Where will the scriplets land in the generated servlet?


I don't understand what this question has to do with the part of my post that you quoted.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lester Burnham wrote:
I don't understand what this question has to do with the part of my post that you quoted.



If we declare a variable in scriptlets, then that will land in the service() method of the generated servlet. Then that will be a method local variable, and it's Thread-safe!

So that, I asked!
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Abimaran Kugathasan wrote:If we declare a variable in scriptlets, then that will land in the service() method of the generated servlet. Then that will be a method local variable, and it's Thread-safe!


Again: Nobody is talking about declaring variables. If you look at the first post, you'll see that the place where the code would go is empty - so we don't know what it does. Maybe it does something threadsafe like using local variables; maybe it uses shared mutable state in a way that is not threadsafe - we simply don't know. Thread safety is not about where code lives, it's about what it does.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lester Burnham wrote:
Again: Nobody is talking about declaring variables. If you look at the first post, you'll see that the place where the code would go is empty - so we don't know what it does. Maybe it does something threadsafe like using local variables; maybe it uses shared mutable state in a way that is not threadsafe - we simply don't know. Thread safety is not about where code lives, it's about what it does.



Yea! Correct! Thanks...
 
Marshal
Posts: 67418
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But, of course, the most correct answer is "This isn't 2002, I'd never use either one in a modern JSP!"
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bear Bibeault wrote:But, of course, the most correct answer is "This isn't 2002, I'd never use either one in a modern JSP!"



Accept it Bear! But what is the usage of scriptlets in these days? Just curious to know! Thanks!
 
When it is used for evil, then watch out! When it is used for good, then things are much nicer. Like this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic