Saturday, October 10, 2009

CSS Sprites


CSS Sprites
Almost all the web sites which we develop will have some basic functionality like, e-mail, print, rss feed, logo, etc.. And usually these will be provided as a image link to the user. Obviously we will be referring to separate image for each link. And we know for each asset, whether it is a image or css the browser makes a separate call to web server to get the information. Since we have separate image for each functionality the number of hops increases.

Till now I have also did the same thing :) , i.e. by including separate image for each one. After knowing about CSS Sprites I was more inclined towards it.

Below is the sample data shows the actual time taken by the HTML markup when compared to other files that supports the HTML formation:


Source: css-tricks.com
As given most of the time browser spends to get the supporting files like images. Consider if we have single image which all the images contained in it. Then the browser just needs to download just a single image which can be served to all. This reduces number of hops dramatically and your performance of the application will increase. This technique of combining the smaller images into single large image (not very large) is called CSS Sprites.

As a contrary many will believe that having single large image file will increase the size of the image file which will have impact on the performance again. But, here is the comparison:

I tested with an simple web application where I had ten small icons, for which
1) Having individual image took:
Number of HTTP Requests: 10
Total Size of the Images downloaded: 12.3 kb
2) Having a single image file using CSS Sprite concept
Number of HTTP Requests: 1
Total Size of the Images downloaded: 7.62 kb
Image File can be created using : http://www.csssprites.com/

Here I am taking about a simple web application, consider large applications where we usually have plenty of images being used. In those scenario CSS Sprite will be very handy.

I thought of sharing this info to every one. For more details about CSS Sprite, use the below links:
Reference:

Regards,
Elango

Friday, August 28, 2009

Reading Properties file using ResourceBundle - native2ascii ANT task

Introduction
Most of the portlets or any web application that we usually create, will make use of the properties file to read and display the localized contents/value present in it. The .properties file created will be of encoding type ASCII. And as we know, for displaying the japenese and other language it should be of UTF-8 characters. Even if the properties files are stored as UTF-8 encoding type, still the content displayed for some of the languages would be junk. The reason being when the properties file is read using resource bundle object it doesn't know whether the characters are of UTF-8 format. There should be mechanism to make this properties file available for the web application.

We spent almost two to figure out this, hope this solves some time for you :)

Below are the steps that should be followed to make your web application to display the locale specific text:

Reading Properties file using Resource Bundle
a) Creating a proper properties file
First step would be to create a properties file with the encoding type UTF-8 format for all the locales. Inside the properties file have the locale specific text. eg. for japenese properties file,

portal.message=ポータル

Eventhough locale specific text is available for the web application to use it, the JVM treats all the contents as similar format. So option is to change the JVM settings to allow the UTF-8 characters. The other option would be to convert the existing UTF-8 characters into UNICODE characters so that it can be displayed normally.

But the problem is to have the unicode value in the properties file. It is hard to maintain and also not human readable. eg. the UNICODE value would be:

portal.message=ポータル

So there needs a mechanism to convert the UTF-8 characters to UNICODE at runtime or at compile time. And mostly we use ANT as the tool to build our projects to create the war file. So the idea should be implemented at the build level so that even the properties file can be human readable and also understood by underlying JVM.

b) Using native2ascii ANT task
Fortunately ANT provides option to use the native2ascii utility.
native2ascii: Converts files from native encodings to ASCII with escaped Unicode. A common usage is to convert source files maintained in a native operating systenm encoding to ASCII prior to compilation.
The option would be is to convert the properties file using the native2ascii utility so that the properties file entries will be converted to Unicode format. The Unicode format can be easliy be understood by any Operating system.
Sample ANT code for this is given below:

<!-- converting the properties file values to unicoce character values-->
<native2ascii encoding="UTF-8" src="${portletmodulename}/src" dest="${warfiledir}/WEB-INF/classes" includes="**/*.properties"/>

This convert all the UTF-8 characters into Unicode format at the build, so that even the web apps can use it. And also properties stored in the filesystem will be UTF-8 characters which is easily maintainable.
Reference:


Regards,
Elango

Removing Unwanted spaces between the portlets

Introduction
One common problem which the portal developer come accross is the spacing issue that is formed between the portlets. Even though we develop themes and skins properly, the space between the portlets will still be exisiting. And in most of the scenario we want that to be controlled.

Here are few steps that needs to be followed to avoid the spaces between the portlets.

Removing Unwanted Spaces between the Portlets
a) Changing the default Skin for the themes
The custom theme or the exisiting portal theme which is applied to the portal page will have the default skin. To remove the unwanted spaces which is occuring between the portlets, first the default skin needs to be changed to "IBM No Skin" which does not contain any drag and drop feature code.

