-1

After compiling this servlet, it shows the following error:
Note: src\IceCream1.java uses unchecked or unsafe operations.

Note: Recompile with -Xlint: unchecked for details.

Here's the code :

import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class IceCream1 extends HttpServlet {

    HttpSession session;
    String kind;
    Order ord;

    private static class Order {

        String kind;
        String with;
        int quantity;

        Order(String thekind) {
        kind = thekind;
        quantity = 1; // default quantity
        }

        Order(String thekind, String withwhat) {
        kind = thekind;
        with = withwhat;
        quantity = 1; // default quantity
        }    

        String getName() {
        if (with == null) 
            return kind;
        return kind + " with " + with;
        }

        int getQuantity() {
        return quantity;
        }

        void setQuantity(int n) {
        quantity = n;
        }

    }

    private static class KillSession extends HttpServlet {
            public void doGet(HttpServletRequest request,
                          HttpServletResponse response)
            throws IOException, ServletException
        {
        // get the session, 
        HttpSession session = request.getSession(true); 

        // invalidate it
        session.invalidate();

        response.setContentType("text/html");

        // session is retrieved before getting the writer

            PrintWriter out = response.getWriter();

            out.println("<html>");
            out.println("<body bgcolor=\"white\">");
            out.println("<head>");

        out.println("<title> Ice cream </title>");

            out.println("</head>");
            out.println("<body>");

        out.println("<h1>Thanks for trying our ice cream!</h1>");

        out.println("Click");
        out.println("<A HREF = \"IceCream1\">here</A>");
        out.println("to buy more ice cream");

        out.println("</body>");
        out.println("</html>");

        out.close();

        }

        public void doPost(HttpServletRequest request,
                          HttpServletResponse response)
            throws IOException, ServletException
        {
        doGet(request,response);
        }
    }

    // the user gets to the page without submitting a form 
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
    {
        response.setContentType("text/html");

    // session is retrieved before getting the writer

    session = request.getSession(true); 

    // see what has been ordered already
    Vector ordered = (Vector) session.getAttribute("order");

    PrintWriter out = response.getWriter();

        out.println("<html>");
        out.println("<body bgcolor=\"white\">");
        out.println("<head>");

    out.println("<title> An ice cream shop </title>");
        out.println("</head>");
        out.println("<body>");

    out.println("<h1>Welcome to our ice cream shop!</h1>");

    // print out what has been ordered
    if (ordered == null && kind == null) {
        session.setAttribute("order", new Vector());
        out.println("You have not ordered anything yet.<BR>");
        out.println("Try our ice cream, it's delicious!<P>");
    }
    else if (ordered == null) { 
        // add the current order to the new order list
        Vector v = new Vector();
        v.add(new Order(kind));
        session.setAttribute("order", v);
    }
    else if (kind != null) {
        ordered.add(new Order(kind));
        session.setAttribute("order", ordered);
    }

    kind = null;

    if ( ordered != null) {
        out.println("You have ordered:<P>");
        // extract all orders
        Enumeration orders = ordered.elements();
        while (orders.hasMoreElements()) {
        out.println(((Order)orders.nextElement()).getName());
        out.println("<P>");
        }
    }

    out.println("Please click on ice cream you want to buy<BR>");
    out.println("to add it to your order<P>");

    // a button for vanilla ice cream
        out.println("<P>");
        out.print("<form action=\"");
        out.print(response.encodeURL("IceCream1"));
        out.print("\" ");
        out.println("method=POST>");
        out.println("<input type=hidden name=\"toBuy\" value=\"Vanilla\">");
        out.println("<br>");
        out.println("<input type=submit value=\"Vanilla\">");
        out.println("</form>");

    // a button for chocolate ice cream
        out.println("<P>");
        out.print("<form action=\"");
        out.print(response.encodeURL("IceCream1"));
        out.print("\" ");
        out.println("method=POST>");
        out.println("<input type=hidden name=\"toBuy\" value=\"Chocolate\">");
        out.println("<br>");
        out.println("<input type=submit value=\"Chocolate\">");
        out.println("</form>"); 

    // a button for strawberry ice cream
        out.println("<P>");
        out.print("<form action=\"");
        out.print(response.encodeURL("IceCream1"));
        out.print("\" ");
        out.println("method=POST>");
        out.println("<input type=hidden name=\"toBuy\" value=\"Strawberry\">");
        out.println("<br>");
        out.println("<input type=submit value=\"Strawberry\">");
        out.println("</form>"); 

    // a button to erase the session
        out.println("<P>");
        out.print("<form action=\"");
        out.print(response.encodeURL("KillSession"));
        out.print("\" ");
        out.println("method=POST>");
        out.println("<br>");
        out.println("<input type=submit value=\"Erase all!\">");
        out.println("</form>");


    out.println("</body>");
    out.println("</html>");

    out.close();

    }

    // the user submits one of the forms
    public void doPost(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
    {

    //session = request.getSession(true);

    kind = request.getParameter("toBuy");


    // the rest is the same for POST and GET, 
    // so we let doGet() do the job
        doGet(request, response);
    }

}

Thanks in advance!

Sandesh
  • 1,154
  • 3
  • 21
  • 38
Gyneth Ashtoreth
  • 23
  • 1
  • 1
  • 4
  • Thats a warning not an error! – Aniket Thakur Dec 26 '13 at 13:47
  • There is no use of encapsulation. All the members are having default access – Keerthivasan Dec 26 '13 at 13:47
  • You are using raw type all over the place... try to use generics writing code for java 1.5 and above – A4L Dec 26 '13 at 13:49
  • [This](http://stackoverflow.com/questions/4925708/suppressing-java-unchecked-warnings-in-jsp-files) might give thought. – Siva Tumma Dec 26 '13 at 13:50
  • Please try doing some research before asking a question. Googling for 'uses unchecked or unsafe operations' returns over a quarter of a million results... – SimonC Dec 26 '13 at 13:54
  • If you compile with the -Xlint:unchecked option the compiler should print detailed information, where the unchecked operation is. (Exactly what the warning says.) Please use minimal effort trying to solve the problem on your own before posting a question next time. – fabian Dec 26 '13 at 13:59
  • I already did lots of research and most of the answers are not applicable to my situation – Gyneth Ashtoreth Dec 26 '13 at 14:00

1 Answers1

3

This is probably because of not inner class because of raw type of vector.

Vector v = new Vector();

and also

  Vector ordered = (Vector) session.getAttribute("order");

That is asking you to parametrze the type.

Foe ex:

  Vector<Order> ordered = (Vector<Order>) session.getAttribute("order");

And also for

  Enumeration orders = ordered.elements();

Check the type for Enumeration also.

P.s :I wonder still you are using Vector Other than List

Suresh Atta
  • 114,879
  • 36
  • 179
  • 284