![]() |
![]() |
![]() |
![]() |
Program the Servlet
Now we area ready to start programming the Servlet which will process the information from the form. The first version of the Servlet will simply greet the person who sent the form.
- Choose the 'Servlet Methods' tab of the Servlet Composer.
- Choose 'doGet' from the choice box to the left of the Code Sourcerer button. The doGet method processes HTTP 'GET' requests.
- Press the Code Sourcerer button. A Code Sourcerer window will appear. We will use the Code Sourcerer to generate the code for this servlet.
- Choose 'Servlet operations', the first item in the list. This option appears only in the Servlet composer or the HttpServlet composer.
- Press Next. You will now see a list of things you can do with Servlets.
- Choose 'Set the MIME output type'. This sets the kind of output which will be generated.
- Choose 'text/html' from the choicebox to the right. You will be outputting html text. If the MIME type you desired was not in the list, you could have typed it in yourself.
- Press Done. The Code Sourcerer will produce a line of Java code which will set the servletResponse object. The code reads
Next we will prepare the Servlet to output text.
- Press the Code Sourcerer button.
- Choose 'Servlet operations' and press Next.
- Choose 'Prepare the Servlet to write text' and hit Done. You will see one more window, asking for the return value.
- Press Ok without making any changes. The Code Sourcerer will produce a line of Java code which reads
Next we will create some text to display.
- Press the Code Sourcerer button.
- Choose 'Servlet operations' and press Next.
- Choose 'Send text to the output' and press Next.
- Since the name of the PrintWriter was left at the default value of 'out', press Next.
- You will now be asked where the text should come from. Choose 'The following text'.
- Type "Hello" into the text area to the right and press Done.
The sourcerer will produce a line of code which reads
You have now created a simple servlet. Now you will test it.
- Go back to the IDE, but do not close the HttpServlet Composer.
- Click on 'All files' at the bottom of the 'Project Groups' section in the left of the IDE. This will display all of the files in your project directory, including html files.
- Double-click on 'NameForm.html' to open the file in your browser. Type in your name and press 'Send'.
Your browser will show the response it has received, which is the word "Hello". The Servlet Composer will launch a new window, called the ServletRequest Viewer. This window shows all the information which was sent along with the last request to your servlet.
- Select the third tab of the ServletRequest Viewer, 'Form Variables'. These are the variables which were sent with the form.
- Click on the line which begins with 'Rep_Name'. At the bottom of the page you will see the Java code needed to access this variable in your program.
- Select the text at the bottom of the ServletRequest Viewer and type Control-C to copy it.
- Return to the Servlet Composer. Be sure you have chosen the doGet method of your Servlet. Be sure that the cursor is on a new line at the bottom of your code.
- Hit Control-V to paste the text into the doGet section of your Servlet.
- Insert a return after the code you just pasted.
Now you will have your servlet output this value back to the browser.
- Press the 'Code Sourcerer' button.
- Choose 'Servlet Operations' and press Next.
- Choose 'Send text to the output' and press Next.
- Leave the name of the PrintWriter object as 'out' and press Next.
- Choose the third option on the page, 'the following variable expression'.
- Type 's' in the text field to the right.
- Press done.
Your servlet should now have the following code in it
You can test your Servlet again.
- Go back to the IDE, but do not close the HttpServlet Composer.
- Double-click on 'NameForm.html' to open the file in your browser.
- Type your name into the browser form and press 'Send'.
You have now created a simple Servlet which responds to a form. In the next section you will modify your Servlet so it searches a database. If you wish to stop now, save your changes in the HttpServlet Composer, close it, and quit the IDE. When you wish to resume, open up the Tutorial7 project and then the Response HttpServlet composer.
Data Representations, Inc. http://www.datarepresentations.com support@datarepresentations.com sales@datarepresentations.com |
![]() |
![]() |
![]() |
![]() |