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. -->
<session-config>
<session-timeout>30</session-timeout>
</session-config>
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);
or 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.
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();
transaction.begin();
EmployeeInfo employeeInfo = new EmployeeInfo();
employeeInfo.setSno(1);
employeeInfo.setName("HibernateTestSave");
session.save(employeeInfo);
transaction.commit();
session.close();
session = sessionFactory.openSession();
transaction = session.beginTransaction();
transaction.begin();
employeeInfo = new EmployeeInfo();
employeeInfo.setSno(1);
employeeInfo.setName("HibernateTestUpdate");
session.update(employeeInfo);
transaction.commit();
session.close();
session = sessionFactory.openSession();
transaction = session.beginTransaction();
transaction.begin();
employeeInfo = new EmployeeInfo();
employeeInfo.setSno(1);
employeeInfo.setName("HibernateTestSaveOrUpdate");
session.saveOrUpdate(employeeInfo);
transaction.commit();
session.close();
}
}
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.
There is no difference between JSP and Servlet.
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.
(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:
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.
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:
If no
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.
No, session.setmaxinactiveinterval(0) doesnt mean that session is invalidated.
session.invalidate() causes the session to invalidate immediately.
Ads By Google
© 2018 - JavaSpartans.com • All Rights Reserved