Last week I trod old ground and spoke about how to get ready to create a servlet for the first time. The obvious carry-on from this is to write an actual servlet. That's what I'm going to do now. The way in which I want to try and do this is that the code I show is actually of some use. All too many "how to write a servlet" type articles end up with a reader thinking "and!?". Hopefully not with this one. If you find this is still the case after reading this, be patient, the next article has an even more useful example.
What the servlet does:
Sat in a meeting recently, the idea of having adverts on an intranet homepage was brought up. Not for anything commercial, just for other parts of the intranet. Nothing special in that. However I got thinking about how this would be a great chance to use a servlet. With it we can redirect the user and log the movement in a database so that the boss can see who and where and what is being clicked. Maybe even a report or two. After all, they like that kind of stuff, don't they.
Using a servlet means that, if this is going to be something used heavily, the server has a lighter load. Servlets are loaded in to memory when they are first run. Conversely, Agents have to loaded in to memory each time they run. Another factor against using an Agent is that, for something like this particular application, there is no rightful home for the code. If you find yourself creating a database just to hold an Agent then maybe it's time to be thinking about using servlets instead.
The Code:
What I don't want to do here is just paste in the whole of the code used. Not that there's a lot of it, just that that doesn't really help anybody. If you want to see the code (you'll have to at some point) then you can open the attached Java file in any Java IDE or a text editor. Let's just look at the important parts.
For all servlets there are two important objects that we always use. The Request (req) and the Response (res) which are initialised for us in the following which is the main class of the servlet and gets called straightaway:
public void service(HttpServletRequest req, HttpServletResponse res)
These two objects have some properties and methods that make life very easy for us. Within the class I made use of the following lines:
1. Stripping parameters out of the requesting URL would normally be a messy affair. Not with servlets, we can use the getParameter() method of the Request object.
String goToURI = req.getParameter("location");
2. If an error occurs we can easily return an error 500 to the browser that is formatted as we normally expect. For example, if the above parameter is missing:if (goToURI == null ) {
res.sendError(res.SC_INTERNAL_SERVER_ERROR, "Error - Location paramater missing!");
}
3. Finally, when we have the location that we want to go to we can send that path to the browser using the sendRedirect() method of the Response object:
res.sendRedirect( goToURI );
How much easier can it get!?
Creating the Domino document isn't quite so straight-forward. In three easy steps it is:
1. Create a new Notes thread, initialise it and create a new session object.
NotesThread nThread = new NotesThread();
nThread.sinitThread();
Session s = NotesFactory.createSession();
2. Open the database, create a document, add the values and save it.
Database db = s.getDatabase( "", "apps/redirect.nsf" );
Document doc = db.createDocument();
doc.replaceItemValue( "Form", "Log" );
doc.replaceItemValue( "Date", df.format( now ) );
doc.replaceItemValue( "Path", goToURI );
doc.save( true );
3. Finally, close the Notes thread.
nThread.stermThread();
Okay, so it's not quite as simple as that. In the above snippets I've missed out all the important try, catch and finally clauses. The way this works should be obvious if you look at the code. Once you get the hang of the skeleton code needed for a servlet you can really go to town. There are many things you can do in servlets that you'd be hard pushed to do with any other technology available so readily.
If you want to get really carried away you could go the whole hog and use just servlets to display your site on the internet. Doing this mean that you can do whatever you like with your HTML without Domino dictating how it's formatted. Now there's a dream....
Trying it out:
If you want to try it out download the example database in to a directory called "apps" on your server and place the LogRedirect.class file in to the server's servlet directory. Open the database and find the homepage. There are instructions and sample links on there. Give it a go. Any problems, let me know.....
Copyright © 2000 - 2024 Jake Howlett of Rockall Design ltd. This article was printed from codestore.net