Servlet Design - How can I do <xxx> in a servlet?

[Note: the following refers to HTML, as that is the most common use for servlets. Your servlet may be responding with WML - or it may even not be an HTTP servlet at all. The principles involved are still the same, though - assuming there's a "simpler" way of responding to the protocol (as in the web, where a web-server can just respond with static pages), your servlet is unlikely to be able to do anything more complicated. The protocol will provide the separation between the client computer and the server one. That doesn't mean that your servlet can't do different things on the server, of course, depending on the request - it could do any number of things, including (in a suitably insecure environment) shutting the computer down! It couldn't, however, shut the client machine down, unless there was something in the protocol to tell the client that.]

This kind of question comes up very often in comp.lang.java.programmer - a couple of specific (and frequently asked) examples are:

The key is to think from the brower's point of view, and design a mockup in statically served files, with some dummy data.

As far as a browser is concerned, it's just talking to a web-server. It doesn't know there's a servlet behind the scenes. From a design point of view, this actually makes things much easier, because it means you can apply all your HTML knowledge when designing servlets.

You can design the user interface, putting in dummy static data where necessary, and then start writing your servlet so that it outputs the HTML you've written, and any images needed. Note that each request only gets one response, so to get a page with two pictures on it, there are three requests. Firstly, the browser requests the HTML for the page, eg index.html Secondly, having read the HTML, it notices that there are two img tags, so it requests each of the images. As far as the servlet programmer is concerned, these are separate requests. With HTTP/1.1 they may come in on the same connection, but that needn't bother you - just write each image as it's requested, and you'll be fine.

Frames are similar to images - there's one request for the frameset, then one request per frame. Stick to the idea that each HTML page you've written corresponds to one request, and everything should work like magic.

Loading and saving files in servlets

One problem people have which isn't related to the above is to do with local files on the server, and how to load them. Many people imagine that if they use a relative filename, eg:
File f = new File ("myfile.txt");
it will be taken as relative to wherever their class file is. Now, leaving aside the fact that if you think it through, that's a pretty odd concept (what if the class is loaded over the network, or in a jar file, or autogenerated?), the thing to do is just avoid truly relative filenames altogether in servlets. Relative files would actually be relative to the working directory of the servlet container. That isn't defined in the servlet specification, and will vary between different containers. The way to overcome this is to use filenames which are relative to a known absolute filename, usually specified in the configuration parameters for a servlet. For instance, I often have a WorkingDirectory parameter, and just take all my filenames relative to that - so as far as the Java libraries are concerned I'm using an absolute filename, but I don't have to hardcode any absolute filenames into my servlets.

Back to the main page.