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

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

2. What is the difference between hibernates save, update and saveOrUpdate methods ?

save - save method stores an object into the database. That means it insert an entry if the identifier doesn't exist, else it will throw error. If the primary key already present in the table, it cannot be inserted.

update - update method in the hibernate is used for updating the object using identifier. If the identifier is missing or doesn't exist, it will throw exception.

saveOrUpdate - This method calls save() or update() based on the operation. If the identifier exists, it will call update method else the save method will be called.

Try the following code.

package hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class HibernateExample {
public static void main(String args[]){
Configuration configuration = new Configuration();
SessionFactory sessionFactory = configuration.configure().buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
EmployeeInfo employeeInfo = new EmployeeInfo();

session = sessionFactory.openSession();
transaction = session.beginTransaction();
employeeInfo = new EmployeeInfo();

session = sessionFactory.openSession();

transaction = session.beginTransaction();
employeeInfo = new EmployeeInfo();


3. Instead of using session-config tag of web.xml, how do you programatically change the default session timeout for your web application ?

We can use session.setMaxInactiveInterval(200); where 200 is in Seconds. Note that the value in web.xml is in minutes. If you set like this the value in web.xml is not used as it is a default one.

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

5. Is there some sort of event that happens when a session object gets bound or unbound to the session ?

HttpSessionBindingListener will hear the events When an object is added
and/or remove from the session object, or when the session is invalidated,
in which case the objects are first removed from the session, whether the
session is invalidated manually or automatically (timeout).
So if we want to implement how many number of users currently online we can make use of this HttpSessionBindingListener.

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

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

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

9. What is the capacity of the session object ? What is the maximum amount of data that you could store inside a session ?

There is no limit on the session memory or capacity.
It totally depends on the JVM Heap Memory.
It depends on the RAM of your server infrastructure.
Even if the RAM is not enough, the Virtual memory concept is there...

So you can put any amount of data inside a session, but burdening session will significantly cause performance issues.

10. If i write session.setmaxinactiveinterval(0); then will the session be invalidated ?

No, session.setmaxinactiveinterval(0) doesnt mean that session is invalidated.
session.invalidate() causes the session to invalidate immediately.

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