b) Modifying the applied Skin
The Skin which is applied to the page has to be modified to remove the unwanted spaces. All the javascript code related to drag and drop should be removed from skin. If "IBM No Skin" is used for the portlet then this step is not needed, as it is by default will not contain any drag and drop related code.

The above changes will remove all the extra spaces between the portlets.

Drawbacks:
Even using the above changes we get rid of the extra spaces, we are also losing the drag and drop feature. The places where drag and drop needs to be implemented, the above approach can't be used. Till WPS6.1 i believe this is the only way where you can remove the spaces between the portlets.

Regards,
Elango

Friday, August 21, 2009

Using TCP monitor in WPS

TCP Monitor:

You can use the TCP monitor which does exist in portal server itself. if you have installed portal server then you can use it using the following ways:

a) Open the cmd prompt, and navigate to AppServer\bin folder

> cd C:\IBM\WebSphere\AppServer\bin\

b) Execute the following command to open the tcp monitor:

C:\IBM\WebSphere\AppServer\bin> java -Djava.ext.dirs=C:\IBM\WebSphere\AppServer\runtimes\ com.ibm.ws.webservices.engine.utils.tcpmon

{give the runtimes path accordingly}

The tcp monitor should have opened by now. For more details: http://publib.boulder.ibm.com/infocenter/wasinfo/v7r0/index.jsp?topic=/com.ibm.websphere.base.doc/info/aes/ae/twbs_tracewbs.html


Regards,

Elango

Back door entry for Portal

Introduction
Did you ever faced a situation where you have deleted the login portlet by chance :) Are your custom theme hides the login portlet itself. But still IBM Web Sphere Portal allows us to login without using the login portlet i.e. back door entry for the admins.

Back Door URL

The URL to login:

https://:/wps/portal/cxml/04_SD9ePMtCP1I800I_KydQvyHFUBADPmuQy?userid=&password=

Example: https://localhost:10035/wps/portal/cxml/04_SD9ePMtCP1I800I_KydQvyHFUBADPmuQy?userid=wpsadmin&password=wpspwd


Regards,

Elango

Enabling Automatic JSP reloading

Introduction
Eventhough in IBM Web Sphere Portal 6.1(refered as Portal going forward) you can deploy deploy the custom theme as a separate war file, only the changes made in the defaul.jsp file will be reflected relatime. Other supporting file like head.jspf files etc.. even if you change the files, it practically it needs a application restart (In Portal 6.0 it required server restart. some what a improvement in that sense:) )

Particulary while developing the themes it would be cool if the JSP reloading is enabled. Make sure that the JSP reloading is disabled in Production and Stagging environment. Otherwise you will run into performance issues :)

Enabling Automatic JSP reloading
Steps followed:
a) Open the file<>/config/cells/cell_name/applications/ wps.ear/deployments/wps/wps.war/WEB-INF/ibm-web-ext.xmi

ie. Say the portal sever is installed in C:\ibm drive, then the path would be : C:\ibm\WebSphere\profiles\wp_profile\config\cells\citizen\applications\wps.ear\deployments\wps\wps.war\WEB-INF\ibm-web-ext.xmi

b) Change the value in the tag , reloadingEnabled to true

c) restart the portal server from WAS console

After making the above changes, changes are reflected immediatley when the files like head.jspf etc.. are changed in themes/skins

Things to Remember:
a) Performance issue will be there. use only for the testing purpose i.e. development environment. Dont use in PRODUCTION or STAGGING environment.
b) To view changes to a JSP that is included by another (parent) JSP, you must also change the parent JSP to indicate that it must be reloaded by the server.

Regards,
Elango

Saturday, May 23, 2009

JSP Coding Guidelines for Portlets

Introduction
Almost every one concentrates on using coding guidelines for Java code. Wondered if any one considered using coding guidelines for JSP page which contributes in creating the final HTML source which passes to the client.

Below is the list of guidelines which I have prepared for JSP (particulary JSP used in portlet applications) from my experience in different projects. Suggestions are welcomed to improve the list.

JSP Coding Guidelines for Portlets
a) Use Java style comments instead of HTML style
HTML comments will remain in the rendered content, adding to the document size and the amount of data that passes to the client. If you use Java style comments within your JSPs, instead, they are removed from the rendered source, along with the rest of the Java code. You must embed these comments within scriptlets:
<%//comments%>

b) Always use Upper case for HTML components
Eventhough HTML is case insentive, but w3c standars suggest to use always the upper case for HTML components. So in JSP all the HTML components should be in Upper case.
Eg: <TABLE>

