Tuesday, December 7, 2010

Validate textbox in Graphical User Interface containing only numbers


Often it is necessary to validate the input data in the Graphical User Interface when received at the server side. According to the received parameters the server may be not called if the parameters are not in the desired format. This will lead to a saving in the used resources server side. This can be easily done by performing a basic validation on the properties of the object received. The validation proposed here below could be smaller by using regular expressions.
The programming code below just loop on a received string and determines if every character of the string is a digit isDigit(). The loop stops if a character is not a digit and returns false.




/**
*
* @author Jose Ferreiro
*/
public class Main {

/**
* This method checks if a String contains only numbers
*/
public boolean containsOnlyNumbers(String str) {

//It can't contain only numbers if it's null or empty...
if (str == null || str.length() == 0)
return false;

for (int i = 0; i < str.length(); i++) {

//If we find a non-digit character we return false.
if (!Character.isDigit(str.charAt(i)))
return false;
}

return true;
}


/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Main main = new Main();
System.out.println(main.containsOnlyNumbers("123456"));
System.out.println(main.containsOnlyNumbers("123abc456"));
}
}


OUTPUT of the main method:
True
False

HAPPY CODING

Saturday, December 4, 2010

Custom 404 Page Using JBOSS (for audit purposes)




Having a custom “page not found”, or 404 page, is an important modification for any website. It’s used to enhance the user experience by presenting an easy to understand message. Sometimes it is also useful to hide information contained in those pages as it reveals information about the server it self, release etc, etc


Setting up a user friendly error page is simple enough using Apache web server. Just modify the line in httpd.conf and point it to a static HTML document:

ErrorDocument 404 /the404_page.html

With JBOSS (or Tomcat-like Java container) application server, it’s slightly trickier. It has to be handled per web application basis. The change is done on the web.xml file, with these entries:

<error-page>

<error-code>404</ error-code>
<location>/404.html < / location >
< /error-page >

For the root directory, modify the web.xml in the ./deploy/jboss-web.deployer/ROOT.war/WEB-INF directory.