Archive for May 2007

links for 2007-05-25

No annotations for servlets?

I was looking at the following article on java.net about the proposed annotations that J2EE 5 will support. I wonder why they are not adding annotations for servlets. If you are writing a simple application, sometimes there is no need to implement an MVC approach. Take servlets that just return JSON or XML to a XMLHttpRequest. Now if your project is already using MVC, then by all means integrate it. Something like what I listed below seems to make a lot of sense to me:



@Servlet(name="My Servlet", urlMapping="/myservlet.jspx")
public class MyServlet extends HttpServlet {
...


There is really no reason why I should have to write something to web.xml everytime I want to add a servlet (especially the verbose way you have to define servlets now). Imagine being able to just add the Spring MVC or Struts JARs to your classpath and having them work without configuring anything in web.xml.


Dynamic dropdowns with extjs using servlets and json-lib

If you have not discovered extjs yet, then you are missing out. I’ve been working with extjs for a couple of months now, and I am very happy with the results — and so are the customers. There are a number of advantages to using Javascript UI components for your web application including:

  • you do not have to recompile after every change (just hit reload)
  • it does not matter what backend framework you are using — you just need to be able to post and retrive JSON or XML data
  • it does not matter what view you are using (Freemarker, Velocity, JSP’s, etc)
  • json-lib makes working with json-lib easy
  • you can do a lot more functionality with a lot less code


The demo shows how to easily do dyanmic drop down boxes with extjs and using servlets to provide the JSON. I used json-lib to create the JSON.


Here are a couple of images of the demo. Just unzip the archive, run “mvn jetty:run”, and go to http://localhost:8080/. You have to have Maven 2 already installed and setup. I have a number of other examples on using extjs with Java including a nice tree reordering demo where I save the nodes to a Hibernate backend that I will try to find some time to post.






links for 2007-05-18

links for 2007-05-17

links for 2007-05-07

links for 2007-05-06

Eclipse and OutOfMemory

I have run into the same problem that seems to be plaguing a number of other Eclipse users lately (http://www.jroller.com/page/phidias?entry=eclipse_running_out_of_memory, http://www-03.ibm.com/developerworks/blogs/page/JEE?entry=eclipse_out_of_memory). This problem was happening rarely for me running Eclipse 3.3M4, but it started happening to me at least 5 times an hour (if not more frequent) when I upgraded to Eclipse 3.3M6. This is nothing new for me. I have been using Eclipse since version 2.0 and have run into OOM errors before — although usually it was because I did something dumb like download a new version and forget to alter the heap size. (Just for fun, try running the old Motif version of Eclipse 2.0. Eclipse has really come a long way.).


I started searching around for fixes for the problem, but I wanted to understand what was going on since I log a lot of hours with Eclipse. I am running Eclipse 3.3M6 (I20070323-1616) on a 2.8GHZ P4 with 4GB of RAM under Windows Vista using JDK 1.5.09. The plugins I have installed are Mylar, Hibernate Tools, Subversive, WTP, and Spring IDE.


My first settings to change were the minimum heap size to 128MB (-Xms128m) and the maximum heap size to (-Xmx512m). I have seen some recommendations to set the minmum size to 512m. I would toggle Window -> Preferences -> General -> Show heap status to on first. Watch your heap usage and see what you normally trend at before setting it to a higher number. If you are only working with small to medium size projects, then you should be fine at that number. As a benchmark, I was able to take my heap up to 150M by running a Project -> Clean on both Spring and Hibernate’s source code at the same time, running it in the background, and opening other projects. I also checked out some large projects with Subversive and ran some standard tasks editting in Mylar. I am sure there are more heap intensive plugins that you are probably running, so best bet is to turn on the status and tweak later.


After verifying that did not fix the problem, I altered the PermSize and MaxPermSize parameters (-XX:PermSize=128m -XX:MaxPermSize=128m) to be 128M. This solved me problem I have now been running Eclipse happy for 7 hours without a crash. If you make these changes and are still having problems, use the -XX:+HeapDumpOnOutOfMemoryError setting to generate a heap dump (for more information on debugging heap dumps, go here and here.


Below is a screenshot I snapped using JConsole to show that my PermGen space does indeed trend over the default setting of 64m. The J2SE 5 version of JConsole did not seem to play nice with Vista, so this is attached to my 1.5.09 JVM using the JDK 6 version of JConsole.





Some other recommendations for fixing the problem include using IBM or JRockit to run Eclipse. Apparently, those JVMs have no concept of PermGen space and the PermGen data is stored directly on the heap. It is then subject to garbage collection like the objects stored in the heap. If I was starting an application from scratch, I would probably use JRockit but most of us are stuck with Sun’s JVM.


Off of the top of my head, I can probably name no more than ten options you can pass the JVM on the command line to configure garbage collection, JMX, system properties, etc. I have been working on J2EE applications for years and a company where we migrated applications from Java 1.2, 1.3.1, 1.4.2, and 1.5. I dealt with a lot of OOM errors in our web applications and did a decent amount of JVM tuning. Having said that, how many Eclipse users know that there are options that can be passed to the JVM for tuning? I would guess that there are a large number of users who have never dealt with tuning JVM parameters at all. Those same users probably also do not realize that there are other JVM options besides Sun’s JVM to choose from.


I was thinking about this as I was looking through this bug in Eclipse Bugzilla. Some of the comments on the bug suggest that this is an Eclipse problem, some of them suggest that it is a Sun problem. However, this is going to be a big annoyance for Eclipse users going forward unless someone fixes it. I do not see anything happening on the Sun side (nor am I sure that they can do anything without significantly changing their JVM), but it would be nice to see Eclipse either 1) present the user with a warning on the splash screen with a link to configuring their JVM properly for Eclipse 2) have a warning that goes off when the PermGen size get’s beloww 5%-10% with a help link on how to fix the problem.

links for 2007-05-03

Spring MVC form tag library and template support

A really useful UI feature of Webwork (now Struts 2) was templates. ASP.NET also provides similar functionality through CSS Control Adapters.Templates allow you to customize the HTML output by the form tag libraries. This feature was really useful for my development team because it allowed everyone to generate accessible (508 compliant) HTML for forms without having to know anything about accessibility. Just implement the templates once and make sure the whole team uses the accessible theme.


Spring MVC has the idea of themes, but does not allow for customizing the HTML output by the form tags. I created an issue in Spring JIRA describing what should be included in the feature. Templates would be a nice addition, but I would settle for a hook to provide custom HTML. A template mechanism could be implemented if that functionality is added.


If you are reading this, vote for the feature.