c) Use Unique id for HTML components
Always use a unique id for a HTML or any components that are used inside JSP. Avoid using id for applying a style to a HTML components. Instead use class for applying same stye class to different HTML components

d) Make pages fully accessible
To allow end users with disabilities to be able to use your portlet, the JSPs should be fully enabled for keyboard-only control and other assistive technologies. Examples of accessibility enablement features include:

  • Use ALT attribute with images to define descriptive text of the image content.
  • Use <LABEL>tags to associate labels with form input controls, so that page readers will be able to associate prompts with inputs.
  • Do not use color alone to denote state or information. For example, using red to emphasize text does not help those who are color blind. Use bold or italics instead, or use color in conjunction with graphic changes.
  • Do not use voice or other sounds to convey information.
  • Final HTML page created out of JSP should not contain more than one H1 tag. (Again it is w3c standards)


e) URIs, HTML element name attributes, and JavaScript resources must be namespace encoded
Since many portlets can exist on a page, and it is possible that more than one portlet will produce content with like-named elements, there is a risk of namespace collision between elements, causing functional problems with the page.
Use the <portlet:namespace/>tag to encode such resources with the portlet instance name. For example, to add the tag to a FORM name:
<FORMname="”<portlet:namespace" method="post">form1” action=”<%=actionUrl%>”>
When the portlet is rendered on the page, the above tag would look something like this: <FORM action="”/wps/myportal/.cmd/ad/.ar/19807697/.c/201/.ce/502/.p/502”" name="”PC_202_form1”" method="POST">

f) Use IFRAMEs with caution
IFRAMEs are an easy way to include external content within a portlet, but undermine the whole portlet principle because the portlet API is just tunneled or side-stepped. Therefore, IFRAMEs should only be used for very special cases, such as surfacing legacy applications. Other potential issues to consider when using an IFRAME are:

  • The IFRAME fills its content based on a URL. The URL must be addressable by the browser; therefore the server that is the target of the URL must be accessible by the browser.
  • Not all browser levels support IFRAMEs.
  • If the content is larger than the IFRAME region, then enable horizontal and vertical scrolling to let the user scroll through the embedded content. Content which contains scrolling regions itself can make it difficult for the end user to manipulate all scrolling regions to view all embedded content, causing usability problems.


IBM Portal Users:
If IFRAMEs are used for integrating external applications then try to use web-app integrator provided by IBM Websphere Portal depending upon the type of the application.

g) Do not use pop-ups
Interactions within the portal are state-based, which means that the portal tracks your trail through the pages and portlets. Using the browser’s back button, or creating pop-up browser instances containing portlets, can cause the portal to lose track of your current state and cause problems. Other than using JavaScript prompts, there is no safe way to spawn pop-ups within the portal, unless the new link takes you to an external page, outside the portal. The alternative is to link to an external page within a new browser window (using the TARGET attribute on the anchor), so that the user is left within the portal in the original browser window.

h) Use portlet style classes instead of specific style-oriented attributes
Portal administrators and users can affect the look and feel of the portal by changing the portal theme and the portlet skins. Portlets can pick up on style changes by using styles defined in the portal theme’s cascading style sheet (Styles.css). For example, instead of decorating an element yourself, refer to the theme’s input class definition:
<input class="”buttonText”" type="”submit”">
Styles.css is loaded by the theme and should not be reloaded by a portlet.

i) Include HTML fragments only in JSPs
Because portlets contribute to the content of a larger page, they should only provide HTML fragments and should not have <TABLE>,<LABEL> , or <HTML>tags.
Make sure the fragments are well-formed to prevent unbalanced pages. Also, remember that the HTML fragment is being added to a table cell (<TD>) in the portal page.

j) Design the view to fit on a page with other portlets
Unlike servlet-based applications, portlets contribute a portion of a larger page. The size of the JSPs (in terms of horizontal and vertical span) can determine how easily the portlet can fit on a page with multiple columns and other portlets. A large portlet will “squeeze” other portlets off the screen and create large scrolling regions, resulting in usability issues.
Therefore, when designing a portlet’s JSPs, avoid unnecessary layout elements, focus the portlet’s view on the pertinent information, and consider whether the portlet is intended to be placed on pages with other portlets.
Use JSP for displaying the data/information to the user, try to move all your Business logic into Java code.

k) Minimize dependencies on JavaScript
Open Source Javascript brains will not accept this point. The point is not using the js completely instead try to use the standard js library like DOJO, JQuery, etc. which provides excellent browser compatability and also inbuilt widgets for rich UI.

