Friday, June 24, 2011

Understanding Singleton Design Pattern in multi-threaded environment

List of main design patterns used by java libraries

Using Database Triggers

Generics in JAVA

Thursday, June 23, 2011

JSF state saving mechanism ..

JSF can save state of component tree in either session or on clients machine inform of serialized data .
While creating component tree JSF creates a key and keep all objects of component tree as UIViewRoot in session with against it.For next JSF request it will use same key to restore that view :)
For client side saving has performance issues like it has to serialize whole tree and write it in response ship in form of gzip to client.Next time when same client make JSF request from same page it de-serialize it and inflate all objects in corresponding state .

Setting for state saving is done in web.xml.

Server side state saving is where the component tree and all component state are stored within the user's session. This entry within the session is tracked by writing a key in the response that is used to lookup the entry on subsequent post-backs.

Client side state saving doesn't leverage the server side session mechanism at all, instead, the component tree and state will be serialized using Java Serialization, GZIP compressed (at least that is the default), Base64 encoded, and written to the response. When a post-back occurs, the encoding process will be reversed which will result in the tree and state we started with.

As to the benefits/drawbacks, review the following table.


State Saving MethodServer Side Memory UsageCPU UsageBandwidth Usage
Server without compressionHighLowLow
Server with compressionLow/MediumHighLow
Client with compressionLowHighMedium
Client without compressionLowMedHigh



Setting in web.xml
  <context-param>
<description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>server</param-value>
</context-param>



1.Server side state saving - following hidden field is added (see the value )

<input type="hidden" name="javax.faces.ViewState" id="javax.faces.ViewState" value="2269033803785808195:-5553424543391650776" autocomplete="off" />


observe >> value="2269033803785808195:-5553424543391650776"



2.Client side state saving - following hidden field is added (see the value )

<input type="hidden" name="javax.faces.ViewState" id="javax.faces.ViewState" value="H4sIAAAAAAAAANVWTWwbRRQeOzFxflryU4UitZC6VSVKuo6TmEJCRZzEaVZxaIRDoCmQjr0Te9Px7mR21l63NCoHBEJCUHFCCuLAhQM9ceGCBOKAhFQQkbjABU6oBxBC4gCHipnxrncTskl6ZCU/z+6+efO975v3Zj/5DcSIRUHPGqxCxWY6VmahVZ6HJNb245df9V/5vgVEZ0AHNqE2A4vMpCpo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YI3x1NEK37wmzj9J4Jo1c1iB1eIC6nsSrfVlqzA8hymTRNjKBxZ4De/GHzn9+jILIs/WxeARG+4uNhK1602QGXtFGVgtaqqWvAvxyyIVwUBg5hswjxkvDzJE4BIikbFow96DMmR3tTtsuzKM/4bEgtIKwswtI8YmVTyzqE9zpLNw0XY6RLNpeHGxRw151+67mHOpff+eWPqPTra/r5Hh+9/lb+z+Wtp2UH5DgSJi0pkMBiGe0WUOWlqf4Nf8KpV090uSxO1k6CR09el7lPcqkU3pq5qyRsqsw1RNoNAHZ2YLfJ3N5a+vXu8esXvA4cYSARVBRVhZqBWFnxgBCndgW8sl/lD0wMDw2m0k8NVP35opsiA9HziX0gJxwpdbaW2j+7QHtf4ttIKntBKMSBBmQWo4wb9UCceXEiYqD+n7IONoYeMUjKepE11cZAF0UGP5MQXawTfrZ0ByVfRA4TbqPCnPNmxAu6ofEdYzW7WUx2s1hIH0z758zgnrUlcworraPNktnhVuu4efnnL+7dirpFMMXXObG9dnbMEKUTfztx7403b3/mlc7LtdOgN0izHN1o8BH8UhHSZ8HU/Um/XexGaFdfcU63eYy6RIm/l6Rd4cdqf3CuLrq30CUE2hxQDwzNXXU7OH+BhOMc9LgL/9SYzzw3t6JON/2OnE2NjAyNjJxLDQ+vjK6OFtLpIbjH54AArMxyo3q49j1HLFbnS4NjRRObdKyOMDZr4wVYvFqipm1oYwUuiHeixClat3WKNFkTwjwpzFjgUImJb4OU3zs4y/8CZd7KiB8LAAA="

Understanding java.util.concurrent package

Using Java Reflection

Tuesday, June 14, 2011

sample DD

