Monday, 25 November 2013

Difference between Hashtable and ConcurrentHashMap and HashMap

Difference between Hashtable and ConcurrentHashMap and HashMap


HashMap
HashTable
ConcurrentHashMap
Not Synchronized
Synchronized
ConcurrentHashMap and CopyOnWriteArrayList implementations provide much higher concurrency while preserving thread safety, with some minor compromises in their promises to caller
Hashmap allow one null key and Multiple null value
Not allow null keys or null values
ConcurrentHashMap do not allow null keys or null values
COLLECTION CLASS
LEGACY CLASSS
COLLECTION CLASS
Works in only Single thread models access
Once the size of hashtable becomes considerable large performance degrade because for iteration it has to be locked for longer duration.
Since ConcurrentHashMap introduced concept of segmentation , how large it becomes only certain part of it get locked to provide thread safety so many other readers can still access map without waiting for iteration to complete.
Not  Loked
Locked on Whole  Map
In Summary ConcurrentHashMap only locked certain portion of Map
You can make HashMap synchronized by wrapping it on Collections.synchornizedMap(HashMap) which will return a collection which is almost equivalent to Hashtable, where every modification operation on Map is locked on Map object

while in case of ConcurrentHashMap, thread-safety is achieved by dividing whole Map into different partition based upon Concurrency level and only locking particular portion instead of locking whole Map. by default  size is 16
HashMap only slightly better. in Single thread Environment
bad performance
ConcurrentHashMap is more scalable and performs better than Synchronized HashMap in multi-threaded environment
Collections.synchronizedMap and Collections.synchronizedList, provide a basic conditionally thread-safe implementation of Map and List. However, several factors make them unsuitable for use in highly concurrent applications  for example their single collection-wide lock is an impediment to scalability and it often becomes necessary to lock a collection for a considerable time during iteration to prevent ConcurrentModificationException.


Does not maintain Insertion order
it maintain Insertion order





Reference :-


Friday, 8 November 2013

Difference between getAttribute() and getParameter()

Difference between getAttribute() and getParameter()


SR
getAttribute()
getParameter()
1
The getAttribute() method always returns you an object
.getParameter() method always returns an string.
2
A "parameter" is a name/value pair sent from the client to the server - typically, from an HTML form. Parameters can only have String values. Sometimes (e.g. using a GET request) you will see these encoded directly into the URL (after the ?, each in the form name=value, and each pair separated by an &). Other times, they are included in the body of the request, when using methods such as POST.

using request.setAttribute(). You can add any type of object you like here, Strings, Custom objects, in fact any object. You add the attribute to the request and forward the request to another resource, the client does not know about this. So all the code handling this would typically be in JSP/servlets. You can use request.setAttribute() to add extra-information and forward/redirect the current request to another resource.
3
An "attribute" is a server-local storage mechanism - nothing stored in scoped attribues is ever transmitted outside the server unless you explicitly make that happen. Attributes have String names, but store Object values. Note that attributes are specific to Java (they store Java Objects),
while parameters are platform-independent (they are only formatted strings composed of generic bytes).
4
There are four scopes of attributes in total: "page" (for JSPs and tag files only), "request" (limited to the current client's request, destroyed after request is completed), "session" (stored in the client's session, invalidated after the session is terminated), "application" (exist for all components to access during the entire deployed lifetime of your application).

The bottom line is: use parameters when obtaining data from the client, use scoped attributes when storing objects on the server for use internally by your application only.
5
the servlet/jsp sets and reads attributes for communicating among themselves
Typically, parameters are send by the user browser via HTTP GET/POST,

Difference between forward and sendRedirect

Difference between forward and sendRedirect





SR
sendRedirect
forward
1
sendRedirect() sends a redirect response back to the client's browser. The browser will normally interpret this response by initiating a new request to the redirect URL given in the response
forward() does not involve the client's browser. It just takes browser's current request, and hands it off to another servlet/jsp to handle. The client doesn't know that they're request is being handled by a different servlet/jsp than they originally called.
2
If you want the browser to initiate a new request to a different servlet/jsp, or if the servlet/jsp you want to forward to is not in the same web application, use sendRedirect().
if you want to hide the fact that you're handling the browser request with multiple servlets/jsp, and all of the servlets/jsp are in the same web application, use forward() or include().
3
This is happend at Client Side .
This happened in Server side
4
In this case existing request and response are lost
In this case existing request and response are  preserve
5
in redirect, you can see the redirected address.
In forward, you will not see the forwarded address (since the browser is not involved)

When can we use forward and when can we use sendRedirect?

Technical scenario: redirect should be used
  1. If you need to transfer control to different domain
  2. To achieve separation of task.