If you are using your own java script implementations please read below:
Because JavaScript implementations and behavior differ widely among browser types and versions, the more your portlet depends on JavaScript, the more browser-dependent your portlet becomes. Additionally, the more of the page that is rendered using JavaScript, the more difficult it is to maintain and to extend to markups other than HTML. Also, it is more difficult to namespace encode JavaScript resources and nearly impossible to properly encode (response.encodeUrl()) URLs built using JavaScript.

l) Don't include css and js file in Portlet JSP
Since portlet contributes a small portion in a portal page never include css and js file from Portlet JSP, instead move all the code into themes code where it will be part of the <HEAD>.

Particulary this will be usefull when including the dojo.js file. Including the dojo.js file in each portlet will create an separate instance of the dojo which causes issues. So it always better to place the css and js inclusion code in <HEAD>ie, themes of the portal.

m) Remove Client Aggregation Code from Portlet
If you are using RAD/RSA 7.5 and above, creating a JSR168 portlet will also include client side aggregation piece of code in portlet JSP. Remove the pieces of code if you are not using client side aggregation option.

n) Use taglibs whenever possible
Encapsulating Java code within taglibs not only lets you easily reuse common view functions, it keeps the JSPs clean and makes them more like normal HTML pages. The page designer can concentrate on layout and decoration, and you reduce the possibility of breaking the embedded Java code.

0) Use JSTL instead of re-inventing common tags
The Java Standard Tag Library (JSTL) defines many commonly needed tags for conditions, iterations, URLs, internationalization and formatting.

p) Avoid updating HTML components on the fly
Try to avoid updating the HTML components on fly ie while the DOM tree is constructed out of the HTML components available in JSP.

Will explain about this one in detail in a separate topic as this common issue is faced by many developers while using javascript.

Regards,
Elango

Using PMD for reviewing Java code

Introduction
In this blog I would like to discuss about some of the code review tools that can be used in RAD (Rational Application Developer) and RSA (Rational Software Architect) or any eclipse based IDE.

Usually the code written by the developers (including me :)- ) are generally not easy to maintain and it is a herculean task to understand the code. So it is always better to use some of the open source code review tools while development, which atleast ensures code return by the developers are readable.

Here we can discuss about how to integrate PMD with some of the existing IDE's. In this case I have taken RAD.

PMD
PMD is an open source code review tool provided by sourceforge.net.

PMD is integrated with JDeveloper, Eclipse, JEdit, JBuilder, BlueJ, CodeGuide, NetBeans/Sun Java Studio Enterprise/Creator, IntelliJ IDEA, TextPad, Maven, Ant, Gel, JCreator, and Emacs.

It is an open source tool which can be used to scan the Java source code for some of the problems like:
  • Possible Bugs - like empty try/catch/finally block/if/else/switch statements

  • Dead code - unused local variables,parameters and private methods

  • Suboptimal Code - StringBuffer usage and wasteful String

  • Complicated expressions - unneccessary if statements, for loops

  • Duplicate code - copied/pasted code that repeats in the Java code

Installing

PMD provides some basic rules which are applied over the Java code, the rules which are used by the PMD are described in detail, here. Alternatively we can also create a new rules if needed or edit the existing rules by using PMD prefernces option.

Window -> Preferences -> PMD -> Rules Configuraion

The above option will be available only after installing PMD on RAD. The following steps describes about how to install PMD for RAD/any eclipse based IDE:

1) Launch RAD/any eclipse based IDE

2) Select the menu “Window -> Preferences -> General -> Network Connections”

3) Select the radio button “Manual proxy configuration:” and enter the proxy server name and port in Http Proxy option. Click Ok.

4) To install the plug-ins, navigate to “Help -> Software updates”

5) Select “Available Software” tab and click the button “Add Site”

6) Enter the value http://pmd.sourceforge.net/eclipse and click “Ok”

7) Expand the node against http://pmd.sourceforge.net/eclipse. If it throws error check the proxy settings which was done in step 3. Make sure that the proxy server and proxy port are entered properly.

8) Select the check-box against “PMD for Eclipse 3” and un-select the check-box against “PMD for Eclipse 2” and click “Install”




9) After installation it will prompt for restart.

Using PMD
After installing PMD just right click on any Java file and select "PMD -> Check Code with PMD" which will show all the code review comments in "Violations Overview" view. If not shown the choose the view from "Windows -> Show View" and search for Violations Overview view. Click Ok.

Turn off some of the irrelevant rules used by PMD using "Windows -> Preferences -> PMD -> Rules Configuration" option. The same option can be used to edit or add a new rule to PMD.

In the next post we will see how to integrate "Check Style" plugin and configure it in RAD. Bye till then.

Regards,
Elango