It is always a good idea to study servlets using web.xml .It has important inforamtion it should be called as mirror of the application .
It is just a XML with setting which servlet container will use to manage application .

 <?xml version="1.0" encoding="ISO-8859-1"?> 
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-app_2_4.xsd"
version="2.4">
<display-name>Test Webapp</display-name>
<description>This is a sample deployment descriptor that shows the
use
of important elements. </description>
<!-- Presence of this element indicates that this WebApp is distributable. -->
<distributable />
<!-- Defines WebApp initialization parameters. -->
<context-param>
<param-name>locale</param-name>
<param-value>US</param-value>
</context-param>
<context-param>
<param-name>DBName</param-name>
<param-value>Oracle</param-value>
</context-param>
<!-- Defines filters and specifies filter mapping -->
<filter>
<filter-name>Test Filter</filter-name>
<description>Just for test</description>
<filter-class>filters.TestFilter</filter-class>
<init-param>
<param-name>locale</param-name>
<param-value>US</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Test Filter</filter-name>
<servlet-name>TestServlet</servlet-name>
</filter-mapping>
<!-- Defines application events listeners -->
<listener>
<listener-class>listeners.MyServletContextListener
</listener-class>
</listener>
<listener>
<listener-class>listeners.MySessionCumContextListener
</listener-class>
</listener>
<!-- Defines servlets -->
<servlet>
<servlet-name>TestServlet</servlet-name>
<description>Just for test</description>
<servlet-class>servlets.TestServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>servlets.HelloServlet</servlet-class>
<init-param>
<param-name>locale</param-name>
<param-value>US</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<security-role-ref>
<!-- role-name is used in HttpServletRequest.isUserInRole(String role)
method. -->
<role-name>manager</role-name>
<!-- role-link is one of the role-names specified in security-role elements. -->
<role-link>supervisor</role-link>
</security-role-ref>
</servlet>
<!-- Defines servlet mappings -->
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/test/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>*.hello</url-pattern>
</servlet-mapping>
<session-config>
<!--specifies session timeout as 30 minutes. -->
<session-timeout>30</session-timeout>
</session-config>
<mime-mapping>
<extension>jar</extension>
<mime-type>application/java-archive</mime-type>
</mime-mapping>
<mime-mapping>
<extension>conf</extension>
<mime-type>text/plain</mime-type>
</mime-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>home.html</welcome-file>
<welcome-file>welcome.html</welcome-file>
</welcome-file-list>
<error-page>
<error-code>404</error-code>
<location>notfoundpage.jsp</location>
</error-page>
<error-page>
<exception-type>java.sql.SQLException</exception-type>
<location>sqlexception.jsp</location>
</error-page>
<taglib>
<taglib-uri>http://abc.com/testlib</taglib-uri>
<taglib-location> /WEB-INF/tlds/testlib.tld </taglib-location>
</taglib>
<taglib>
<taglib-uri>/examplelib</taglib-uri>
<taglib-location> /WEB-INF/tlds/examplelib.tld </taglib-location>
</taglib>
<security-constraint>
<display-name>Example Security Constraint</display-name>
<web-resource-collection>
<web-resource-name>Protected Area</web-resource-name>
<url-pattern>/test/*</url-pattern>
<!-- only POST method is protected -->
<http-method>POST</http-method>
</web-resource-collection>
<web-resource-collection>
<web-resource-name>Another Protected Area</web-resource-name>
<url-pattern>*.hello</url-pattern>
<!-- All methods are protected as no http-method is specified -->
</web-resource-collection>
<auth-constraint>
<!-- Only the following roles can access the above resources. The role
must be defined in security-role. -->
<role-name>supervisor</role-name>
</auth-constraint>
<user-data-constraint>
<!-- Specifies the type of communication between the client and the server.
It can be: NONE, INTEGRAL, or CONFIDENTIAL -->
<transport-guarantee>INTEGRAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
<login-config>
<!-- auth-method can be: BASIC, FORM, DIGEST, or CLIENT-CERT -->
<auth-method>FORM</auth-method>
<realm-name>sales</realm-name>
<form-login-config>
<form-login-page>/formlogin.html</form-login-page>
<form-error-page>/formerror.jsp</form-error-page>
</form-login-config>
</login-config>
<!-- Specifies the roles that are defined in the application server. For
example, Tomcat defines it in conf\tomcat-users.xml -->
<security-role>
<role-name>supervisor</role-name>
</security-role>
<security-role>
<role-name>worker</role-name>
</security-role>
</web-app>