I am working on some coursework for a simple Java EE web application that records blog comments. On the add comment page there are two fields: name
and comments
I am using a RequestDispatcher
to pass control to an acknowledgement.jsp
if the fields are not empty, and an error.jsp
if one (or both) are empty passing a relevant message to the acknowledgement
page.
This is the logic I have:
RequestDispatcher disp;
if (!name.isEmpty() && !comment.isEmpty())
{
CommentBean commentBean = new CommentBean();
commentBean.setComment(comment);
commentBean.setName(name);
req.setAttribute("comment", commentBean);
disp = req.getRequestDispatcher("acknowledgement.jsp");
disp.forward(req, resp);
return;
}
else if (!name.isEmpty() && comment.isEmpty())
{
disp = req.getRequestDispatcher("error.jsp");
statusMessage = "Comment field empty";
req.setAttribute("status", statusMessage);
disp.forward(req, resp);
}
else if (name.isEmpty() && !comment.isEmpty())
{
disp = req.getRequestDispatcher("error.jsp");
statusMessage = "Name field empty";
req.setAttribute("status", statusMessage);
disp.forward(req, resp);
}
else
{
disp = req.getRequestDispatcher("error.jsp");
statusMessage = "Name and comment fields empty";
req.setAttribute("status", statusMessage);
disp.forward(req, resp);
}
The code does what I want but I can't help thinking this could be written a lot better but I am not sure how to go about it.
Edit: If this question should be written in another way please advise & I will edit straight away
Aucun commentaire:
Enregistrer un commentaire