There’s a great article sys-con.com called: Why Do ‘Cool Kids’ Choose Ruby or PHP to Build Websites Instead of Java?
I’ve been developing desktop and web apps in Java for a fair few years and I’m no Java-basher by any means. But when I’ve built stuff in my spare time I’ve always used PHP, so I guess I kinda fall into the article’s cool kids category. Before I give my reasons for not using Java for spare-time projects I thought I’d post some of the great FUD from the cool kids article:
PHP and Ruby etc are cool for building pages, but they are not ideal candidates for building middleware logic. Given that Java covers the “high end” of the spectrum well (where sophisticated processing is needed), wouldn’t it be great to use Java all the way?
PHP, Ruby etc not good for middleware logic? I’d say that as languages, Ruby and Python are ahead of Java in terms of functionality – they both have rock-solid OO support and functional programming features like closures that are still not in Java. PHP is a bit behind but as of version 5 it also has sold OO capabilities. It’s simple enough to write well-structured classes and build code using well-known design patterns using Ruby, Python or PHP so I really can’t see how they are not suitable for middleware.
Is it Java as a programming language too difficult to use, comparing with those scripting oriented interpreted languages? Yes, this maybe the reason.
WHAT THE FUCK? On syntax alone I’d say that Ruby is the most difficult to pick up. The only reason I would consider Java more difficult to learn is that it’s strong-typed and a beginner would have to learn about casting, generics and so on. And I guess the insistence on abstracting everything to the nth degree does make for more verbose and more difficult to learn and remember syntax. This is Java-snobbery in action.
JavaServer Faces: JSF is the new kid on the block. Is it going to make building websites easier? Probably not. It is designed for simplifying building form-based applications.
If there’s one thing that turned me off from Java web development it’s JSF. I just cannot buy into it at all. How anyone can say that JSF simplifies anything is beyond me. I know JSF2.0 fixes a lot of 1.x’s problems but boy were there a lot of problems. Tons of Javascript injected into in each page just to make the basics work. POST requests for everything, this beggar’s belief, it breaks basic navigation and made it impossible to bookmark a page. Duh.
The scripting languages are pretty easy to learn, and lack the complexity of Java (generics being an exquisite example of that). To learn Java, it’s not like just picking up a book and having all you need to begin developing. This is the polar opposite of scripting languages, where a short book or online tutorial will provide all you need to begin. After all, many people would rather die than learn something new. And Java takes some effort to master. Whereas with a scripting language, the basics are easy and then one simply needs to learn about other “packages” or plug-ins.
This is a snippet from the first comment and this is real FUD-gold. Generics an exquisite example of complexity? I think the commentor, raysea, needs to go and find out why generics don’t exist in Python, Ruby, etc. The rest of this comment is again pure Java snobbery.
OK, a lot of websites are fairly simple, mainly composed of markup pages, scripts and some lightweight logic on the server side, where PHP and Ruby are good for. Java maybe an overkill for such websites. But there are a lot of websites that are much more sophisticated than “lightweight” logic on the server side. For example, FaceBook was relatively simple initially, but now with FaceBook API and Platform, its complexity is growing. Why not use Java for such websites?
He uses Facebook as an example of a sophisticated Java app but this is such an extreme example it’s not worth thinking about. How many people are using Java to build sites with Facebook’s complexity and scalability requirements? Next to none. So where is this extra sophistication that Java provides being used? People use the example of intranet sites with lots of controls. Is that harder in Ruby? Nope. And besides, same question as before: how many people are building intranet sites versus extranet ones? I find it hard to see many examples where I would need Java to build a site.
So why do the cool kids shun Java web dev?
For me, it’s simple: Because the cool kids what to get things done quickly and cleanly and they know full-well that their Ruby, Python or PHP app running on Apache will fly. The other reason is that Java web frameworks have mostly become bloated monsters that abstract everything to the nth degree and required a lot of XML config whereas Rails, CakePHP and Django have mastered the art of quick and painless web dev.
I don’t doubt that Java running on Tomcat will outperform PHP on Apache but unless you are writing the next Twitter it really doesn’t matter – unless your app becomes super popular you are never going to reach the limit of a scripting languages performance.
Well, I’m going back to Java from PHP
Java’s saviour has arrived. The Play framework takes the best ideas from Rails etc and offers a Java framework that eschews complexity, configuration and bloat and lets you concentrate on the job in hand: building great webapps.
Posted August 20th, 2009 |
Published in Java
Firstly, an example of querying a single table using the JPA Query Language (JPQL). Assume in all example that the em object is an instantiated EntityManager.
Query q = em.createQuery( "select c from Customer c" );
List<Customer> list = q.getResultList();
Here we have an entity called Customer, which represents our underlying customer table in our database. The above query selects all rows from the customer table. The resulting List could be used like this:
for (Customer cust : list)
{
System.out.println( "customer's name = " + cust.name );
}
We can iterate over the List and print the name value from each Customer entity.
Now, assuming we want to read from the customer and store tables in a single query but they have no foreign key relationship. Our query would now be:
Query q = em.createQuery( "select cus, st from Customer cus, Store st where cus.visited = st.name" );
List<Object[]> list = q.getResultList();
The query looks similar but we name the Customer and Store entities and define the join with the where clause. To use the ResultSet List is now also a little different as the List no longer contains Customer objects, each element is now an array containing a Customer and a Store. We can use it like:
for (Object[] objects : list)
{
Customer cust = (Customer)objects[0];
Store store = (Store)objects[1];
System.out.println( "Customer's name = " + cust.name );
System.out.println( "Store's address = " + store.address );
}
This technique is useful when creating HTML tables of search results that span across multiple tables that don’t have foreign keys to easily join them.
If you are creating JSF pages and are getting an error like this:
java.lang.IllegalStateException: Component javax.faces.component.UIViewRoot@2c02699b not expected type. Expected: javax.faces.component.UIData. Perhaps you're missing a tag?
com.sun.faces.taglib.html_basic.DataTableTag.setProperties(DataTableTag.java:265)
javax.faces.webapp.UIComponentClassicTagBase.findComponent(UIComponentClassicTagBase.java:604)
javax.faces.webapp.UIComponentClassicTagBase.doStartTag(UIComponentClassicTagBase.java:1126)
com.sun.faces.taglib.html_basic.DataTableTag.doStartTag(DataTableTag.java:372)
org.apache.jsp.steve_jsp._jspx_meth_h_005fdataTable_005f0(steve_jsp.java:125)
org.apache.jsp.steve_jsp._jspService(steve_jsp.java:83)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)
com.sun.faces.context.ExternalContextImpl.dispatch(ExternalContextImpl.java:408)
com.sun.faces.application.ViewHandlerImpl.executePageToBuildView(ViewHandlerImpl.java:442)
com.sun.faces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:115)
com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:106)
com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:251)
com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:144)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:245)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)
Then you need to make sure that you are surrounding your database-aware JSF controls with a <f:view></f:view> pair.
As a simple fix add <f:view> before the <html> tag and </f:view> after the </html> tag.
Posted August 14th, 2009 |
Published in Java
This site walks you through creating a Pong game for J2ME-enabled mobiles. Starting with setting up your development environment, through to deploying the thing.
Have you ever wanted to program a game, but did not know where to start? Or have you wanted to program a game for mobile phones, but found the introductions provided by SUN, Nokia and other more confusing than enlighting? Then you’ve come to the right place.
This course takes you through every single step required to program your own J2ME game (that is, a game for mobile phones).
http://www.koderko.dk/J2METutorial/
Posted July 6th, 2009 |
Published in Java
We have a Java Server Faces project on the go at the moment. We did a POC the other week that kinda works. We’ve since been on a course and armed with our new knowledge it is obvious that the POC is using a poor design and that is causing it to not work correctly.
So today I argued strongly for throwing the POC away, starting again and doing it properly. This just seems to me to be the right thing to do – use recognised design patterns and make an abstracted and extensible piece of software.
But the POC almost works.
And we’re using Agile and so we’re meant to be delivering early and often. Binning the POC will probably mean that stories that were meant for this iteration won’t make it. Not doing the POC means we deliver something I’m not happy with.
But I’m gonna bite the bullet on this one, fix the POC and not redo it. Getting the stories into the iteration beats my developer instinct to do it right.
Posted April 20th, 2009 |
Published in Java
The simplest way to create a Singleton in a thread safe manner in Java is to:
public class A {
private static A instance = new A();
private A() {}
public static A getInstance()
{
return instance;
}
}
To briefly describe that: The constructor is private to avoid instantiations. The public getInstance() method is used to get an instance of the class. getInstance() simply returns the private static instance variable. The JVM guarantees there will only ever be one private static instance variable.
I would always use the above but there is another alternative that works:
class Foo {
private static class BarHolder
{
public static Bar bar = new Bar();
}
public static Bar getInstance()
{
return BarHolder.bar;
}
}
This is different in that the singleton class Foo does not itself hold a private static variable, it delgates that to the private static BarHolder class. BarHolder has a public static Bar variable and that is accessed via the getInstance() method of the singleton. This pattern can be used when you don’t necessarily want the singleton instance to be created when the JVM loads the class.