Escaping quotes in Java

Here is a simple tip to replace a quote with a backslash and a quote :
‘ ==> \’

public static String echappeApostrophe(String s) {
String temp;
temp = s.replaceAll(“‘”,”\\\\'”);
return temp;
}

I needed 4 backslashes

To better understand what is happening here, i printed out a few values :

String a = “\'”;
String b = “\\'”;
String c = “\\\'”;
String d = “\\\\'”;

System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);

output :

\’
\’
\\’

This is useful if for instance you need to pass, from a Java program, a string with a quote inside as a parameter to a dynamically generated
JavaScript function.

Else you get a Javascript error because the quote is not escaped.

More info here :

http://java.developpez.com/faq/java/?page=langage_chaine#LANGAGE_STRING_antislash

Design approaches

Here is another memento I wrote to remember the different approaches when writing EJB 3 or Web services.

TOP DOWN BOTTOM UP IN THE MIDDLE
EJB 3 Develop entity beans first then derive db schema Develop db schema first and drive upwards to create entity beans from your schema Create entity beans and db schema and map both respectively
Web Services contract first : in the top-down scenario, the developer first creates or is given the WSDL file for the service, and then must create a component to implement the operations. First you create a Java bean or EJB bean and then use the Web services wizard to create the WSDL file and Web service. In this case the starting point consists of both the WSDL file and the implementation component, and the developer must create additional support code that maps between the two.

http://en.wikipedia.org/wiki/Top-down

Aggregation and composition in Java

I usually forget the differences between the composition and the aggregation relationships and mix them together, so here is a memento that i will share with everyone.

  • Composition : a filled diamond
  • Aggregation : an unfilled diamond

The relation between a car and a tyre is an aggregation because the tyre is still a tyre if it is not attached to a car. The tyre has a life of its own. It exists outside of a car and you can use it on another car.

The relation between a car and a carburetor is a composition because the carburetor has no use if it is out of a car.

Aggregation and composition in Java

Now more interesting is the implementation in Java of these 2 relationships :

Aggregation :

class Car {


}

class Tyre {

private  car = new Car();


}

Composition :

class Carburetor {


}

class Car{
Carburetor  carburetor = null ;
public void setCarburetor( Carburetor  carburetor )
{
this.carburetor = carburetor;
}


}

Selenium

Today I discovered a useful testing tool, written in Javascript and HTML.

One can quickly make functional tests with the Firefox plugin Selenium IDE. If your web app runs on IE, just export the sequence of tests into either an HTML file or a Java file.

Here is a screenshot of some testing i did at work. The tests run directly inside the browser !


Web trend map 2008

The coolest gift for geeks, the A0 poster of the 2008 Web Trend Map (841mm x 1189mm / 33.25in x 46.75in), is now up for grabs:

http://informationarchitects.jp/web-trend-map-3-get-it/

http://informationarchitects.jp/start/

http://informationarchitects.jp/ia-trendmap-2007v2/

MappingDispatchAction: a single Action class but several action tags

The MappingDispatchAction class extends org.apache.struts.actions.DispatchAction.
It is similar to DispatchAction, except that a different action form, identified by the name attribute,
can be specified for each action mapping.

MappingDispatchAction: Use this when you want to define multiple form handlers for the same Action.

DispatchAction: Use this if for any reason you don’t want to define multiple form handlers (e.g., to make struts-config.xml more manageable).

So, instead of having a single action declaration, we will have one action declaration per method defined in the action class.
Very useful, if for instance, you need to handle different form beans for each method of the action class.

http://mani.fileave.com/tutor/StrutsTut.htm

The difference between MVC 1 and MVC 2

The MVC architecture :
Model : Responsible for the business domain state knowledge
View : Responsible for a presentation view of the business domain
Controller : Responsible for controlling the flow and state of the user input

There are 2 models of the MVC architecture :
Model 1 (MVC 1) and Model 2 (MVC 2).

  • In MVC 1, the application control is decentralized, because the current page being displayed determines the next page to display.
  • In MVC 2, a controller servlet is the target of a request submission rather than the JSP pages themselves.
    A Model 2 architecture introduces a controller servlet between the browser and the JSP pages or servlet content being delivered.
    Struts is a good example of a framework based on MVC 2 because the ActionServlet servlet will select the proper view to respond to the user.
  • http://faq.javaranch.com/java/Model1Model2MVC
    http://java.sun.com/blueprints/guidelines/designing_enterprise_applications_2e/web-tier/web-tier5.html
    http://www.javaworld.com/javaworld/jw-12-1999/jw-12-ssj-jspmvc.html

    Struts : MVC 2