MY mENU


Monday 12 March 2012

Methods in Java


Methods:

  • If you define more than one method with the same name in the same class, Java must be able to determine which method to invoke based on the number and types of parameters defined for that method.
  • The compiler will complain if you have two methods with identical signatures exception for the return type and/or the exceptions thrown, i.e. can’t overload based on return type and/or exceptions thrown.
  • Determining which methods will be invoked with integer params.
  • It is possible to declare an inherited method abstract.
  • Obviously, it is not possible to override a final method.
  • A subclass may make an inherited method synchronized, or it may leave off the synchronized keyword so that its version is not synchronized. If a method in a subclass is not synchronized but the method in the superclass is, the thread obtains the monitor for the object when it enters the superclass’s method.
  • It is possible to declare an inherited method as abstract, but then there is no way to get to the behaviour in the hierarchy above the abstract declaration.
  • Return types must match the overriden method in the superclass exactly. The parameter types must match those in the superclass exactly, i.e. in the same order. If this is not the case, then the superclass’s method is not overridden. Compiler will not complain, however, unless exactly the same signature except for return type (or exceptions thrown).
  • You cannot make a method in a subclass more private than it is defined in the superclass, ‘though you can make it more public.
  • Coercion of arguments only happens with overloading, not overriding.
  • It is perfectly legal to have two different instance variables with the same name if they are defined in different classes where one class inherits from the other.
  • Which variable is accessed depends on the type of object reference which the variable was declared to hold. Which method gets invoked depends on the underlying object. 
  • A native method does not have a body, or even a set of braces, e.g. public native void method();
  • A native method cannot be abstract.
  • A native method can throw exceptions.

Memory and Garbage Collection


Memory and Garbage Collection:

  • As soon as you lose the reference to an object, that object becomes eligible for garbage collection.
  • Setting your object reference to null makes it a candidate for garbage collection.
  • You can directly invoke the garbage collector by getting an object which represents the current runtime and invoking that object’s gc() method.
  • Can’t predict when garbage collection will occur, but it does run whenever memory gets low.
  • If you want to perform some task when your object is about to be garbage collected, you can override the java.lang.Object method called finalize(). This method is declared as protected, does not return a value, and throws a Throwable object, i.e. protected void finalize() throws Throwable.
  • Always invoke the superclass’s finalize() method if you override finalize().
  • The JVM only invokes finalize() once per object. Since this is the case, do not resurrect an object in finalize as when the object is finalized again its finalize() method will not be called. Instead you should create a clone of the object if you must bring the object back to life.
  • Remember that Java passes method parameters by value and not by reference. Obviously then, anything that happens to a primitive data type inside a method does not affect the original value in the calling code. Also, any reassignment of object references inside a method has no effect on the objects passed in.

How to Install IIS server on Windows 7


Microsoft IIS (Internet Information Server) is the Windows counterpart of  Linux Apache server. It is the second most popular HTTP server in the world after Apache. IIS provides the following services: FTP (File transfer protocol), SMTP for email and HTTP for web server. 
Note: The IIS server is not available with Windows 7 Home edition. So, you must have Windows 7 Professional edition or Ultimate edition for installing IIS web server.
How to Install Microsoft IIS Server on Windows 7
  1. Insert your Windows 7 installation disk into optical drive and goto Start> Control Panel > Programs.
  2. From the dialog box that opens, click on Turn Windows features on or off.
  3. image21
  4. Another dialog box will open that will contain a list of many Windows features which are not yet installed on your computer.
  5. Now, you have to check the box for ‘Internet Information Services (IIS)‘ leaving all the default installation settings intact.
  6. Once IIS server is installed on your computer, you can view your homepage by typing “http://localhost” in the address bar of your web browser. The default root path, where you can store your the files of your website is C:Inetpubwwwroot assuming that Windows is installed in “C:”drive.
By following these steps, you can setup your own HTTP to test your website locally on you computer, before uploading it to Internet. You can also setup your own server to host your website permanently on your computer, instead of an online hosting provider. You can also use this server alternatively, as an FTP server to easily share files with your friends. So, setup your own server on Windows 7 and enjoy !

Servlet Questions



What is the Servlet?
A servlet is a Java technology-based Web component. Managed by a web server or Application    server (Tomcat, Weblogic, Glashifish..etc). 
Servlet generates dynamic content and interacts with web clients(browsers) via a request and response programming model. 
so we call servlets are server side components. 
servlets are not designed for a specific protocols. But mostly it works on HTTP based protocol. Therefore, the word "servlet" is often used in the meaning of "HTTP Servlet".

