Friday, July 29, 2011

Java 7 is out!

You can download it from here.

It contains new features in the syntax (the diamond operator, try with resources, switch with Strings), a new FileSystem API, support for asynchronous I/O, a new fork/join framework for multicore performance and other cool new features.

Wednesday, July 27, 2011

Handling UTF-8 encoded content with jQuery 1.4

Recently I encountered a text encoding related issue in jQuery 1.4.

Let's say that we have a todo list application written in jQuery.
This application supports internationalization for english and arabic, so we will use UTF-8 character encoding.

The content displayed is stored in a database and it is read using a servlet.

The problem that I encountered is that, for some reason, arabic text was not displayed correctly.


The code in the view.jsp contains

<%@ page contentType="text/html; charset=UTF-8" %>

<script language="JavaScript" src="<%= PortalUtil.getStaticResourceURL(request, request.getContextPath()   "/js/todo.js", portlet.getTimestamp()) %>" charset="UTF-8"></script>

The todo.js code makes ajax calls to a servlet that serves the todo list information in json format.

var ajaxOptions = {
  scriptCharset: 'UTF-8',
  contentType: 'application/x-www-form-urlencoded; charset=utf-8'
}

jQuery.ajaxSetup(ajaxOptions);

var folderData = {
  'action' : 'readFolders',
  'userId' : userId,
  'userName' : userName
  }
jQuery.ajax({
 url: this.contextPath '/TodoServlet',
 data: folderData,
 dataType: 'json',
 success: function(data){
  if(data.message == 'SUCCESS'){
   todoFolders.folders = Array();
   todoFolders.quickNotes = Array();


   //normal and tasks folders
   if(data.folders && data.folders.length > 0){
    jQuery.each(data.folders, function(i,f) {
     var folder = new TwFolder(f.folderId, f.folderIcon, f.folderTitle, f.folderColor, f.folderType, todoFolders.contextPath, f.folderContent);
     ...
    });
   }
  }
 }
}); 

It seems that the arabic content should be displayed correctly because the page content type is set to UTF-8, and the content type in ajax setup is set as well to UTF-8. But, unfortunately, this was not the case.

After a few hours of digging around the web for solutions to this problem, I found that there was one thing that I didn't do. In TodoServlet.java, the servlet that is called by jQuery, the character encoding must be set on the response.

public class TodoServlet extends HttpServlet {
 
 public void doPost(HttpServletRequest request, HttpServletResponse response) {
  handleAction(request, response);
 }


 protected void handleAction(HttpServletRequest request, HttpServletResponse response) {
  try {
   request.setCharacterEncoding("UTF-8");
  } catch (UnsupportedEncodingException e) {
   e.printStackTrace();
  }
  response.setCharacterEncoding("UTF-8");
  
  String action = request.getParameter("action");
  long userId = GetterUtil.getLong(request.getParameter("userId"));


  if (userId != 0) {
   JSONObject jsonResponse = JSONFactoryUtil.getJSONFactory().createJSONObject();
  }
...

This solved my problem, and the result was this:


I hope this helps if you encounter this type of issue.

Cheers!

Monday, July 25, 2011

The New File System API in JDK 7

Here is an interesting presentation on the new enhancements regarding file system manipulation coming up in Java 7.


The Beginning

Hi there!

This is my new blog in which I will share my experiences in software development. Currently I am working on Oracle Webcenter, ADF, JSF, JSR-168 portlets and Liferay Portal, so my posts will be mainly about these technologies.

But keep an eye out for other interesting articles, websites, blogs and other resources related to computer science and software development in general. I will post here everything I find interesting about these topics.

Cheers!