Monday, November 9, 2009

Servlet HttpSession Management

Session tracking is a mechanism that is used to maintain state about a series of requests from the same user. Java Servlet technology provides an API for managing sessions and allows several mechanisms for tracking sessions. One of the best approach is by using HttpSession object. HttpSession interface is defined in "javax.servlet.http" package and is used for the purpose of session tracking while working with servlets.

we can access a session by calling the HttpServletRequest.getSession() or HttpServletRequest.getSession(boolean) method of a request object. This method will return the current session associated with this request or if the request does not have any session, it will create a new one.


A sample Program demonstrating the usage of HttpSession


import java.io.*;
import java.util.*;

import javax.servlet.*;

import javax.servlet.http.*;


class HttpSessionEx extends HttpServlet {


public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws
IOException, ServletException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();

out.println( "<HTML><BODY>" );


HttpSession sess = request.getSession(true);


// HttpSession Methods


Date cd = new Date(sess.getCreationTime());

Date acd = new Date(sess.getLastAccessedTime());

out.println("Session ID " + sess.getId());

out.println("Session was Created at: " + cd);

out.println("Session was Last Accessed at: " + acd);


// getting the session item


Object sessobj = sess.getAttribute( "power" );

out.println( "<BR>" + sessobj );


// getting the contents of the session


Enumeration data = sess.getAttributeNames();


while( data.hasMoreElements() ) {


String value = (String) data.nextElement();

sessobj = sess.getAttribute(value);

out.println( "<BR>" + value + " = " + sessobj );

}


out.println( "</BODY></HTML>" );

}

}

Read more...

Friday, November 6, 2009

Hibernate Lazy Initialization

Lazy Initialization in Hibernate

Hibernate supports the feature of lazy initilasation for both entities and collections, which actually means is, the Hibernate engine loads only those objects that we are querying for and doesn't try to fetch other entities (that are associated with the entity we are querying) or collections.


Lazy initialization load the child objects while loading parent object. To getting this set the lazy false in mapping file or class.lazy false in class file and hibernate will load the child when parent is loaded from the database. by default lazy is true. Lazy loading means that any foreign key references that you have in your table will be loaded only when referred to by the application. Eager loading means everything will be loaded at once.


An attribute 'lazy' can be used to let Hibernate know if the associated entity or collection has to be lazily loaded or prefetched.

<set name="Child" lazy="false" inverse="true">
<key column="FOREIGN_KEY_COL"/>

<one-to-many class="Parent"/>

</set>
This causes the collection to be eagerly fetched rather than doing a lazy fetch. If on the other hand, the attribute value of lazy is set to true, then hibernate will not make an attempt to fire the query for fetchingthe collection object until the request is made by the user.

Read more...

Wednesday, November 4, 2009

Java Program to list the Contents of a Zip File

Java Sample program to list the contents of a zip file

In this program we need to get the entries of the zip file, since each file in a zip file is represented by an entry. In this program assume that the filename of the zip file is 'Test.zip'. By calling the entries method of the ZipFile object we get an Enumeration back that can be used to loop through the entries of the file. We have to cast each element in the Enumeration to a ZipEntry.


import java.util.zip.ZipFile;
import java.util.zip.ZipEntry;
import java.io.IOException;
import java.util.Enumeration;

public class ListZipFiles
{
public void doListFiles()
{
try
{
ZipFile zipFile = new ZipFile("Test.zip");
Enumeration zipEntries = zipFile.entries();

while (zipEntries.hasMoreElements())
{
//Process the name, here we just print it out
System.out.println(((ZipEntry)zipEntries.nextElement()).getName());
}
}
catch (IOException ex) {
ex.printStackTrace();
}
}
// Command line arguments
public static void main(String[] args) {

new Main().doListFiles();
}
}

Read more...
Blog Widget by LinkWithin

JS-Kit Comments

  © Blogger template Newspaper III by Ourblogtemplates.com 2008

Back to TOP