66 909
Assignments Done
99,1%
Successfully Done
In October 2018

Answer to Question #5730 in Java | JSP | JSF for dilini

Question #5730
Write a Servlet that takes the user’s name and age from a form.
• Echo back the name and age along with a message stating the price of movie tickets.
• The price is determined by the age passed to the JSP.
• If the age is greater than 62, the movie ticket price is $7.00.
• If the user is less than 10 years old, the price is $5.00.
• For everyone else, the price is $9.50.
Expert's answer
package com.ndrei;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
& * Servlet implementation class Echo
& */
public class Echo extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
& * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
& */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
& performTask(request, response);
}

/**
& * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
& */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
& performTask(request, response);
}

private void performTask(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
& int age = Integer.valueOf(request.getParameter("age"));
& String price = "$9.50";
& if (age > 62) {
& price = "$7.00";
& } else if (age < 10) {
& price = "$5.00";
& }
&
& response.setContentType("text/html");
& PrintWriter out = response.getWriter();
& out.println("<html>");
& out.println("<body>");
& if (age < 0 || age > 120 ) {
& out.println("<h2>");
& out.println("Error!! Invalid age");
& out.println("</h2>");
& } else {
& out.println("<h2>");
& out.println("Name:");
& out.println(request.getParameter("name"));
& out.println("</h2>");
& out.println("<h2>");
& out.println("Price:");&
& out.println(price);
& out.println("</h2>");
& }
& out.println("</body>");
& out.println("</html>");
& out.close();
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions