Servlet Wizard

This Servlet Wizard has two pages, the first one is used to create the Servlet and JSP.

In the second page you can type some message to show in the JSP, and selected a checkbox to generate the 'optional' onPost() method.

The wizard is too simple, but it is just to show how to use two Velocity templates (one for the Servlet, other to the JSP)



It also shows the "container" element that let's you choose the Java package (Servlet) and the WebRoot folder (JSP).

The checkbox just puts a boolean value in the velocity context, so you can use the #if expression normally.



How to create this Wizard

To create this Servlet wizard you just have to create three files:

ServletWizard.xml (wizard configuration file), Servlet.vm (Servlet template) and the jsp.vm (JSP template)

Create the ServletWizard.xml file:


<?xml version="1.0" encoding="UTF-8"?>
<EclipseWork>
    <wizard>
        <title>Create a simple Servlet+JSP</title>

	<!-- First Page -->
        <component-page >
        	<description>Create Servlet + JSP</description>

        	<group label="Servlet">
			<container name="package" label="Package:" type="package" />
			<textfield name="servlet" label="Servlet Name: " />

			<type name="extends" label="Extends HttpServlet: " value="javax.servlet.http.HttpServlet"/>
		</group>

		<group label="JSP">
			<container name="folder" label="WebRoot :" type="folder" />
			<textfield name="jsp" label="JSP Name: " />
		</group>
	</component-page>

	<!-- Second Page -->
	<component-page>
		<description>Optional WizardPage</description>

		<textfield name="javadoc" label="Type the Servlet Javadoc: " />
	     	<textfield name="message" label="Type some Message: " />
	     	
	     	<group>
	     		<checkbox name="checkPost" label="Implement doPost() ?" />
	     	</group>
	</component-page>

	<!-- Templates -->
        <output>
        	<template component="jsp" velocity="jsp.vm" extension="jsp" container="folder" />
            	<template component="servlet" velocity="Servlet.vm" extension="java" container="package" />
	</output>
    </wizard>
</EclipseWork>



The XML file defines the wizard. When the "Finish" button in the wizard is selected, EclipseWork will start the code generation, and it will use the Velocity Templates defined in the XML configuration file.

Create the Servlet.vm Template:




package $package;

import java.io.IOException;

import javax.servlet.ServletException;
import $extends.fullyName;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author $javadoc
 *
 */
public class $class extends $extends.name {

	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setAttribute("msg", "$message");
		req.getRequestDispatcher("${jsp.fullPath}").forward(req, resp);
	}

#if($checkPost)
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req,resp);
	}
#end
}



Create the jsp.vm Template:


<%=request.getAttribute("msg")%>		

Running the Wizard

To run the wizard just right click in the EclipseWork.xml file, and select "EclipseWork -> OpenWizard".

This project is in continuing development. Any feedback will be appreciated.

Servlet code

The wizard will generate a Servlet like this (and also the hello.jsp):


/**
 * @author This is the javadoc
 *
 */
public class HelloServlet extends HttpServlet {

	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setAttribute("msg", "HelloWorld message");
		req.getRequestDispatcher("/jsp/hello.jsp").forward(req, resp);
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req,resp);
	}
}