What is Struts ?
Apache Struts is a free open-source framework for creating Java web applications.
Difference Between Struts 1 and Struts 2
Struts1 was an action- oriented framework that implemented a Model-View-Controller (MVC) separation of concerns in its architecture.
Struts2 is a brand new implementation of those same MVC principles in an action-oriented framework.
Difference Between Struts 1 and Struts 2.
Why we need MVC framework?
Why we need Struts ?
To Bind request parameters to Java types
To Validate data
To Make calls to business logic
To Make calls to the data layer
To Render presentation layer (HTML, and so on)
To Provide internationalization and localization
STRUTS2 FEATURES:
Interceptors for layering cross-cutting concerns away from action logic;
Annotation-based configuration to reduce or eliminate XML configuration;
Powerful expression language, Object-Graph Navigation Language(OGNL), that transverses the entire framework.
Mini-MVC–based tag API that supports modifiable and reusable UI components.
The framework's architecture and tags are buzzword compliant.
Struts works well with conventional REST applications and with nouveau technologies like SOAP and AJAX.
Resources for Understanding and Using Struts
Sturts developers guide Link http://struts.apache.org/2.x/docs/core-developers-guide.html
Struts examples http://struts.apache.org/2.x/docs/strutsxml-examples.html
Saturday, November 20, 2010
Differences Between Struts 1 and Struts 2
Base Action class is abstract class in Struts1 where as it is Interface in Struts2 and
also provides a base ActionSupport class that implements commonly used interfaces.
Struts 1 Actions are singletons and must be thread-safe where as
Struts 2 doesn't have thread-safety issues as Action objects are instantiated for each request.
Actions are dependent on the servlet API , as HttpServletRequest and HttpServletResponse are passed to execute method in Struts1 but in case of Struts 2, Actions are not container dependent because they are made simple POJOs. In struts 2, the servlet contexts are represented as simple Maps.
Testing Struts2 action is simple as there is no Servlet API usage in execute method .
Struts 1 recieves an input by creating an ActionForm object which extend a ActionForm base class.
Other JavaBeans classes cannot be used.
Struts 2 requires Action properties as input properties ,these Input properties may be rich object types.
Struts 1 integrates with JSTL, so it uses the JSTL EL(Expression Language http://en.wikipedia.org/wiki/Expression_Language).
Struts 2 can use JSTL, but the framework also supports a more powerful language called "Object Graph Notation Language" (OGNL).
Struts 1 binds objects into the page context by using the standard JSP mechanism.
Struts 2 uses a ValueStack technology to make the values accessible to the taglibs
without coupling the view to the object to which it is rendering.
Struts 1 ActionForm properties are almost in the form of Strings. Commons-Beanutils are
used by used by Struts 1 for type conversion.
Struts 2 uses OGNL for type conversion.
Struts 1 uses manual validation that is done via a validate method on the ActionForm, or
by using an extension to the Commons Validator.
Struts 2 allows manual validation that is done by using the validate method and the XWork Validation framework.
Each module in Struts 1 has a separate Request Processors (lifecycles), while all the Actions in the module must share the same lifecycle.
In Struts 2 different lifecycles are created on a per Action basis via Interceptor Stacks.
For Full comparision Between Struts 1 and Struts 2
http://struts.apache.org/2.0.14/docs/comparing-struts-1-and-2.html
Tags
Struts,Differences,Frame Works,
also provides a base ActionSupport class that implements commonly used interfaces.
Struts 1 Actions are singletons and must be thread-safe where as
Struts 2 doesn't have thread-safety issues as Action objects are instantiated for each request.
Actions are dependent on the servlet API , as HttpServletRequest and HttpServletResponse are passed to execute method in Struts1 but in case of Struts 2, Actions are not container dependent because they are made simple POJOs. In struts 2, the servlet contexts are represented as simple Maps.
Testing Struts2 action is simple as there is no Servlet API usage in execute method .
Struts 1 recieves an input by creating an ActionForm object which extend a ActionForm base class.
Other JavaBeans classes cannot be used.
Struts 2 requires Action properties as input properties ,these Input properties may be rich object types.
Struts 1 integrates with JSTL, so it uses the JSTL EL(Expression Language http://en.wikipedia.org/wiki/Expression_Language).
Struts 2 can use JSTL, but the framework also supports a more powerful language called "Object Graph Notation Language" (OGNL).
Struts 1 binds objects into the page context by using the standard JSP mechanism.
Struts 2 uses a ValueStack technology to make the values accessible to the taglibs
without coupling the view to the object to which it is rendering.
Struts 1 ActionForm properties are almost in the form of Strings. Commons-Beanutils are
used by used by Struts 1 for type conversion.
Struts 2 uses OGNL for type conversion.
Struts 1 uses manual validation that is done via a validate method on the ActionForm, or
by using an extension to the Commons Validator.
Struts 2 allows manual validation that is done by using the validate method and the XWork Validation framework.
Each module in Struts 1 has a separate Request Processors (lifecycles), while all the Actions in the module must share the same lifecycle.
In Struts 2 different lifecycles are created on a per Action basis via Interceptor Stacks.
For Full comparision Between Struts 1 and Struts 2
http://struts.apache.org/2.0.14/docs/comparing-struts-1-and-2.html
Tags
Struts,Differences,Frame Works,
Thursday, September 2, 2010
Java Annotations Introduction
Annotations
Annotations provide data about a program that is not part of the program itself. They have no direct effect on the operation of the code they annotate.
Annotations have a number of uses, among them:
There are two types of annotations available:
- Information for the compiler — Annotations can be used by the compiler to detect errors or suppress warnings.
- Compiler-time and deployment-time processing — Software tools can process annotation information to generate code, XML files, and so forth.
- Runtime processing — Some annotations are available to be examined at runtime.
An annotation is the meta-tag that you will use in your code to give it some life.
- Simple annotations: These are the basic types supplied with Tiger, which you can use to annotate your code only; you cannot use those to create a custom annotation type.
- Meta-annotations: These are the annotation types designed for annotating annotation-type declarations. Simply speaking, these are called the annotations-of-annotations.
Annotation type is used for defining an annotation.
Annotation Types
An Annotation type definition takes an "at" (@) sign, followed by the interface keyword plus the annotation name.
On the other hand, an annotation takes the form of an "at" sign (@), followed by the annotation type.
There are three annotation types:
- Marker: Marker type annotations have no elements, except the annotation name itself.
- Single-Element: Single-element, or single-value type, annotations provide a single piece of data only. This can be represented with a data=value pair or, simply with the value (a shortcut syntax) only, within parenthesis.
- Full-value or multi-value: Full-value type annotations have multiple data members. Therefore, you must use a full data=value parameter syntax for each member.
Example to Define an Annotation (Annotation type)
public @interface MyAnnotation {
String doSomething();
}
String doSomething();
}
Singel element Usage:
MyAnnotation (doSomething="What to do")
public void mymethod() { .... }
multi-value Usage
@MyAnnotation (doSomething="What to do", count=1, date="09-09-2005")
public void mymethod() { .... }
Simple Annotations
There are three annotation types that are predefined by the language specification itself:
- Deprecated
- Override
- Suppresswarnings
@Deprecated
annotation indicates that the marked element is deprecated and should no longer be used. The compiler generates a warning whenever a program uses a method, class, or field with the@Deprecated
annotation.
The Javadoc tag starts with a lowercase "d" and the annotation starts with an uppercase "D".
// Javadoc comment follows /** * @deprecated * explanation of why it was deprecated */ @Deprecated static void deprecatedMethod() { }
@Override
annotation informs the compiler that the element is meant to override an element declared in a superclass .
// mark method as a superclass method // that has been overridden @Override int overriddenMethod() { }
@SuppressWarnings
annotation tells the compiler to suppress specific warnings that it would otherwise generate.
// use a deprecated method and tell // compiler not to generate a warning @SuppressWarnings("deprecation") void useDeprecatedMethod() { objectOne.deprecatedMethod(); //deprecation warning - suppressed }
Meta-Annotations
Meta-annotations, which are actually known as the annotations of annotations, contain four types. These are:
The target annotation indicates the targeted elements of a class in which the annotation type will be applicable. It contains the following enumerated types as its value:
- Target
- Retention
- Documented
- Inherited
The retention annotation indicates where and how long annotations with this type are to be retained. In simple it indicates the scope of the annotation Type.
- @Target(ElementType.TYPE)—can be applied to any element of a class
- @Target(ElementType.FIELD)—can be applied to a field or property
- @Target(ElementType.METHOD)—can be applied to a method level annotation
- @Target(ElementType.PARAMETER)—can be applied to the parameters of a method
- @Target(ElementType.CONSTRUCTOR)—can be applied to constructors
- @Target(ElementType.LOCAL_VARIABLE)—can be applied to local variables
- @Target(ElementType.ANNOTATION_TYPE)—indicates that the declared type itself is an annotation type
There are three values:
The documented annotation indicates that an annotation with this type should be documented by the javadoc tool. By default, annotations are not included in javadoc. But if @Documented is used, it then will be processed by javadoc-like tools and the annotation type information will also be included in the generated document.
- RetentionPolicy.SOURCE—Annotations with this type will be by retained only at the source level and will be ignored by the compiler
- RetentionPolicy.CLASS—Annotations with this type will be by retained by the compiler at compile time, but will be ignored by the VM
- RetentionPolicy.RUNTIME—Annotations with this type will be retained by the VM so they can be read only at run-time
Inherited annotation
It is important to understand the rules relating to inheritance of annotations, as these have a bearing on join point matching based on the presence or absence of annotations.
By default annotations are not inherited.The annotation used on the super class doesn't get inherited to sub class,unless the annotation (@MyAnnotation used in the following example)used on super class has the @Inherited meta-annotation.
Annotation are not be inheritable in case of extending Interface even if they have @Inherited meta-annotation.
@MyAnnotation class Super { @Oneway public void foo() {} } class Sub extends Super { public void foo() {} }Then Sub does not have the MyAnnotation annotation, and Sub.foo() is not an @Oneway method, despite the fact that it overrides Super.foo() which is.
If an annotation type @MyAnnotation has the meta-annotation @Inherited then an annotation of that type on a class will cause the annotation to be inherited by sub-classes. So, in the example above, if the MyAnnotation type had the @Inherited attribute, then Sub would have the MyAnnotation annotation.
@Inherited annotations are not inherited when used to annotate anything other than a type.
A type that implements one or more interfaces never inherits any annotations from the interfaces it implements(they are inheritable only if subclass extends another super class).
Every compiler warning belongs to a category. The Java Language Specification lists two categories: "deprecation" and "unchecked." The "unchecked" warning can occur when interfacing with legacy code written before the advent of generics .
To suppress more than one category of warnings, use the following syntax:
To suppress more than one category of warnings, use the following syntax:
@SuppressWarnings({"unchecked", "deprecation"})
Annotation Processing:
The more advanced uses of annotations include writing an annotation processor that can read a Java program and take actions based on its annotations.
The more advanced uses of annotations include writing an annotation processor that can read a Java program and take actions based on its annotations.
To make annotation information available at runtime, the annotation type itself must be annotated with
as follows:
@Retention(RetentionPolicy.RUNTIME)
, as follows:
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@interface AnnotationForRuntime {
// Elements that give information
// for runtime processing
}
Resources
2)An Introduction to Java Annotations
http://www.developer.com/java/other/article.php/3556176/An-Introduction-to-Java-Annotations.htm
3)Chapter 2. Annotations
http://www.eclipse.org/aspectj/doc/released/adk15notebook/annotations.html
4)Java Custom Annotations (Has good Example on Inherited meta-annotation)
http://technicalmumbojumbo.wordpress.com/2008/01/13/java-custom-annotations/
**********************************************************************************************************************************************
**********************************************************************************************************************************************
Friday, August 27, 2010
Java Command-line flags for the JVM
DisableExplicitGC
-XX:+DisableExplicitGC flag automatically turns a System.gc() call into a no-op
HeapDumpOnOutOfMemoryError
-XX:+HeapDumpOnOutOfMemory
Setting this command tells the JVM to take a "heap dump snapshot" and save it to a file for processing.
You can specify the actual path to which the file is saved using the corresponding -XX:HeapDumpPath flag.
java -X lists all the non-standard (but mostly safe) arguments that the JVM provides — things like:
* -Xint, which runs the JVM in interpreted mode (which can be useful for testing whether the JIT compiler is actually having an effect on your code or verifying if you have a bug in the JIT compiler).
* -Xloggc:, which does the same thing as -verbose:gc but logs to a file instead of spe
Source
http://www.ibm.com/developerworks/java/library/j-5things11/index.html
Java,Java Command Line Flags
-XX:+DisableExplicitGC flag automatically turns a System.gc() call into a no-op
HeapDumpOnOutOfMemoryError
-XX:+HeapDumpOnOutOfMemory
Setting this command tells the JVM to take a "heap dump snapshot" and save it to a file for processing.
You can specify the actual path to which the file is saved using the corresponding -XX:HeapDumpPath flag.
java -X lists all the non-standard (but mostly safe) arguments that the JVM provides — things like:
* -Xint, which runs the JVM in interpreted mode (which can be useful for testing whether the JIT compiler is actually having an effect on your code or verifying if you have a bug in the JIT compiler).
* -Xloggc:, which does the same thing as -verbose:gc but logs to a file instead of spe
Source
http://www.ibm.com/developerworks/java/library/j-5things11/index.html
Java,Java Command Line Flags
MySQL Joins
Mysql Inner Join returns the set of only those records which matches in one table with another.
If I do a regular JOIN (with none of the keywords INNER, OUTER, LEFT or RIGHT),
then I get all records that match in the appropriate way in the two tables,
and records in both incoming tables that do not match are not reported:
mysql> select name, phone, selling from demo_people join demo_property
on demo_people.pid = demo_property.pid;
If I do a LEFT JOIN, I get all records that match in the same way and
IN ADDITION I get an extra record for each unmatched record
in the left table of the join - thus ensuring (in following example) that every
PERSON gets a mention ie from demo_people table:
mysql> select name, phone, selling from demo_people left join demo_property
on demo_people.pid = demo_property.pid;
If I do a RIGHT JOIN, I get all the records that match and
IN ADDITION I get an extra record for each unmatched record in the right table
of the join - in following example, that means that each property (from demo_property )
gets a mention even if we don't have seller details.
mysql> select name, phone, selling from demo_people right join demo_property
on demo_people.pid = demo_property.pid;
Mysql Full Join is used to return all the records from both left and right outer join.
The joined table contain all records from both tables and fill nulls values
for those missing matches on either side.
The Full Join in Mysql is the outcome result set of left outer join and right outer join using UNION clause.
mysql> select * from Roseindia as R left outer join newstrack as N
-> on R.empid=n.empid
-> Union
-> select * from Roseindia as R right outer join newstrack N
-> on R.empid=n.empid;
An INNER JOIN does a full join, just like the first example, and
the word OUTER may be added after the word LEFT or RIGHT in the 2nd & 3rd examples -
it's provided for ODBC compatibility and doesn't add an extra capabilities.
Mysql Natural Join is a specialization of equi-joins. The join compares all columns in
both tables that have the same column-name in both tables that have column name in
the joined table. The resulting set include only one column for each pair of the same named column.
More Info http://www.roseindia.net/sql/sqljoin/mysql-natural-join.shtml
CROSS JOIN:This type of join is the simplest join. The cross join result in cartesian product of all the records from two tables
mysql> select * from roseindia cross join newstrack;
INNER JOIN OR EQUI JOIN:This is the type of join where tables are combined based on a common column.
OUTER JOIN: Join is used to combine all rows of one table with matching rows from the other table and also show unmatchable records from other table. It is used whenever multiple tables must be accessed through a SQL SELECT statement.
http://www.roseindia.net/sql/sqljoin/mysql-join-query.shtml
Sources:
http://www.wellho.net/mouth/158_MySQL-LEFT-JOIN-and-RIGHT-JOIN-INNER-JOIN-and-OUTER-JOIN.html
http://www.roseindia.net/sql/sqljoin/mysql-full-join.shtml
For more info:
http://www.keithjbrown.co.uk/vworks/mysql/mysql_p5.php
Tags:MySQL,SQL Joins,
If I do a regular JOIN (with none of the keywords INNER, OUTER, LEFT or RIGHT),
then I get all records that match in the appropriate way in the two tables,
and records in both incoming tables that do not match are not reported:
mysql> select name, phone, selling from demo_people join demo_property
on demo_people.pid = demo_property.pid;
If I do a LEFT JOIN, I get all records that match in the same way and
IN ADDITION I get an extra record for each unmatched record
in the left table of the join - thus ensuring (in following example) that every
PERSON gets a mention ie from demo_people table:
mysql> select name, phone, selling from demo_people left join demo_property
on demo_people.pid = demo_property.pid;
If I do a RIGHT JOIN, I get all the records that match and
IN ADDITION I get an extra record for each unmatched record in the right table
of the join - in following example, that means that each property (from demo_property )
gets a mention even if we don't have seller details.
mysql> select name, phone, selling from demo_people right join demo_property
on demo_people.pid = demo_property.pid;
Mysql Full Join is used to return all the records from both left and right outer join.
The joined table contain all records from both tables and fill nulls values
for those missing matches on either side.
The Full Join in Mysql is the outcome result set of left outer join and right outer join using UNION clause.
mysql> select * from Roseindia as R left outer join newstrack as N
-> on R.empid=n.empid
-> Union
-> select * from Roseindia as R right outer join newstrack N
-> on R.empid=n.empid;
An INNER JOIN does a full join, just like the first example, and
the word OUTER may be added after the word LEFT or RIGHT in the 2nd & 3rd examples -
it's provided for ODBC compatibility and doesn't add an extra capabilities.
Mysql Natural Join is a specialization of equi-joins. The join compares all columns in
both tables that have the same column-name in both tables that have column name in
the joined table. The resulting set include only one column for each pair of the same named column.
More Info http://www.roseindia.net/sql/sqljoin/mysql-natural-join.shtml
CROSS JOIN:This type of join is the simplest join. The cross join result in cartesian product of all the records from two tables
mysql> select * from roseindia cross join newstrack;
INNER JOIN OR EQUI JOIN:This is the type of join where tables are combined based on a common column.
OUTER JOIN: Join is used to combine all rows of one table with matching rows from the other table and also show unmatchable records from other table. It is used whenever multiple tables must be accessed through a SQL SELECT statement.
http://www.roseindia.net/sql/sqljoin/mysql-join-query.shtml
Sources:
http://www.wellho.net/mouth/158_MySQL-LEFT-JOIN-and-RIGHT-JOIN-INNER-JOIN-and-OUTER-JOIN.html
http://www.roseindia.net/sql/sqljoin/mysql-full-join.shtml
For more info:
http://www.keithjbrown.co.uk/vworks/mysql/mysql_p5.php
Tags:MySQL,SQL Joins,
Tuesday, August 24, 2010
Spring aware interfaces
For the most part, beans running in the Spring container, They don't know (or
even need to know) their names or even that they are running within a Spring
container. This is usually a good thing because if a bean is aware of the container,
then it becomes coupled with Spring and may not be able to exist outside of the container.
A well designed component should not have direct dependencies on its container,
But sometimes, beans need to know more. Sometimes they need to know the
truth who they are and where they are running.
This capability comes in the form of the
Bean-NameAware,
BeanFactoryAware,
and ApplicationContextAware interfaces .
By implementing these three interfaces,
beans can be made aware of their name, their BeanFactory, and their ApplicationContext, respectively.
However, by implementing these interfaces, a bean becomes coupled with Spring.
The Spring container tells a bean what its name is through the BeanNameAware
interface.
public interface BeanNameAware {
void setBeanName(String name);
}
Spring's ApplicationContextAware and BeanFactoryAware interfaces enable a
bean to be aware of its container. These interfaces declare a setApplication-
Context() method and a setBeanFactory() method, respectively.
When to use?
your bean needs access to parameterized text messages in a message source.
Or maybe it needs to be able to publish application
events for application event listeners to respond to. Whatever the case, your bean
should be aware of the container in which it lives.
Spring,Java,Spring FAQ,
even need to know) their names or even that they are running within a Spring
container. This is usually a good thing because if a bean is aware of the container,
then it becomes coupled with Spring and may not be able to exist outside of the container.
A well designed component should not have direct dependencies on its container,
But sometimes, beans need to know more. Sometimes they need to know the
truth who they are and where they are running.
This capability comes in the form of the
Bean-NameAware,
BeanFactoryAware,
and ApplicationContextAware interfaces .
By implementing these three interfaces,
beans can be made aware of their name, their BeanFactory, and their ApplicationContext, respectively.
However, by implementing these interfaces, a bean becomes coupled with Spring.
The Spring container tells a bean what its name is through the BeanNameAware
interface.
public interface BeanNameAware {
void setBeanName(String name);
}
Spring's ApplicationContextAware and BeanFactoryAware interfaces enable a
bean to be aware of its container. These interfaces declare a setApplication-
Context() method and a setBeanFactory() method, respectively.
When to use?
your bean needs access to parameterized text messages in a message source.
Or maybe it needs to be able to publish application
events for application event listeners to respond to. Whatever the case, your bean
should be aware of the container in which it lives.
Spring,Java,Spring FAQ,
Wednesday, August 18, 2010
UDH in Binary SMS / Multipart SMS messsages
Multipart SMS messaging
By design, SMS is developed to send up to 140 bytes of user data. All user data is send in the 'User Data' part of the SMS packet. Because SMS text messages are encoded using 7-bit characters you can send up to 160 characters in a single SMS message. When sending Unicode text, you can only send 70 characters per single SMS message.It is however possible to split up text and data messages and send them using multiple SMS messages. The receiving party will be able to combine the messages to the original message. This is called Segmentation and Reassembly (SAR). When sending multipart SMS messages you will be charged for every single SMS message sent.
Sending Multipart Messages through a GSM phone or modem
To send enhanced content, a so called user data header (UDH) is added add the beginning of the user data block of the SMS. When using an UDH, there is less data left for user data in the User Data field (140 - length of UDH).An UDH can be used to send multipart messages, smart messaging (ringtones, WAP push, pictures etc), voicemail indications and other services. In this article we will only discuss the use of UDH to send multipart text messages.
The UDH for message concatenation will only take 5 bytes, so there are 135 bytes left for the user data. When sending concatenated text messages, you can send 153 characters when using 7-bit text, when using Unicode 67 characters per part.
Byte | Value | Description |
---|---|---|
01 | 00 | Information Element Identifier: Concatenated short message, 8bit reference number |
02 | 03 | Information Element Data Length (always 03 for this UDH) |
03 | A4 | Information Element Data: Concatenated short message reference, should be same for all parts of a message |
04 | 03 | Information Element Data: Total number of parts |
05 | 01 | Information Element Data: Number of this part (1/3) |
SMS 1 User Data: 00 03 A4 03 01 [ 135 bytes of message data ]
SMS 2 User Data: 00 03 A4 03 02 [ 135 bytes of message data ]
SMS 3 User Data: 00 03 A4 03 03 [ 30 bytes of message data ]
Source and Full Info
User Data Header (UDH)
The SMS that make up a concatenated SMS are related together by using the User Data Header (UDH) of an SMS. The UDH is a collection of bytes which can be put at the start of the SMS content. It can be used to control what happens to the rest of the content. For instance, it was used to send the older style of Nokia picture messages.To indicate that the content contains a UDH, a flag on the SMS called the UDH Indicator (UDHI) must be turned on. This tells the phone that it must separate the UDH from the rest of the content.
Figure:Format of an SMS with a UDH
As already stated, a UDH can control various things and so can contain various commands. These commands are called Information Elements (IE's). These IE's always take the following format: an Identity Element Identifier (IEI) followed by the Length of the IE Data (IEDL) followed by the IE Data (IED). A UDH can contain 1 or more of these IE's.
Resources
1)Parameters with Definitions for Sending SMS and MMS 2)Sending Binary Messages using NowSMS
Labels:
Binary SMS,
Multi SMS,
SMPP,
SMS Sending,
UDH,
User Data Header
Subscribe to:
Posts (Atom)