For example, database update and data display can be separated by redirect. Do the PaymentProcess and then redirect to displayPaymentInfo. If the client refreshes the browser only the displayPaymentInfo will be done again and PyamenProcess will not be repeated. But if you use forward in this scenario, both PaymentProcess and displayPaymentInfo will be re-executed sequentially, which may result in incosistent data.

Thursday, 7 November 2013

static and dynamic inDifference between JSP include directive and JSP include actionclude

Difference between JSP include directive and JSP include action





SR
(Dynamic Include)
(Static Include)
1
The syntax for dynamic include is <jsp:include page="file.jsp”/>
The syntax for static include is <%@ include file=”file.jsp” %>
2
<jsp:include page=”relativeURL” /> is the JSP include action element.
The jsp:include action element is like a function call. At runtime, the included file will be ‘executed’ and the result content will be included with the soure JSP page. When the included JSP page is called, both the request and response objects are passed as parameters.
JSP include directive.
At JSP page translation time, the content of the file given in the include directive is ‘pasted’ as it is, in the place where the JSP include directive is used. Then the source JSP page is converted into a java servlet class. The included file can be a static resource or a JSP page. Generally JSP include directive is used to include header banners and footers.
3
dynamic includes is slower
Static includes are faster than dynamic includes
4
Dynamic include would throw a Page not found (HTTP Error 404), if the resource is not available at run time.
In static include if the resource is not available, the container would throw an internal server error (500).Once the page is compiled, even if the resource is not available, static include would work as expected. This is because the resource is already a part of the main file and it will not look for the file at run time.


5
dynamic include is more efficient for dynamic content
Static includes are more efficient for static content
6
dynamic include can accept a parameter.


<jsp:include page="/index.jsp">
 <jsp:param name="name" value="sos" />
</jsp:include>

Static include cannot accept a parameter
7
all thing here checked at Run time
The JSP compilation procedure is that, the source JSP page gets compiled only if that page has changed. If there is a change in the included JSP file, the source JSP file will not be compiled and therefore the modification will not get reflected in the output.

IBM WebSphere Portal Architecture

IBM WebSphere Portal Architecture






Note:-If some body have  Other version Architecture Image or some information please send it to me at venktesh.dwivedi@gmail.com

Tuesday, 5 November 2013

Spring Annotation

Core Spring Annotation


SR
Annotation
Use
Explanation
1
@Autowired
Constructor, Field,
Method

Declares a constructor, field, setter
method, or configuration method
to be autowired by type. Items
annotated with @Autowired do not
have to be public.
2
@Configurable
Type
Used with <context:springconfigured>
to declare types whose
properties should be injected, even
if they are not instantiated by Spring.
Typically used to inject the properties
of domain objects.

3
@Order
Type, Method, Field
Defines ordering, as an alternative
to implementing the org.
springframework.core.Ordered
interface.

4
@Qualifier
Field, Parameter, Type,
Annotation Type
Guides autowiring to be performed by
means other than by type.

5
@Required
Method (setters)
Specifies that a particular property
must be injected or else the
configuration will fail.
6
@Scope
Type
Specifies the scope of a bean, either
singleton, prototype, request, session,
or some custom scope.




Stereotyping Annotations

These annotations are used to stereotype classes with regard to the application tier that they belong to. Classes that are annotated with one of these annotations will automatically be registered in the Spring application context if <context:component-scan> is in the Spring XML configuration. In addition, if a  PersistenceExceptionTranslationPostProcessor is configured in Spring, any bean annotated with @Repository will have SQLExceptions thrown from its methods translated into one of Spring’s unchecked  DataAccessExceptions.


SR
Annotaion
Use
Explanation
1
@Component
Type
Generic stereotype annotation for any Spring-managed
component.

2
@Controller
Type
Stereotypes a component as a Spring MVC controller.

3
@Repository
Type
Stereotypes a component as a repository. Also indicates that SQLExceptions thrown from the
component’s methods should be translated into Spring
DataAccessExceptions.


4
@Service
Type

Stereotypes a component as a service.





Spring MVC Annotations
These annotations were introduced in Spring 2.5 to make it easier to create Spring MVC applications with minimal XML configuration and without extending one of the many implementations of the Controller interface.







SR
Annotaion
Use
Explanation
1
@Controller
Type
Stereotypes a component as a Spring
MVC controller.
2
@InitBinder
Method
Annotates a method that customizes
data binding.
3
@ModelAttribute
Parameter,
Method

When applied to a method, used
to preload the model with the value
returned from the method. When applied to a parameter, binds a model attribute to the parameter .
4
@RequestMapping
Method, Type

Maps a URL pattern and/or HTTP method to a method or controller type.

5
@RequestParam
Parameter
Binds a request parameter to a method  parameter.
6
@SessionAttributes
Type
Specifies that a model attribute should be  stored in the session.

