Today I'm coding in Java - writing my first Java web app. For various
reasons, I can't get online or at any of the computers that contain any of my
other active projects (the Topic Exchange, a comment server and the PyCS
search extension), but I do have a copy of "Oracle 8i Java Component Programming
with EJB, CORBA, and JSP" and an old Pentium notebook with a text editor. Not a
Java compiler, but I know C# syntax well enough that I can hopefully bang out a
whole heap of almost-Java and fix it later on.
My impression so far is that it's not as annoying as I'd expected. It's
certainly quite verbose compared to Python, but the JDBC stuff isn't too
bad: I can handle using PreparedStatement
and
ResultSet
objects for everything.
I'm coding in almost the same style as I use in Python. It doesn't translate
well into C++, where all the redundancy in declarations and definitions makes
creating new objects and methods into quite a chore, but it seems OK here.
One disadvantage to not being able to get online is that I can't get at any
of the Apache and OpenSymphony projects that everyone
seems to rave about. Or maybe that's an advantage - I'm actually getting some
code written, instead of spending all day reading about things.
Part of the app is a very simple template compiler. It takes files that look
like this:
<item>
<title>{{title}}</title>
<link>{{link}}</link>
<description>{{description}}</description>
<guid>{{link}}</guid>
</item>
and turns them into code like this:
package nz.co.myelin.foobar.templates;
class rss_item
{
public String title;
public String link;
public String description;
public String link;
rss_item()
{
}
public String render()
{
return " <item>\n"
+ " <title>"
+ this.title
+ "</title>\n"
+ " <link>"
+ this.link
+ "</link>\n"
+ " <description>"
+ this.description
+ "</description>\n"
+ " <guid>"
+ this.link
+ "</guid>\n"
+ " </item>\n"
+ "\n";
}
}
This is a more complicated version of what I do when I want to display
something from a Python script, and is very similar to the HTML::Template module
in Perl. Not as powerful as things like Cheetah, but also
harder to screw up, and it also lets you let other people write their own
templates without worrying about security or that they might mess up the code,
because there's no code in the template.
Another thing - caching. Apparently the accepted way to cache web pages in
the Java world is to use a library called OSCache. However, it sounds like it
occasionally goes mad and totally pummels your server. I
don't really understand this -- how difficult does caching web pages need to be?
You just put a little hook in the bit of your code which writes the page out to
the server, and get it to save a copy in the cache. From then on, if you need
to serve that page, you grab it back out with a quick SELECT
from
the database and serve it. If you want to be clever, you can store a
Last-Modified
date and an E-Tag
in the cache as well,
and then other people might cache you too. All this is trivial stuff, and it
won't get you kicked off your server for bad behaviour
because it's too simple to break.
... more like this: [