April 17, 2009
Today is about the tenth time I’ve needed to redirect to a slightly modified version of the current URL. So, I have to piece together a URL from pieces in an HttpServletRequest. And I can never remember which piece is which. I know, I know, the difference between “servletPath” and “pathInfo” should be intuitively obvious to me. So now, for the last time, here is what the pieces mean, for an example URL: http://www.myserver.com:8080/appname/servletname/my/path?name=value
- protocol – “HTTP/1.1” (you’ll have to convert this back to http or https, for example)
- serverName – “www.myserver.com”
- serverPort – 8080
- contextPath – “/appname”
- servletPath – “/servletname”
- pathInfo – “/my/path”
- queryString – “name=value”
Not looking at all of these values now, so some might be slightly off – please post a comment to correct me.
February 11, 2009
I’ve just uploaded a minor patch to Sample App. This fixes some errors in the production build targets, and adds more thorough JavaDoc and YUI Doc.
sample-app-0.1.1.zip – 20.6 MB
You can also find out more about Sample App.
February 10, 2009
I’ve just posted a Java webapp called “Sample App” (until I can think of a better name for it). The goal for this webapp is to be a project template using industry-standard libraries and best-practices for full-stack Java application development, from a high-performance rich client tier, back to the database. There’s nothing particularly revolutionary about it, but it’s hard to find this particular combination of features – especially a combined rich frontend and backend. It’s released under a BSD license.
Check it out, take what’s helpful to you, and improve on the rest!
- Logging (YUI Logger)
- Combination of assets to save on HTTP requests
- CSS sprites automatically generated (SmartSprites)
- Minification (YUI Compressor)
- Ajax requests (YUI Connection)
- Strict presentation logic boundary (JSTL)
- Copy in properties files
- Separation of business logic and webapp into distinct projects
- Dependency injection (Spring)
- Unit testing (JUnit)
- MVC (Struts configured via Spring)
- ORM (Hibernate)
- Logging (Log4J)
- Separate Business Logic and DAO tiers
- Separate build targets for dev and prod environments, changing config files, adding additional JS, and enabling/disabling logging and minification
- Run on local box from a bare checkout using a build target, without any additional configuration or native code (Jetty, HSQLDB)
- JDK 1.5 or above
- Ant 1.6.5 or above
Download the zip file above, expand it, go to sample-webapp/build, and run ant. This will run the default target run-dev. The app will then be accessible on your local computer at http://localhost:8080/sample-webapp/
August 12, 2008
I managed to get xhtmlrenderer working as a Servlet Filter, so that any HTML page you apply the filter to will be converted to PDF. Here are a few hints:
- If you want to keep the XHTML in memory rather than writing it out to a file, then you’ll need an XML parser like Xerces to parse the XHTML into a DOM object before you can pass it to xhtmlrenderer. (iText has got to have some kind of XML parser it uses that you could probably use too, instead of including a separate library. I just didn’t take the time to figure it out.)
- You’ll need something like a ByteArrayServletOutputStream to capture the JSP output in-memory so you can then convert it to PDF. Catalina has one.
- If you have a table that spans multiple pages and you want to repeat the table header and footer on each page, use the custom CSS attribute “-fs-table-paginate: paginate;” on the table.
- Be sure to pass xhtmlrenderer the URL of the page it’s rendering, so that it can access relative URLs to pull up external resources like images and CSS files.
- I’m using JSPs as the templating solution, and I ran into some buffering issues where no content was written into the ServletOutputStream when I was applying the filter. A manual call to out.flush() at the end of the JSP works for now, but I’m looking for a better solution.
August 8, 2008
Looking today for a few OSS options for generating PDFs from Java. Here’s a quick summary of what I’ve found:
- Apache FOP – generates PDFs from a library-specific XML document. You can use XSLT to translate your own XML into the library-specific XML.
- iText – generates PDFs using API calls.
- xhtmlrenderer (AKA Flying Saucer Project) – generates PDFs from HTML documents. You can use your regular templating engine to create the HTML, or even put the renderer in a servlet filter in order to PDFify JSP output. As a result, maintenance is easier than with the other frameworks, because you’re maintaining template files of the same kind as your webapp. HTML is going to be less flexible than native PDF commands, but there is a workaround for repeating table headers and footers on each page, for example.
A well-known library I don’t recommend is pd4ml – it also generates PDFs from HTML documents, but, if memory serves me correctly, it isn’t as feature rich as xhtmlrenderer, and it’s also commercial (non-free) software.