Transaction Annotations
The @Transactional annotation is used along with the <tx:annotation-driven> element to declare transactional  boundaries and rules as class and method metadata in Java.

SR
Annotaion
Use
Explanation
1
@Transactional
Method,Type
Declares transactional boundaries and rules on a bean and/or its methods.




The <tx:annotation-driven> element tells Spring to keep an eye out for beans that are annotated with @Transactional. In addition,you’ll also need a platform transaction manager bean declared in the Spring context. For example, if your application uses Hibernate, you’ll want to include the HibernateTransactionManager:
<bean id=”transactionManager”
class=”org.springframework.orm.hibernate3.
HibernateTransactionManager”>
<property name=”sessionFactory” ref=”sessionFactory”
/> </bean>
With the basic plumbing in place, you’re ready to start annotating the transactional boundaries:
@Transactional(propagation=Propagation.SUPPORTS,
readOnly=true)
public class TreasureRepositoryImpl implements
TreasureRepository {
…
@Transactional(propagation=Propagation.REQUIRED,
readOnly=false)
public void storeTreasure(Treasure treasure) { …}
…
}
At the class level, @Transactional is declaring that all methods should support transactions and be read-only. But, at the method-level, @Transactional declares that the storeTreasure() method requires a transaction and is not read-only.
Note that for transactions to be applied to @Transactionalannotated
classes, those classes must be wired as beans in Spring.






JMX Annotations
These annotations, used with the <context:mbean-export> element, declare bean methods and properties as MBe operations and attributes.

SR
Annotaion
Use
Explanation
1
@ManagedAttribute
Method
Used on a setter or getter method
to indicate that the bean’s property
should be exposed as a MBean
attribute.
2
@ManagedNotification
Type
Indicates a JMX notification emitted
by a bean.
3
@ManagedNotifications
Type

Indicates the JMX notifications
emitted by a bean.
4
@ManagedOperation
Method

Specifies that a method should be
exposed as a MBean operation.
5
@ManagedOperationParameter
Method
Used to provide a description for an
operation parameter.
6
@ManagedOperationParameters
Method
Provides descriptions for one or
more operation parameters.
7
@ManagedResource
Type
Specifies that all instances of a class
should be exposed a MBeans.

Exposing a Spring Bean as a MBean To get started with Spring-annotated MBeans, you’ll need to include <context:mbean-export> in the Spring XML configuration:
<context:mbean-export/>
Then, you can annotate any of your Spring-managed beans to be exported as MBeans:
@ManagedResource(objectName="pirates:name=PirateService")
public interface PirateService {
@ManagedOperation(
description="Get the pirate list")
public List<Pirate> getPirateList();
}
Here, the PirateService has been annotated to be exported as a
MBean and its getPirateList() method is a managed operation.




Aspectj Annotations


SR
Annotaion
Use
Explanation
1
@Aspect
Type
Declares a class to be an aspect.
2
@After
Method
Declares a method to be called after a
pointcut completes.

3
@AfterReturning
Method
Declares a method to be called after a
pointcut returns successfully.
4
@AfterThrowing
Method
Declares a method to be called after a
pointcut throws an exception.
5
@Around
Method
Declares a method that will wrap the
pointcut.
6
@Before
Method
Declares a method to be called before
proceeding to the pointcut.

7
@DeclareParents
Static Field
Declares that matching types should be given new parents—that is, it introduces new functionality into matching types.
8
@Pointcut
Method
Declares an empty method as a pointcut  placeholder method.

What’s important to note, however, is that while you can use AspectJ annotations to define Spring aspects, those aspects will be defined in the context of Spring AOP and will not be handled by the AspectJ runtime. This is significant because Spring AOP is limited to proxying method invocations and does not provide for the more exotic pointcuts (constructor interception, fieldinterception, etc.) offered by AspectJ.
Annotating Aspects
To use AspectJ annotations to create Spring aspects, you’ll first need to provide a bit of Spring XML plumbing:
<beans xmlns="http://www.springframework.org/schema/
beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/
schema/beans
http://www.springframework.org/schema/beans/
spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/springaop-
2.5.xsd">
…
<aop:aspectj-autoproxy/>
…
</beans>
The <aop:aspectj-autoproxy> element tells Spring to watch for
beans annotated with AspectJ annotations and, if it finds any, to
use them to create aspects. Then you can annotate bean classes
to be aspects:
@Aspect
public class ChantySinger {
@Pointcut(“execution(* Pirate.plunder(..))”)
public void plunderPC() {}
@Before(“plunderPC()”)
public void singYoHo() {
…
}
@AfterReturning(“plunderPC()”)
public void singAPiratesLifeForMe() {
…
}
}
This simple annotation-based aspect has a pointcut that is triggered by the execution of a plunder() method on the Pirate class. Before the Pirate.plunder() method is executed, the singYoHo() method is called. Then, after the Pirate.plunder() method returns successfully, the singAPiratesLifeForMe()



