search
top
Currently Browsing: Java

Code completion for FireBug

FireBug is a neat Firefox extension very helpful for Javascript/Ajax developers.

FireBug comes with a very powerful console facility and the best thing is that you can access it from Javascript. But all those functionalities are a little hard to remember so I’ve created a code completion library for JSEclipse, another useful tool for an Javascript developer.

Download: firebug.xml.

To install this library you have to go into the JSEclipse menu, Add library submenu and point the dialog to the downloaded XML file. Next time when you will enter in JSEClipse:

FireBug code completion

Have fun.

KTML4 – JSF component

Tonight, having nothing better to do, I’ve start to look around on how I can improve the chances for the KTML4 to be bought.

My starting point in this search was Sun Java Studio Creator 2. This new IDE from Sun is a really cool tool in my opinion and it has a lot of nice features especially in the new AJAX way of writing a web application. Creator 2 has a components palette from where you can drag and drop new items into your page. That palette would be a nice place for KTML4 to be.

Looking around on how can I implement a component for Creator 2 I’ve realized that there is nothing special to them (or maybe I didn’t searched enough), they are simply JSF components. And this is even better as JSF components are supporter by other IDEs and that means a bigger market for us. So keep in touch as KTML4 JSF will soon be out.

Online HTML editor for JSP

Online HTML editor for JSP

A JSP CMS as any other CMS, needs a good content editor. InterAKT has unveiled their last version of KTML, 4.0, that comes now in a new flavor: JSP. This editor is very easy to integrate and offers a lot of very useful features. Quoting from InterAKT site:

KTML is an online HTML editor that helps you edit your website content directly in a browser. The editor loads fast and has an easy-to-use interface (similar to desktop editors). The latest version offers superior Word compatibility, a revolutionary Image Editor and XHTML 1.1 support. KTML has wide browser compatibility and supports most platforms (including MAC).

JSP/Java – strip_tags() PHP like function

Another PHP function that is very used is strip_tags.
This function tries to return a string with all HTML tags stripped from a given string.

   1:      public static String strip_tags(String text, String allowedTags) {
   2:          String[] tag_list = allowedTags.split(",");
   3:          Arrays.sort(tag_list);
   4:   
   5:          final Pattern p = Pattern.compile("<[/!]?([^\\\\s>]*)\\\\s*[^>]*>",
   6:                  Pattern.CASE_INSENSITIVE);
   7:          Matcher m = p.matcher(text);
   8:   
   9:          StringBuffer out = new StringBuffer();
  10:          int lastPos = 0;
  11:          while (m.find()) {
  12:              String tag = m.group(1);
  13:              // if tag not allowed: skip it
  14:              if (Arrays.binarySearch(tag_list, tag) < 0) {
  15:                  out.append(text.substring(lastPos, m.start())).append(" ");
  16:   
  17:              } else {
  18:                  out.append(text.substring(lastPos, m.end()));
  19:              }
  20:              lastPos = m.end();
  21:          }
  22:          if (lastPos > 0) {
  23:              out.append(text.substring(lastPos));
  24:              return out.toString().trim();
  25:          } else {
  26:              return text;
  27:          }
  28:      }

JSP – htmlspecialchars() htmlentities() PHP like function – version 2.0

The search has continued and no solution has been found yet.

As I was not pleased with the performance of my previous approach I worked out the following one:

  
StringBuffer sb = new StringBuffer();
for(int i=0; i<content.length(); i++) {
  char c = content.charAt(i);

  switch (c) {
    case '<' :
      sb.append("&lt;");
      break;
    case '>' :
      sb.append("&gt;");
      break;

    case '&' :
      sb.append("&amp;");
      break;
    case '"' :
      sb.append("&quot;");
      break;
    case '\'' :

      sb.append("&apos;");
      break;
    default:
      sb.append(c);
    }
}
  
content = sb.toString();

This verision is a lot faster then the previous one. On the page source from the slashdot site this version performed in under 8ms while the other one had it’s best performance in 20ms (usually in the range of 30ms).

JSP – htmlentities() PHP like function

In PHP there is a very useful function: htmlentities().

What this function does is to replace < with &lt;, > with &gt; and so forth. This is very useful when editing into a page some html code stored inside a database.

I can not find something similar for JSP. If you are using Struts, there is no need for something like this, but if you are using plain JSP you may not be so fortunate.

So I approached this through brute force:

