Lately, I have been experimenting with a technique to accelerate web application development in Java. It does without custom servlets and relies on Java Server Pages (JSP).
Here is how it is implemented.
First, define an interface called Controller
. Declare a method called handleRequest
that takes as arguments an HttpServletRequest
and an HttpServletResponse
. The method must throw an Exception
.
package web;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public interface Controller {
public void handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception;
}
Next, create a hierarchy of directories and JSP files. The structure must follow the logical organisation of the application. For example, assuming we are writing a page for users to register new accounts, we create the following files in a directory called /user/register
.
- Â index.jsp — the entry point for our controller
- default.jsp — the default view
- success.jsp — the view for a successful registration
Next, in file index.jsp
, write the following code.
<% web.Controller c = new web.Controller() {
public void handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
account = null;
messages = new java.util.ArrayList();
username = request.getParameter(username);
password1 = request.getParameter(password1);
password2 = request.getParameter(password2);
email = request.getParameter(email);
if (request.getParameter(register) != null) {
registerActionPerformed(request, response);
}
request.setAttribute(messages, messages);
request.setAttribute(username, username);
request.setAttribute(email, email);
if (account != null) {
request.getRequestDispatcher(success.jsp).
forward(request, response);
} else {
request.getRequestDispatcher(default.jsp).
forward(request, response);
}
}
protected void registerActionPerformed(HttpServletRequest request,
HttpServletResponse response) throws Exception {
if (validate()) {
model.user.Account existing = model.user.AccountRepository.
getByUsername(username);
if (existing != null)
messages.add(error.duplicate_username);
existing = model.user.AccountRepository.getByEmail(email);
if (existing != null)
messages.add(error.duplicate_email);
if (messages.isEmpty()) {
model.user.RegisterAccountService service =
new model.user.RegisterAccountService();
account = service.register(username, password1, email);
}
}
}
protected boolean validate() throws Exception {
if (username == null || username.trim().length() == 0)
messages.add(error.username_missing);
if (password1 == null || password1.trim().length() == 0)
messages.add(error.password_missing);
if (password1 != null)
if (!password1.equals(password2))
messages.add(error.password_mismatch);
if (email == null || email.trim().length() == 0)
messages.add(error.email_missing);
return messages.isEmpty();
}
private model.user.Account account;
private java.util.ArrayList messages;
private String username;
private String password1, password2;
private String email;
};
c.handleRequest(request, response);
%>
Next, create files default.jsp
and success.jsp
. File default.jsp
could look like this:
< %@page contentType=text/html%>
< %@page pageEncoding=UTF-8%>
< %@taglib uri=http://java.sun.com/jsp/jstl/core prefix=c%>
< %@taglib uri=http://java.sun.com/jsp/jstl/fmt prefix=fmt%>
< !DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN
http://www.w3.org/TR/html4/loose.dtd>
<html>
<body>
<h1><fmt:message key=title.register_account /></h1>
</body>
</html>
So far this approach has shown the following benefits.
index.jsp
becomes a controller.- Because JSP are compiled automatically, I avoid manual compilation.
- I don’t have to map individual JSP files, as I do with servlets.
- URLs are consistent.
This technique is unsuitable for large projects but is good enough for writing quick web application projects.