• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Constructor And Static block

 
Ranch Hand
Posts: 315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is it a good practice to have static blocks and constructor in servlets. if both are used, what issues might be faced by the servlet in terms of concurancy.


For example

 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

is it a good practice to have static blocks and constructor in servlets. if both are used, what issues might be faced by the servlet in terms of concurancy.



I think it won't have any issues with regards to concurrency. Container make sure that servlets are ready to serve any request after being properly initialized.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bad idea. Servlets don't need constructors because they are initialized using their own life-cycle methods, and static blocks just invite threading problems.

Follow best practices and limit data to variables declared within the method bodies.
 
Neeraj Vij
Ranch Hand
Posts: 315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks Bear.

what will happen, if I the example given uses the constructor and static blocks?

please give some detailed examples or links for reference.

I can't google from my office

Thanks,
Neeraj.
 
Vijitha Kumara
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Neeraj Vij wrote:what will happen, if I the example given uses the constructor and static blocks?



Reason is when you have a static (class level) variable when multiple threads executed in a given servlet instance you will have all sorts of concurrency issues and that's why you should avoid that.
 
Neeraj Vij
Ranch Hand
Posts: 315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But in the example, I have used vector. Will it still have concurency issues ?

I just wrote a small servlet for constructors usage-

if we don't want to use a ServletConfig object, it is fine to use a public no-arg constructor. Constructor was inviked first and then the init() method.

I want to know the risks/disadvantage of using constructors in servlet. if any?

Thanks
Neeraj.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Inconsistency is not good. No experienced web developer worth their salt will use a servlet constructor because it's confusing and fragile. Just because you can get something to work, doesn't mean it's right. Following accepted practices and standards makes your code much more readable and understandable.
 
Neeraj Vij
Ranch Hand
Posts: 315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bear.


I will try to conivince the developer to not use constructors.

Will vectors be safe in transactions, if used in static blocks ?

Thanks,
neeraj.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Neeraj Vij wrote:
I can't google from my office



I've never seen this before. I wonder why would they do this . I hope this is not common in most offices...
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's a bit off topic, but you wouldn't believe some of the idiotic policies that big companies put in place.

My previous job was with a small software company that got bought by a BIG BANK. Some of the policies that got put into place were nothing short of ludicrous.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've never used a read-write static block in the over ten years I've been writing servlets -- since their very inception.

Data that's to be shared across the application goes in the application context, per-user data in the session.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic