Rapid web development

August 30, 2005 on 5:30 pm | In Java | 1 comment

I 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.jsp serves 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.jsp as 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.

Technorati Tags: ,

Related Posts:

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*

  1. Create a new web application project called *AxisTest*.
  2. Register the Axis JARs as a required library for the project.
  3. 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
        
  4. 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;
        }
    }
    

  5. Create .deploy.wsdd and .undeploy.wsdd files in /WEB-INF/.

    _WSStuff.deploy.wsdd_

    
    
    
        
    
        
    
        
        
    
    
            urn:test
        
    

    _WSStuff.undeploy.wsdd_

    
    
        
    
    
  6. Customise and add the following to build.xml.
    
    
    
    
    
    
    
        
    
    
    
    
    
    
    
    
         
    
         
    
        
            
        
    
        
            
    
            
    
    
    
            
    
            
    
        
    
        
            
    
    
    
            
    
            
    
            
        
    
  7. Build and deploy project.

*Client-side*

The service should now be available from *http://localhost:8088/AxisTest/services*.

  1. Create a new project.
  2. Register the Axis JARs as required library for the project.
  3. Customise and add the following to build.xml.
    
    
    
    
        
    
    
    
        
    
        
            
        
    
        
            
    
            
    
    
            
    
            
                
            
    
            
            
        
    
  4. 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: ,

Related Posts:

The Mighty Mouse

August 3, 2005 on 1:42 pm | In General, Apple | Add a comment

Apple has finally released a “two-button” mouse, the “Mighty Mouse”:http://www.apple.com/uk/mightymouse/.

Technorati Tags:

Related Posts:




Powered by blog.mu with Pool theme design by Borja Fernandez.