<%
content = content.replace("&", "&amp;");
content = content.replace("<", "&lt;");

content = content.replace(">", "&gt;");
content = content.replace("\"", "&quot;");
content = content.replace("'", "&apos;");
%>

But this is no elegant solution and I will try to find a better one. Do you know a better one?

The / impact or how to lose one day.

I was developing a master-detail administration module for an ERP at the company I am working now.

So, I finish my task, hand it over to the administration team and start working on the next one.

The next day I have received a bug report that was saying my module did not worked at all. I tested it myself on the deployment server and it was true. I was seeing only a blank page.

I am relatively new to the j2ee / struts development so I did not know what was happening. On my jboss local server everything was working OK, on the Oracle Application Server – blank page.

The worst part for this it was that I did not have rights to make debug on the Oracle. After a day of struggle one colleague enlighten me: it was a “/” missing from the struts-config action’s forward definition.

I had written:

<forward name="default" path="template.jsp?page=erp/...

and it should have been:


<forward name="default" path="/template.jsp?page=erp/...

Not very smart of me… :(

Struts error reporting

Struts was using two different classes to report errors and messages back to the user: ActionErrors and ActionMessages. As the ActionErrors and ActionError classes were deprecated since 1.2.0. version I will talk now only about ActionMessages and ActionMessage.

From an action class when you need to send a message to the page all that must be done is to set it up using the ActionMessages class.

Struts ActionMessages example:

ActionMessages actionMessages = new ActionMessages();

actionMessages.add(ActionMessages.GLOBAL_MESSAGE,
new ActionMessage("my.error"));
saveMessages(request, actionMessages);

This error will be displayed in JSP using:

<logic:messagesPresent message="true">
<html:messages message="true" id="msg">
<bean:write name="msg" ignore="true"/>
</html:messages>

</logic:messagesPresent>

One issue to be mentioned here is that “my.error” is a key that will be used to look up message text in an appropriate message resources database, like Application.properties for example.

But if you do not need to report a generic error message you will have to use the following code:

ActionMessages actionMessages = new ActionMessages();
actionMessages.add(ActionMessages.GLOBAL_MESSAGE,
new ActionMessage("my.error"));
request.setAttribute("warnings", actionMessages);

This code will set up an error message under an arbitrary key and display it with the html:messages tag:

<html:messages name="warnings" id="message">
<bean:write name="message" />
</html:messages>

There are some differences to be noticed here.

  1. There is no message=”true” attribute present that signals to look up for an ActionMessage under the Globals.MESSAGE_KEY.
  2. There is no <logic:messagePresent> tag as it will not be validated.

If your error message gets constructed dynamically the ActionMessage class offers four constructors that allow message customization with up to four parameters.

So the message:

new ActionMessage("my.error", "p1", "p2", "p3", "p4")

where

my.error=You are not allowed to do {0}, {1}, {2} and {3}

will render

You are not allowed to do p1, p2, p3 and p4

JBoss 3.2 connection problem

I am developing an application using JBoss as a deployment server. After a redeploy the JBoss throws an error on me: org.jboss.deployment.DeploymentException: Connection refused: connect

The problem was in jboss.xml file:

<!DOCTYPE jboss PUBLIC
"-//JBoss//DTD JBOSS3.2//EN"
"http://localhost/jbossdtd/jboss_3_2.dtd">

After I have changed this to:


<!DOCTYPE jboss PUBLIC
"-//JBoss//DTD JBOSS 3.2//EN"

"http://www.jboss.org/j2ee/dtd/jboss_3_2.dtd">

everything has gone smoothly.

Struts and JBoss

I found myself the other day in a strange position developing a project on JBoss.

The project was based on Struts and after deployment it didn’t worked. The symptoms were this: the landing page was not updated to the version of the deployed project. Instead the browser was showing to me a page from a previous Struts based project.

After I sniff around on the Internet I found no solution to my problem :( . So I did it the hard way.

I start searching for a pattern in the JBoss behavior and I found that it cached all the files that were common between the two projects. That was because the last project was from CVS and had all the sources older then the first project I was working with. So I modified the index.jsp and after loading it into the browser (and sow it was the correct one) I’ve start to search on my hard drive all the files not older than 10 minutes. And there it was :D , c:\tmp\project_name\index_jsp.java.

So after I’ve deleted c:\tmp\project_name directory everything gone back to normal.

« Previous Entries

top