Rapid web development
August 30, 2005 on 5:30 pm | In Java | 1 commentI am experimenting with the following technique to speed up web application development with Java.
First, define an interface called Controller with a single method called handleRequest that takes an HttpServletRequest and an HttpServletResponse as arguments. The method should also 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;
}
Then, create a hierarchy of JSP files and folders based on user stories. For example, say, we are writing a page to allow users to register new accounts. We’ll create the following files.
In the /user/register folder:
- index.jsp This is the entry point for our controller.
- default.jsp This is the view that is shown by default.
- success.jsp This is the view that is displayed when user registration is successful.
Now, in index.jsp, write the following:
< %
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 the default.jsp and success.jsp pages.
For example, default.jsp looks 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">
I am not sure how viable this is as as way to speed up web application development, but the following benefits are already obvious.
index.jspserves as the controller.- There is no need to re-compile after adding controllers since JSP pages are compiled automatically, thus speeding up code-test cycle.
- There is no need for servlet mappings. The folder structure and the designation of
index.jspas a welcome file take care of that. - URLs are clean and clear.
There are certainly weaknesses in using this scheme, but as I wrote at the beginning, I am still experimenting with this.
Related Posts:
- MVC framework (again!)
- How to identify and fix an anemic domain model
- EJB Exception Handling
- How to use multiple triggers in RIFE
Using Apache Axis in NetBeans 5.0
August 26, 2005 on 4:33 pm | In Java | 33 comments*Introduction*
I did not want to have to install Sun Java Application Server on my computer just to use web services. So, I looked around on the Internet and, from the information I gathered here and there, managed to put together this short tutorial on how to set up NetBeans projects to use Axis for web services.
*Server-side*
- Create a new web application project called *AxisTest*.
- Register the Axis JARs as a required library for the project.
- Add the following to
web.xml.org.apache.axis.transport.http.AxisHTTPSessionListener AxisServlet org.apache.axis.transport.http.AxisServlet AdminServlet org.apache.axis.transport.http.AdminServlet 100 SOAPMonitorService org.apache.axis.monitor.SOAPMonitorService SOAPMonitorPort 5001 100 AxisServlet /servlet/AxisServlet AxisServlet *.jws AxisServlet /services/* SOAPMonitorService /SOAPMonitor AdminServlet /servlet/AdminServlet 30 wsdl text/xml xsd text/xml index.html index.jsp index.jws - Write the service class.
/* * HelloService.java * * Created on 26 August 2005, 15:57 * * $Id$ * $Log$ */ package test; /** * * @author Eddy.Young * @version $Revision$ */ public class HelloService { public String sayHello(String name) { return "Hello, " + name; } } - Create
and.deploy.wsdd files in.undeploy.wsdd /WEB-INF/._WSStuff.deploy.wsdd_
urn:test _WSStuff.undeploy.wsdd_
- Customise and add the following to
build.xml. - Build and deploy project.
*Client-side*
The service should now be available from *http://localhost:8088/AxisTest/services*.
- Create a new project.
- Register the Axis JARs as required library for the project.
- Customise and add the following to
build.xml. - Write client code.
package clienttest; import test.*; public class Main { public static void main(String[] args) throws Exception { HelloServiceServiceLocator sl = new HelloServiceServiceLocator(); HelloService service = sl.getHelloService(); System.out.println(service.sayHello(args[0])); } }
Technorati Tags: NetBeans, Axis
Related Posts:
- Internet service providers promise faster broadband for 2006
- National Health Card Project in Brazil
- Rapid web development
- HOWTO: Improved JPOX integration with NetBeans 4.0
The Mighty Mouse
August 3, 2005 on 1:42 pm | In General, Apple | Add a commentApple has finally released a “two-button” mouse, the “Mighty Mouse”:http://www.apple.com/uk/mightymouse/.
Technorati Tags: apple
Related Posts:
- Apple releases Mac OS X 10.4.7 update
- Mac OS X 10.2.8
- Boot Camp for Mac OS X on Intel-based Macs
- TiBar, an application launcher
Powered by blog.mu with Pool theme design by Borja Fernandez.

