这些内部课程有什么问题?

时间:2013-12-26 13:44:27

标签: java servlets

编译此servlet后,会显示以下错误:
Note: src\IceCream1.java uses unchecked or unsafe operations

Note: Recompile with -Xlint: unchecked for details.

这是代码:

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);
    }

}

提前致谢!

1 个答案:

答案 0 :(得分:3)

这可能是因为没有内部类,因为矢量的原始类型。

Vector v = new Vector();

以及

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

那就是要求你参数化这个类型。

敌人:

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

还有

  Enumeration orders = ordered.elements();

同时检查枚举的类型。

P.s:我想知道你还在使用Vector而不是List