|
Ok - heres the article that actually goes through
the Java Servlet code for this mini project.
I've tried to make it as straightforward as
possible, and have put particular tasks into
seperate methods to help.

The Files
Before I start, you can download the files
i'm using for this project
here.
The Class Configuration
I have really gone to town with this Servlet
and followed good techniques where possible.
The following is the declaration code.
|
public class DXReceive
extends HttpServlet {
public Session s=null;
public String dbname="members/edia/disc.nsf";
public String dbnameLink="members/edia/DXLinkMan.nsf";
public String viewLinkName="vLinksID";
public String form="MainTopic";
public Database db=null;
public Database dbLink=null;
public View vwLink=null;
public Document doc=null;
public Document docLink=null;
public String FIELD_ID="id";
public String FIELD_CATEGORY="Categories";
public String FIELD_NAME="From";
public String FIELD_SUBJECT="Subject";
public String FIELD_ORGANISATION="organisation";
public String FIELD_COUNTRY="country";
public String FIELD_EMAIL="email";
public String FIELD_WEBSITE="website";
public String FIELD_COMMENT="Body";
public String category;
public String id;
}
|
You will note the declaration of the Database,
view, form names etc in advance - makes it easier
to change when they are all in one place. Below
this is the declaration of the 'Notes Objects'
that we will be using. Finally the actual fields
that we will be populating in the Notes Document
are also declared - again to make it easier
when I need to change anything.
As a note, in the version I tested on my Domino
Server I did have the database names etc setup
in the Servlet.properties
file. However as the servlet for this site
runs on a Tomcat/Jakarta
Server at DominoDeveloper.net
I have to setup the parameters differently -
and I couldn't get it to work (I was rushed!)
so instead I have 'hard coded' them.
The Methods
First of all - the important one is doPost().
This is where your Servlet kicks off (remembering
what I said about doPost()
and doGet() in a
previous article).
The other methods: connectDomino(),
createComment(),
outputMessage()
and recycleDomino()
are 'service' methods for doPost(). In other
words I have seperated out each function of
the servlet into different methods which are
then called from doPost(). I have done this
to help you see what is going on, plus for good
coding practice.
The methods: getServletInfo()
and init() we do
not need to concern ourselves with at this point.
doPost()
As previously mentioned, doPost() is where
it all kicks off. It is the skeleton of the
whole servlet and you will note the usual try,catch,finally
structure for error handling that I have mentioned
before.
At the start of the method, we firstly (apart
from getting a PrintWriter handle) get the field
values that were passed from our HTML
Form.
This is very straightforward and we use the
following syntax: String
name=req.getParameter("name");.
The key to this being the getParameter()
method of the HttpServletRequest object (req).
The rest of doPost() is then very straightforward
- after creating a Notes Thread using: NotesThread.sinitThread();,
the following methods are then called in turn:
|
connectDomino() - Does
all the necessary setting up the Domino
Session, connecting to databases etc
createComment() -Creates
the comment document in the target database
outputMessage() - Sends
a confirmation message back to the user
recycleDomino() - Cleans
up and recycles the Domino Objects created
|
connectDomino()
The connectDomino() method is where all the
Domino session, database etc connecting is done.
|
s = NotesFactory.createSession();
db=s.getDatabase("",dbname);
dbLink=s.getDatabase("",dbnameLink);
vwLink=dbLink.getView(viewLinkName);
docLink=vwLink.getDocumentByKey(id,true);
if(docLink!=null){category=docLink.getItemValueString("title");}else{category="General";id="0";}
|
You can see over the first three lines the
creation of the 'Notes Session' and then connecting
to two notes databases (the variables dbname
and dbnamelink were set earlier).
The first database (db) is where we are going
to create a new Notes Document, and the second
(dbLink) is a 'link database' I use to match
up the article id passed from the HTML
Form to the full article name.
The fourth and fifth lines are getting handles
to the id lookup view in the Link Database and
then the document that matches the id.
The final line of the code simply sets the
String variable category to the article title
for later use when creating the 'Comment' document.
When writing this article, I noticed that really
I should check that the two databases have been
found before proceeding - if they are not the
error would be handled by the NotesException
handler but it would be better practice to check
them for null and return a more meaningful error.
You will perhaps also notice that once you
get the hang of putting java together, dealing
with exceptions etc the actual code part is
not too dissimilar to LotusScript - so if you
have used LotusScript you are most of the way
there already.
createComment(type,name,subject,organisation,country,email,website,comment)
This method obviously creates the 'Comment'
document and is very very simple - any LotusScript
developer will recognise the syntax.
It is called from the doPost() method and passes
all the parameters as String objects.
doc was declared as a NotesDocument in the
global declarations (together with db), and
db was 'attached' to the database within the
connectDomino() method.
|
doc=db.createDocument();
doc.replaceItemValue("form",form);
doc.replaceItemValue(FIELD_ID,id);
doc.replaceItemValue(FIELD_CATEGORY,category);
doc.replaceItemValue(FIELD_NAME,name);
doc.replaceItemValue(FIELD_SUBJECT,subject);
doc.replaceItemValue(FIELD_ORGANISATION,organisation);
doc.replaceItemValue(FIELD_COUNTRY,country);
doc.replaceItemValue(FIELD_EMAIL,email);
doc.replaceItemValue(FIELD_WEBSITE,website);
doc.replaceItemValue(FIELD_COMMENT,comment);
doc.computeWithForm(false,false);
doc.save();
|
The main point to notice are the use of the
"FIELD_" Strings that were declared
in the global declarations.
outputMessage(name,out)
When everything has been done - the document
created/saved etc - it is time to confirm to
the end user that the task has been completed.
To do this we need to get a handle to the PrintWriter
of the HttpServletResponse object. I have done
this by passes the PrintWriter to the outputMessage()
method using "out".
This is the code:
|
public void outputMessage(String
name, PrintWriter out) {
out.println("<html><head><title>ProjectDX.org</title></head><body>");
out.println("<br><br><center><font
size=3 face=\"Verdana, Arial, Helvetica,
sans-serif\">");
out.println("ProjectDX.org</font></center>");
out.println("<br><br><br><center><font
size=3 face=\"Verdana, Arial, Helvetica,
sans-serif\">");
out.println("Thankyou "+name+"</font></center>");
out.println("<br><br><br><br><div
align=\"center\" style=\"text-decoration:underline;cursor:pointer;color:blue;font:verdana
9pt\" onClick=\"window.close()\">Click
here to Close</div>");
out.println("</body></html>");
}
|
All I am doing is passing out basic HTML to
the PrintWriter together with the variable 'name'
- for personalisation.
recycleDomino()
The final part of the servlet is tidying everything
up. We have to handle our own objects and 'recycle'
them (using the recycle() method). We do this
as follows:
|
if(doc!=null){doc.recycle();}
if(db!=null){db.recycle();}
if(docLink!=null){docLink.recycle();}
if(vwLink!=null){vwLink.recycle();}
if(dbLink!=null){dbLink.recycle();}
s.recycle(); |
You will note that I have been good and check
the Notes Objects exist before calling the recycle()
method. An error would occur if I didn't do
this and the database name was incorrect etc.
Also note that I have recycled in reverse order
- ie session last and documents first.
Well thats the Java Servlet - hope you can
follow it - im not the worlds most gifted technical
writer - but I hope my examples do the 'real
explanation'.
I also tend not to explain how to do something
if I have in a previous article - so if you
are learning from scratch I would recommend
you read the previous Java Servlet articles
first.
|