aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes servlet's constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "servlet Watch "servlet New topic
Author

servlet's constructor

yeshwanth reddy
Ranch Hand

Joined: Mar 10, 2006
Posts: 38
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
Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
Posts: 442
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.


SCJP1.4
"Continuous effort - not strength or intelligence - is the key to unlocking our potential."
*Winston Churchill
Ernesto Leyva
Ranch Hand

Joined: Feb 23, 2006
Posts: 62
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.
Chandra Sagi
Ranch Hand

Joined: May 05, 2005
Posts: 162
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
Frederic Esnault
Ranch Hand

Joined: Feb 13, 2006
Posts: 284
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)


SCJP 5 - SCWCD 1.4 - SCBCD 1.3 - Certification study documents/resources: http://esnault.frederic.free.fr/certification
yeshwanth reddy
Ranch Hand

Joined: Mar 10, 2006
Posts: 38
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
 
wood burning stoves
 
subject: servlet's constructor