Archive for category Servlet

How to create a web-site starter kit (Java)

What Is a Starter Kit?

Starter Kit is the easiest way to start using a new technology. Basically, it is a template project with a comprehensive documentation on where the project can be applied, which functionality is included and how to extend the template with your own functionality.

In this article we will go through a creation of your own StarterKit for a Servlet database application.

Project Structure

Create a simple java project in your development environment.

For example you can use the following structure:

src – directory for your source code packages

doc – documentation for the StarterKit

META-INF:

manifest.mf

WEB-INF:

            classes – compiled application classes

            lib – required libraries

            web.xml – configuration

Source Code

It is recommended to separate database logic from the front-end. For example, you can use com.yourpackage package for your servlet class source and com.yourpackage.data for a data-aware module.

In the servlet you can predefine some html code for the page layout, for example:

PrintWriter out = response.getWriter();

response.setContentType(“text/html”);

out .println(“<!DOCTYPE HTML PUBLIC \”-//W3C//DTD HTML 4.01 Transitional//EN\” \”http://www.w3.org/TR/html4/loose.dtd\”>”);

out.println(“<html>” );

out.println(“<body>”);

getDatabaseData();

out.println(“</body>”);

out.println(“</html>”);

getDatabaseData is a function, which will get data from the database module and publish it on the webpage. For a template project getDatabaseData() function should stay empty – this is the user responsibility to decide on the layout of the page, depending on his application requirements. To make this obvious – add an explicit comment inside the function template.

com.yourpackage.data package can be used for your database-handling module. The responsibilities of the module are:

set up a database connection

provide an easy API to get database data

close the connection on application termination

You can use pooling and create connection pool in ServletContextListener#contextInitialized event handler. The pool will have to be destroyed in ServletContextListener#contextDestroyed. Otherwise you may want to set up a new connection for each session using sessionCreated and sessionDestroyed events of HttpSessionListener. Note that sessionDestroyed event is only fired if the session expires, so it may be a better idea to close the connection explicitly.

ServletContextListener and HttpSessionListener classes should be defined in web.xml if used.

Documentation

To make the template project a StarterKit we will need to add documentation. A general project description and goal can be placed into index.html in the root directory. This will ensure that the user can easily find the documentation when the StarterKit is deployed.

More specific documentation, explaining how to create a sample application using the StarterKit can be placed into the doc folder.

The ready StarterKit can be packaged into a war file for easy deployment.

Leave a comment