Creating a Servlet which uses Templates
We have thus far created a Servlet which can ask a database for information and display information from the database. Now we will use an HTML template to create a more attractive web page.
Preparing a Servlet to use Templates
Before our servlet can use templates, we need to add some more code to the declaration area of your servlet.
- Choose "Goto declaration code" from the Code menu. This will take you back to the declaration code area of the HttpServlet Composer.
- Click on the Code Sourcerer.
- Choose 'File operations...' and press Next.
- Choose the first option, 'Create a new File object from a pathname...' and press Next.
- You will be prompted to enter the full pathname of the file. Press the browse button to the right to open a system-dependent file dialog. Select the Tutorial3 directory (found in the Project folder of your Simplicity Professional installation) and choose the file 'NameTemplate.html'.
- Press done.
Next, we will edit the code in the doGet section of the code.
- Go to the Servlet methods tab of the Servlet Composer.
- Go to the doGet area.
- Remove the last 2 lines of code from the doGet area. They will read
- Go to the top of the doGet area, and add 2 slashes (//) before the lines which begin out.println. This will prevent this code from being run.
- Press the Code Sourcerer button.
- Choose 'Template operations...'. Press Next.
- Choose 'Create a new TemplateProcessor Object...' and press Next.
- Press Next to keep the default name for the file object.
- Press done to keep the default name for the TemplateProcessor.
- Press the Code Sourcerer button again.
- Choose 'Template operations...'. Press Next.
- Choose 'Add name/Value pairs to a TemplateProcessor...' and press Next.
- Press done to keep the default values for the name/value pair.
- Edit the code that is generated so that "value" is replaced with s. The line should now read
- Repeat steps 16-20, except enter '$sales' for the name and 'yourSales' for the value.
- Edit the code that is generated to remove the quotes from around yourSales.
After editing, this line of code should read
- Press the Code Sourcerer button again.
- Choose 'Template operations...'. Press Next.
- Choose 'Get processed text from TemplateProcessor'. Press done to keep the default name for the TemplateProcessor.
- Press Ok to accept the default return value.
- Type the following code into the doGet area. This code could be generated by the Code Sourcerer in Servlet Operations, if you wish.
- Choose 'Save' from the File menu to save your work.
- Choose 'Initialize Class' from the Program menu. You have made changes to the declaration area of the Servlet, which need to be run.
- Choose 'Execute init' from the Program menu. Initializing the Servlet will open the database connection.
- Go back to the IDE without closing the Servlet Composer.
- Open the html file you created earlier, NameForm.html, from the IDE. Your browser should open to the form page you created.
- Try typing in a name and pressing "send".
Try different names, like "Carl" or "Fred". The TemplateProcessor uses an existing HTML file which contains replacement variables (named $name and $sales). This simplifies the creation of Servlets by keeping the HTML formatting in an .html file (created with your favorite HTML editor) and making programming the Servlet output much less confusing.
| Data Representations, Inc. http://www.datarepresentations.com support@datarepresentations.com sales@datarepresentations.com |