Showing posts with label Session. Show all posts
Showing posts with label Session. Show all posts

Sunday, 27 July 2014

What is JSESSIONID in J2EE Web application - JSP Servlet?

What is JSESSIONID in J2EE Web application - JSP Servlet? 

JSESSIONID is a cookie generated by Servlet container like Tomcat or Jetty and used for session management in J2EE web application for http protocol. Since HTTP is a stateless protocol there is no way for Web Server to relate two separate requests coming from same client and Session management is the process to track user session using different session management techniques like Cookies and URL Rewriting. If Web server is using cookie for session management it creates and sends JSESSIONID cookie to the client and than client sends it back to server in subsequent http requests.

When JSESSIONID created in Web application?


  • In Java J2EE application container is responsible for Session management and by default uses Cookie.
  • If user request is served by Servlet than session is created by calling request.getSession(true) method. it accepts a boolean parameter which instruct to create session if its not already existed. if you call    request.getSession(false) then it will either return null if no session is associated with this user or return the associated HttpSession object.
  • If HttpRequest is for JSP page than Container automatically creates a new Session with JSESSIONID if this feature is not disabled explicitly by using page directive %@ page session="false" %>.
  • Once Session is created Container sends JSESSIONID cookie into response to the client. In case of HTML access, no user session is created. If  client has disabled cookie than Container uses URL rewriting for managing session on which jsessionid is appended into URL as shown below:           
  •  Ex. - https://localhost:8443/supermart/login.htm;jsessionid=1A530637289A03B07199A44E8D531427
  • When HTTP session is invalidated(), mostly when user logged off, old JSESSIONID destroyed and a new JSESSIONID is created when user further login.

Tuesday, 15 July 2014

JSP implicit objects

JSP implicit objects are created by container while translating JSP page to Servlet source to help developers. We can use these objects directly in scriptlets that goes in service method, however we can’t use them in JSP Declaration because that code will go at class level.
We have 9 implicit objects that we can directly use in JSP page. Seven of them are declared as local variable at the start of _jspService() method whereas two of them are part of _jspService() method argument that we can use.
  1. out Object
  2. request Object
  3. response Object
  4. config Object
  5. application Object
  6. session Object
  7. pageContext Object
  8. page Object
  9. exception Object
  1. out Object

    JSP out implicit object is instance of javax.servlet.jsp.JspWriter implementation and it’s used to output content to be sent in client response. This is one of the most used JSP implicit object and thats why we have JSP Expression to easily invoke out.print() method.
  2. request Object

    JSP request implicit object is instance of javax.servlet.http.HttpServletRequest implementation and it’s one of the argument of JSP service method. We can use request object to get the request parameters, cookies, request attributes, session, header information and other details about client request.
  3. response Object

    JSP response implicit object is instance of javax.servlet.http.HttpServletResponse implementation and comes as argument of service method. We can response object to set content type, character encoding, header information in response, adding cookies to response and redirecting the request to other resource.
  4. config Object

    JSP config implicit object is instance of javax.servlet.ServletConfig implementation and used to get the JSP init params configured in deployment descriptor.
  5. application Object

    JSP application implicit object is instance of javax.servlet.ServletContext implementation and it’s used to get the context information and attributes in JSP. We can use it to get the RequestDispatcher object in JSP to forward the request to another resource or to include the response from another resource in the JSP.
  6. session Object

    JSP session implicit object is instance of javax.servlet.http.HttpSession implementation. Whenever we request a JSP page, container automatically creates a session for the JSP in the service method.
    Since session management is heavy process, so if we don’t want session to be created for JSP, we can use page directive to not create the session for JSP using <%@ page session="false" %>. This is very helpful when our login page or the index page is a JSP page and we don’t need any user session there.
  7. pageContext Object

    JSP pageContext implicit object is instance of javax.servlet.jsp.PageContext abstract classimplementation. We can use pageContext to get and set attributes with different scopes and to forward request to other resources. pageContext object also hold reference to other implicit object.
  8. page Object

    JSP page implicit object is instance of java.lang.Object class and represents the current JSP page. page object provide reference to the generated servlet class. This object is very rarely used.
  9. exception Object

    JSP exception implicit object is instance of java.lang.Throwable class and used to provide exception details in JSP error pages. We can’t use this object in normal JSP pages and it’s available only in JSP error pages.

For Example Refer my next post  

Wednesday, 25 June 2014

Hibernate

Hibernate -

Hibernate is an open-source Java Persistence framework that lets applications connect to and work with relational databases using Object Relational Mapping . It implements the Java Persistence API - JPA which is the standard specification for object - relational mapping.Persistence is a process of storing the data to some permanent medium and retrieving it back at any point of time even after the application that had created the data ended.

 Architecture - 


Fig (a) Architecture of hibernate 

            The above diagram shows architecture of Hibernate. It creates a layer between Database and the Application. It loads the configuration details like Database connection string, entity classes, mappings etc.

Elements of the hibernate - 

SessionFactory

              The SessionFactory is a factory of session and client of ConnectionProvider. It holds second level cache (optional) of data. The org.hibernate.SessionFactory interface provides factory method to get the object of Session. 

Session

         The session object provides an interface between the application and data stored in the database. It is a short-lived object and wraps the JDBC connection. It is factory of Transaction, Query and Criteria. It holds a first-level cache (mandatory) of data. The org.hibernate.Session interface provides methods to insert, update and delete the object. It also provides factory methods for Transaction, Query and Criteria.
 

Transaction

            The transaction object specifies the atomic unit of work. It is optional. The org.hibernate.Transaction interface provides methods for transaction management.

ConnectionProvider

               It is a factory of JDBC connections. It abstracts the application from DriverManager or DataSource. It is optional.

 

TransactionFactory

               It is a factory of Transaction. It is optional.