63 189
Assignments Done
99,3%
Successfully Done
In July 2018

Answer to Question #3915 in Java | JSP | JSF for shiva

Question #3915
Write a servlet that lets a user vote for his favorite food from a combo box or radio buttons (the user must able to make multiple food selection per request)
Expert's answer
Index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Main page</title>
</head>
<body>
<h1>Please, choose Your favorite food</h1>
<form action="http://localhost:8080/WebApplication/FoodServlet" method="post">
<input type="checkbox" name="food" value="Meat">Meat<br/>
<input type="checkbox" name="food" value="Fish">Fish<br/>
<input type="checkbox" name="food" value="Cheese">Cheese<br/>
<input type="checkbox" name="food" value="Fruit">Fruit<br/>
<input type="checkbox" name="food" value="Salad">Salad<br/>
<input type="checkbox" name="food" value="Dessert">Dessert<br/>
<input type="checkbox" name="food" value="Drink">Drink<br/>
<input type="submit" value="OK">
</form>
</body>
</html>


FoodServlet.java


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 FoodServlet extends HttpServlet {
/**
* Processes requests for both HTTP
* <code>GET</code> and
* <code>POST</code> methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String[] food = request.getParameterValues("food");

try {
/*
* TODO output your page here. You may use following sample code.
*/
out.println("<html>");
out.println("<head>");
out.println("<title>User choise</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>User choise:</h1>");
out.println("<ul>");
for(int i=0; i<food.length;i++){
out.println("<li>"+ food[i]);
}

out.println("</ul>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to
edit the code.">
/**
* Handles the HTTP
* <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP
* <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}

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