How many types of servlets are there?
 Saying there are two types of servlet
1.GenericServlet
2.HttpServlet is always wrong answer.
There is a possibility of developing ‘n’ types of servlet like HTTP protocol base servlet, FTP protocol base  servlets, SMTP protocol base servlets etc. 
Since all the web-servers & Application servers available in the market are coming as HTTP protocol based servers. so the programmer prefers to develop their servlets as HTTP servlets. 
The javax.servlet.http.HttpServlet class is designed based on HTTP protocol Standards.

How a servlet  is executing without main() method?
If you give a java class directly to JVM/JRE for execution then JVM/JRE expects public static void main (strong args[]) to start the execution. But we are not giving our web application directly to the JVM/JRE. We are hosting our web application in web Container. This web container is already containing main(). So this main() will be execute.
Conclusion : web-server/web-container is also a java application that also contains main() method, so we no need to keep main() in our web application.


Can I keep main() method in our servlet class?
You can place but it never executes automatically .because it is not a life cycle method. You can see main() method acting as helper method(normal method) to life cycle methods. you can call this main() from any lifecycle method explicitly as a helper method.

 Can we write a default constructor for servlet ?
 Yes.But not recommended. Container by default calls the default constructor. If we don't write container will create default constructor.

Can we place both parameterized and zero Argument constructor in my servlet?
 Yes you can, In a servlet class you can place parameterized constructor along with user-defined zero argument constructor but these parameterized constructors never executes.

What is the difference between code placed in the constructor and code placed in the init() method?
To read init parameters of a servlet we need ServletConfig object. But this  ServletConfig object is created after the execution of constructor and before execution of init() method.
So code placed in the constructor cannot read init parameters because ServletConfig object is not available. But code placed in the init()  can read init parameters because ServletConfig object is available in the init().

 For initializing a servlet can we use constructor in place of init ().
 No,  Because ServletConfig object is not available in constructor. (by using ServletConfig only we can read init-parameters from web.xml)

 When servlet object will be created?
The web-container creates object for a servlet when one of the following situations occur
a. When servlet gets first request from the browser.
b. For first request given to servlet after restarting the server.
c. For first request given to servlet after restarting the web-application.
d. For first request given to servlet after reloading the web-application.
e. During server startup or when web-application is deployed. When<load-on-startup> is enabled.

When servlet object will be destroyed?
 The web-container destroys servlet object when one of the following situations occur.
a. When you shutdown the server
b. When server is crashed
c. When you stop the running web-application.
d. When web-application is reloaded.
f. When web-application is undeployed
f. When garbage collector destroys the servlet object.

 How can we create deadlock condition on our servlet?
 one simple way to call doPost() method inside doGet() and doGet()method inside doPost() it will create deadlock situation for a servlet. This is rather simple servlet interview questions but yet tricky if you don’t think of it.

 Who will create Servlet Object ?
 WebServer Or Application server
 Description :   Creating servlet object managing servlet object by calling life cycle methods, processing request & destroying servlet object is the responsibility of the underlying “web-server/application server”.
Programmer just writes the logic he wants to execute by taking the support of life cycle methods.

When request & response objects will be created in servlet?
 For every new request coming to servlet a separate request & response objects will be created by “web server” before executing the service() method these two objects are visible in the service() method once request related response goes to browser request,response objects of that HttpServlet will be destroyed.

 Note : If 100 requests are given to a servlet 100 pairs  of request & response objects will be created & will be destroyed at the end of request processing & response generation.

GenericServlet Vs HttpServlet



                GenericServlet


                         HttpServlet

Signature: public abstract class GenericServlet extends java.lang.Object implements Servlet, ServletConfig, java.io.Serializable


Signature: public abstract class HttpServlet extends GenericServlet implements java.io.Serializable

GenericServlet defines a generic, protocol-independent servlet.(for  any protocol like FTP, SMTP etc.)


HttpServlet defines a HTTP protocol specific servlet

In GenericServlets you cannot use Cookies or HttpSession, Session tracking is not possible is not possible.


These all are possible in HttpServlet .

Generic servlet overrides service method            


Http servlets overrides doGet and doPost methods        


To write a generic servlet, you need only override the abstract service method.

To write a http servlet, you need only override the any doXXX(-,-) or service(-,-) method.