Jamar Butler MMI 656 Web Design Technologies Assignment 4 ✓ Solved

Jamar Butlermmis 656 Webdesign Technologiesassignment 4instructor Dr

Write a servlet that returns a randomly chosen greeting from a list of five different greetings. The greetings must be stored as constant strings in the program.

Write the markup document to create a form with the following capabilities: a. A text widget to collect the user’s name b. Four checkboxes, one each for the following items: i. Four 25-watt light bulbs for $2.39 ii. Eight 25-watt light bulbs for $4.29 iii. Four 25-watt long-life light bulbs for $3.95 iv. Eight 25-watt long-life light bulbs for $7.49 c. A collection of three radio buttons that are labeled as follows: i. Visa ii. Master Card iii. Discover

Write a servlet that computes the total cost of the ordered light bulbs from Exercise 11.8 after adding 6.2 percent sales tax. The servlet must inform the buyer of exactly what was ordered, in a table.

Sample Paper For Above instruction

Introduction

This paper provides a comprehensive solution to the three core tasks outlined in the assignment: creating a servlet that generates random greetings, designing an HTML form for light bulb orders with multiple options, and developing a servlet to calculate the total cost including sales tax. Each component will be detailed and explained to demonstrate an effective approach to web development using Java Servlets and HTML.

Part 1: Servlet that Returns a Random Greeting

The initial task involves creating a Java Servlet that, upon being accessed, displays a randomly selected greeting from a fixed list of five greetings. The greetings are stored as constant string variables within the servlet class, ensuring easy maintenance and readability.

Implementation details include defining the greeting constants and utilizing the java.util.Random class to select one greeting at random. The servlet responds with a simple HTML output that displays the greeting message to the user.

Example Code:

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class GreetingServlet extends HttpServlet {

private static final String[] GREETINGS = {

"Hello!", "Welcome!", "Hi there!", "Good day!", "Greetings!"

};

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html");

PrintWriter out = response.getWriter();

// Select a random greeting

int index = (int) (Math.random() * GREETINGS.length);

String greeting = GREETINGS[index];

// Output the greeting

out.println("

Jamar Butlermmis 656 Webdesign Technologiesassignment 4instructor Dr");

out.println("

" + greeting + "

");

out.println("