JavaSpartans
Java J2ee Struts Hibernate Spring Interview Questions Java J2ee Struts Hibernate Spring Interview Questions
1 2 3 4 5    Next

1. What are daemon threads?

In java we have two type of Threads : Daemon Thread and User Threads. Generally all threads created by programmer are user thread (unless you specify it to be daemon or your parent thread is a daemon thread). User thread are generally meant to run our programm code. JVM doesn't terminates unless all the user thread terminate.

On the other hand we have Daemon threads. Typically these threads are service provider threads. They should not be used to run your program code but some system code. These thread run paralley to your code but survive on the mercy of the JVM. When JVM finds no user threads it stops and all daemon thread terminate instantly. Thus one should never rely on daemon code to perform any program code.

For better understanding consider a well known example of Daemon thread : Java garbage collector. Garbage collector runs as a daemon thread to recalim any unused memory. When all user threads terminates, JVM may stop and garbage collector also terminates instantly.


2. What is the life cycle of a servlet ?

Each Servlet has the same life cycle: a) A server loads and initializes the servlet by init () method. b) The servlet handles zero or more client?s requests through service() method. c) The server removes the servlet through destroy() method.


3. Is it true that servlet containers service each request by creating a new thread? If that is true, how does a container handle a sudden dramatic surge in incoming requests without significant performance degradation?

The implementation depends on the Servlet engine. For each request generally, a new Thread is created. But to give performance boost, most
containers, create and maintain a thread pool at the server startup time. To service a request, they simply borrow a thread from the pool and when
they are done, return it to the pool. For this thread pool, upper bound and lower bound is maintained. Upper bound prevents the resource exhaustion problem associated with unlimited thread allocation. The lower bound can instruct the pool not to keep too many idle threads, freeing them if needed.


4. What is Servlet Chaining ?

Servlet chaining is a technique in which two or more servlets can cooperate in servicing a single request. In servlet chaining, one servlet?s output is piped to the next servlet?s input. This process continues until the last servlet is reached. Its output is then sent back to the client.


5. What is the difference ServletContext and ServletConfig

ServletContext Defines a set of methods that a servlet uses to communicate with its servlet container. ServletConfig is a servlet configuration object used by a servlet container used to pass information to a servlet during initialization. All of its initialization parameters can ONLY be set in deployment descriptor. ServletConfig contains configuration data while


6. What is a green thread ?

Native threads can switch between threads preemptively, switching control from a running thread to a non-running thread at any time. Green threads only switch when control is explicitly given up by a thread (Thread.yield(), Object.wait(), etc.) or a thread performs a blocking operation (read(), etc.). On multi-CPU machines, native threads can run more than one thread simultaneously by assigning different threads to different CPUs. Green threads run on only one CPU. Native threads create the appearance that many Java processes are running: each thread takes up its own entry in the process table. One clue that these are all threads of the same process is that the memory size is identical for all the threads - they are all using the same memory. Unfortunately, this behavior limits the scalability of Java on Linux. The process table is not infinitely large, and processes can only create a limited number of threads before running out of system resources or hitting configured limits.


7. What is the purpose of <load-on-startup> tag in web.xml ?

This tag specifies that the servlet should be loaded automatically when the web application is started.

The value is a single positive integer, which specifies the loading order. Servlets with lower values are loaded before servlets with higher values (ie: a servlet with a load-on-startup value of 1 or 5 is loaded before a servlet with a value of 10 or 20).

When loaded, the init() method of the servlet is called. Therefore this tag provides a good way to do the following:

  • start any daemon threads, such as a server listening on a TCP/IP port, or a background maintenance thread
  • perform initialisation of the application, such as parsing a settings file which provides data to other servlets/JSPs

If no value is specified, the servlet will be loaded when the container decides it needs to be loaded - typically on it's first access. This is suitable for servlets that don't need to perform special initialisation.


8. Is Servlet a thread-safe object ?

Yes, Servlet is thread-safe.


9. What are the different identifier states of a Thread?

- The different identifiers of a Thread are: R - Running or runnable thread, S - Suspended thread, CW - Thread waiting on a condition variable, MW - Thread waiting on a monitor lock, MS - Thread suspended waiting on a monitor lock


10. How do you pass data ( including JavaBeans ) to a JSP from a servlet ?

(1) Request Lifetime:
Using this technique to pass beans, a request dispatcher (using either "include" or "forward") can be called. This bean will disappear after processing this request has been completed.
Servlet:
request. setAttribute("theBean", myBean);
RequestDispatcher rd = getServletContext(). getRequestDispatcher("thepage.jsp");
rd. forward(request, response);
JSP PAGE:
(2) Session Lifetime: Using this technique to pass beans that are relevant to a particular session (such as in individual user login) over a number of requests. This bean will disappear when the session is invalidated or it times out, or when you remove it.
Servlet: HttpSession session = request. getSession(true);
session. putValue("theBean", myBean); /* You can do a request dispatcher here, or just let the bean be visible on the next request */
JSP Page:
(3) Application Lifetime: Using this technique to pass beans that are relevant to all servlets and JSP pages in a particular app, for all users. For example, I use this to make a JDBC connection pool object available to the various servlets and JSP pages in my apps. This bean will disappear when the servlet engine is shut down, or when you remove it.
Servlet: GetServletContext(). setAttribute("theBean", myBean); JSP PAGE:


Java J2ee Struts Hibernate Spring Interview Questions
1 2 3 4 5    Next
Menu - javaspartans.com - Java J2ee Struts Spring Hibernate Interview Questions
Hostens.com - A home for your website
Ads By Google