• 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

servlet's constructor

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
i have a few queries for u.

1. why dont we write any code in the constructor of the servlet?(reason other than unavailablility of config and context)

2.can a servlet have static fields and methods(is it a good practice to write them?)

3. why only a get request can be bookmarked(and how the bookmarking is done?)

4. what does a VM mean with respect to the folder structure( for example say 2 servlets are in 2 VM) does that mean they are in 2 different systems...or in 2 different webservers on the same JVM?Why is the book always referring to things on different VMs'?

5. how do the sessions migrate from one VM to the other if they are on two different machines?

6.what is the difference b/w client known URL name and Deployer known secret internal name on page 46?

Hope you help me with these queries.

Regards,
Yashu
 
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. why dont we write any code in the constructor of the servlet?(reason other than unavailablility of config and context)

Because it is called only once .

2.can a servlet have static fields and methods(is it a good practice to write them?)

static fields are used when you want to maintain single copy of variable for multiple instances of the class.As already there is always one instance per servlet,it doesn't make any sense to have static fields in servlet.
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think two main reasons why you do not write initialization code in a constructor is because

1) You have to hard-code rather than use a DD.
2) You can not share those values with other servlets since you are not in context yet.
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Originally posted by yeshwanth reddy:
[QB]Hi all,
i have a few queries for u.

1. why dont we write any code in the constructor of the servlet?(reason other than unavailablility of config and context)

Normally we use a constructor to initialize things or do things when an instance of the class is created. Here we handle everything in the init() method. So there should not be any need to override the constructor already provided(default constructor). There should be a constructor without any arguments provided if you override the constructor, since it would be one used to create an instance. Other than that there should not be any constraints I guess.


2.can a servlet have static fields and methods(is it a good practice to write them?)

It could have static fields and methods. I don't think its a good practice though.

3. why only a get request can be bookmarked(and how the bookmarking is done?)

I think when you bookmark a GET request whole of the URL gets saved which has the information required in the URL. This would take you to the correct page. You could try to give an URL with the parameters supplied in the URL if you already knew them.


4. what does a VM mean with respect to the folder structure( for example say 2 servlets are in 2 VM) does that mean they are in 2 different systems...or in 2 different webservers on the same JVM?Why is the book always referring to things on different VMs'?

You are correct. If you run two webserver on the same machine you would have two VM's one for each. Virtual machine is part of the process memory allocated for it.


5. how do the sessions migrate from one VM to the other if they are on two different machines?


Through replication which occurs during load sharing, etc,. That would be the problem of the Load Balancing Server to replicate the session details from one machine(VM) to other Machine(VM). An explaination on this is in pg 256 & 257 of HFSJ. That should give you a clear picture.


6.what is the difference b/w client known URL name and Deployer known secret internal name on page 46?

Client known URL is the URL which we use in the <servlet-mapping> <url-pattern>. This the URL which the clients types into the browser and the server looks into the DD and directs him to the appropriate servlet. The secret internal name is just a name given to the servlet inside the DD itself. It doesn't have to do with any thing else. The name is used in <servlet><servelt-name>xyz</ </ tag and <servlet-mapping><servlet-name>xyz</ </ only.


Hope this helps. I may be wrong with some of the answers.

Thanks
Chandu
 
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just a word about point 6.

The servlet name may be used also to forward/include a request to the servlet if you decide to use a named dispatcher.

<servlet-name>MaServlet</servlet-name>

RequestDispatcher rd = ctx.getNamedDispatcher("MaServlet");

Remember the request parameters usually set are not when using NamedDispatcher
(javax.servlet.forward.request_uri and so on)
 
yeshwanth reddy
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Thanks a lot for those replies. You answered most of my doubts (especially chandra)..but more of them will be posted shortly...

Thanks and Regards,
Yashwanth
 
reply
    Bookmark Topic Watch Topic
  • New Topic