JSR -250 Annotation

SR
Annotation
Use
Explanation
1
@PostConstruct
Method
Indicates a method to be invoked after a bean has been created and dependency injection is complete. Used to perform any initialization work necessary.

2
@PreDestroy
Method
Indicates a method to be invoked just
before a bean is removed from the Spring context. Used to perform any cleanup work necessary.
3
@Resource
@Resource
Indicates that a method or field should be injected with a named resource (by default, another bean).

Wiring Bean Properties with @Resource Using @Resource, you can wire a bean property by name:
public class Pirate {
@Resource
private TreasureMap treasureMap;
}
In this case, Spring will attempt to wire the “treasureMap” property with a reference to a bean whose ID is “treasureMap”. If you’d rather explicitly choose another bean to wire into the property, specify it to the name attribute:
public class Pirate {
@Resource(name=”mapToSkullIsland”)
private TreasureMap treasureMap;
}
Initialization and Destruction Methods Using JSR-250’s @PostConstruct and @PreDestroy methods,
you can declare methods that hook into a bean’s lifecycle. For  example, consider the following methods added to the Pirate
class:
public class Pirate {
…
@PostConstruct
public void wakeUp() {
System.out.println(“Yo ho!”);
}
@PreDestroy
public void goAway() {
System.out.println(“Yar!”);
}
}
As annotated, the wakeUp() method will be invoked just after Spring instantiates the bean and goAway() will be invoked just before the bean is removed from the Spring container.


Testing Annotations

These annotations are useful for creating unit tests in the JUnit 4 style that depend on Spring beans and/or require a transactional context.


SR
Annotation
Use
Explanation
1
@AfterTransaction
Method
IUsed to identify a method to be invoked after a transaction has
completed.
2
@BeforeTransaction
Method
Used to identify a method to be
invoked before a transaction starts.
3
@ContextConfiguration
Type
Configures a Spring application
context for a test.
4
@DirtiesContext
Method
Indicates that a method dirties the Spring container and thus it must
be rebuilt after the test completes.
5
@IfProfileValue
Type,
Method
Indicates that the test class or method is enabled for a specific profile configuration.
6
@NotTransactional
Method
Indicates that a test method must
not execute in a transactional
context.
7
@ProfileValueSourceConfiguration
Type
Identifies an implementation of a
profile value source. The absence
of this annotation will cause profile
values to be loaded from system
properties.
8
@Repeat
Method
Indicates that the test method must
be repeated a specific number of
times.
9
@Rollback
Method
Specifies whether or not the transaction for the annotated
method should be rolled back
or not.
10
@TestExecutionListeners
Type
Identifies zero or more test execution listeners for a test class.
11
@Timed
Method
Specifies a time limit for the test
method. If the test does not complete before the time has expired, the test will fail.
12
@TransactionConfiguration
Type
Configures test classes for transactions, specifying the
transaction manager and/or the
default rollback rule for all test
methods in a test class.


Writing a Spring-Aware Test
The key to writing a Spring-aware test is to annotate the test class with @RunWith, specifying SpringJUnit4ClassRunner as the class runner behind the test:
@RunWith(SpringJUnit4ClassRunner.class)
public class PirateTest {
…
}
In this case, the Spring test runner will try to load a Spring application context from a file named PirateTest-context.xml. If you’d rather specify one or more XML files to load the application context from, you can do that with @ContextConfiguration:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "pirates.xml" })
public class PirateTest {
…
}
With test configured to load a Spring application context, you now may request that Spring autowire properties of the test class with beans from the Spring context:

Writing a Spring-Aware Test, continued
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "pirates.xml" })
public class PirateTest {
@Autowired
private Pirate pirate;
@Autowired
private TreasureMap treasureMap;
@Test
public void annotatedPropertyShouldBeAutowired() {
assertNotNull(pirate.getTreasureMap());
assertEquals(treasureMap, pirate.getTreasureMap());
}
}
In this case, the pirate and treasureMap properties will be wired with the beans whose ID are “pirate” and  “treasureMap”, respectively. Accessing the Spring Context in a Test If you need the Spring application context itself in a test, you can autowire it into the test the same as if it were a bean in the context:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "pirates.xml" })
public class PirateTest {
@Autowired
private Pirate pirate;
@Autowired
private ApplicationContext applicationContext;
@Test
public void annotatedPropertyShouldBeAutowired() {
assertNotNull(pirate.getTreasureMap());
assertEquals(applicationContext.
getBean("treasureMap"), pirate
.getTreasureMap());
}
}

Very Impotent Links for Java and Portal development development

Jar Download for Spring  Maven Or Gradel Dependency for Spring