Java J2ee Struts Hibernate Spring Interview Questions Java J2ee Struts Hibernate Spring Interview Questions
1 2 3    Next

1. What are the differences between JSP and Servlet ?

There is no difference between JSP and Servlet. 

Basically a JSP is also a Servlet. JSP?s will be converted or compiled into Servlets.
JSP?s are tag based where as Servlets are code based.
JSP?s are for web designers who does not know much about Java, Servlets are for java web developers.
JSP?s are very slow compared to Servlets.
We can extend the functionality of the JSP?s very easily using Custom Tag Libraries but is not possible in Servlets.
JSP?s must be used for presentation logic Where as Servlets must be used for business logic.
The only supported protocol in JSP is HTTP Where as Servlets can support any protocol like HTTP, FTP, and SMTP etc

2. What is Server - Side Include Servlets ( SSI ) ?

Server-Side Includes allows embedding servlets within HTML pages using a special servlet tag. In many servlets that support servlets, a page can be processed by the server to include output from servlets at certain points inside the HTML page. This is accomplished using a special internal SSINCLUDE, which processes the servlet tags. SSINCLUDE servlet will be invoked whenever a file with an. shtml extension is requested. So HTML files that include server-side includes must be stored with an . shtml extension.

3. 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.

4. How are Servlets and JSP Pages related ?

JSP pages are focused around HTML (or XML) with Java codes and JSP tags inside them. When a web server that has JSP support is asked for a JSP page, it checks to see if it has already compiled the page into a servlet. Thus, JSP pages become servlets and are transformed into pure Java and then compiled, loaded into the server and executed.

5. How do you implement logging in Servlets ?

Just implement log4j as it is the good one. Download the jar or add its details in maven. in log4j.xml or .properties file add configuration .

6. 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.

7. 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.
request. setAttribute("theBean", myBean);
RequestDispatcher rd = getServletContext(). getRequestDispatcher("thepage.jsp");
rd. forward(request, response);
(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:

8. What is the different ways you can communicate between servlets?

Below are the different ways of communicating between servlets :
Using Request dispatcher Object.
Sharing resource using ServletContext object.
Include response of a resource in response of Servlet.
Calling public method of the resource.
Servlet chaining.

9. What is the default session timeout for web applications ?

The default session timeout for J2EE web applications is 30 minutes. This value is specified in the web.xml file of your web application. 
<!-- ==================== Default Session Configuration ================= -->
  <!-- You can set the default session timeout (in minutes) for all newly   -->
  <!-- created sessions by modifying the value below.                       -->


We can also set the session timeout values programmatically. In that case we will override the default value which is specified in web.xml file.
You can provide a negative value here to indicate that the session never expires like this session.setMaxInactiveInterval(-1);
session.setMaxInactiveInterval(36000); which is equals to 10*60*60
getMaxInactiveInterval(), getLastAccessedTime() can be used as per your requirement.

We have to observe that the tagged one is in minutes and this one which we are explicitly setting is in Seconds.

10. How to use a servlet as your main web page, instead of redirecting to a servlet from your index.jsp page

Just use the URL Pattern inside welcome file list with out begining slash.

Java J2ee Struts Hibernate Spring Interview Questions
1 2 3    Next
Menu - - Java J2ee Struts Spring Hibernate Interview Questions - A home for your website
Ads By Google