500错误所需的故障排除帮助(Java)

时间:2014-05-03 17:56:18

标签: java servlets cookies

我正在学习java课程,而且我已经打了一堵砖墙。在执行此代码之前,我创建了一个cookie,用于将运输编号存储为cookie名称,并将运输日期存储为cookie值。

表单然后要求用户输入确认号码。以下代码应该:

  • 将键入的字符串转换为整数
  • 检索已保存的Cookie并将名称转换为整数
  • 将输入的号码与保存的Cookie号码进行比较
  • 如果输入的号码和保存的号码相同,则会检索cookie值(日期为字符串)
  • 然后将cookie值转换为日历日期值
  • 然后将日期值与今天的日期进行比较
  • 如果发货号码有效(与存储的cookie匹配)且日期超过发货日期前5天,则用户会收到取消消息。如果没有,则用户收到备用消息。

还有一些事情可以阻止显而易见的事情:   - 是的,我知道在现实生活中,这将存储在数据库中。这是一个类赋值,我们目前不使用数据库。   - 我明白我没有说明没有用户输入。在代码工作之后我会谈到它。

以下代码抛出500错误,我不知道为什么。它编译得很好:

/*
 * cancelOrder servlet looks for the cookies associated
 * with the shopping cart confirmation page
 */

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

public class cancelOrder extends HttpServlet {

    //sets initial values for what we will retrieve
    //from the cookie
    int confirmNumber = 0;
    String shippingDate = "01/01/2000";
    Date date;

/** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
*/
public static long getDateDiffInDays(Date date1, Date date2) {
    long diffInMillis = date2.getTime() - date1.getTime();
    return TimeUnit.MILLISECONDS.toDays(diffInMillis);
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
    //gets the fields that the user filled
    String enteredConfirmNumber = request.getParameter("ConfirmationNumber");


    //converts confirmNumber from string
    //to integer 
    int enteredConfirmNo = Integer.parseInt(enteredConfirmNumber);

    //gets cookie value if it matches user input
    Cookie[] cookies = request.getCookies();
    Cookie cookie;
    for(int i=0; i<cookies.length; i++) {
        cookie = cookies[i];
        String foundCookie = cookie.getName();
        //converts cookie name from string
        //to integer 
        int cookieNo = Integer.parseInt(foundCookie);

        if(cookieNo == enteredConfirmNo) {
            confirmNumber = cookieNo;
            shippingDate = cookie.getValue();
        }
    }

    SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); 

    try {
        date = formatter.parse(shippingDate);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    //converts shippingDate from string
    //to calendar format
    Calendar shipDate = Calendar.getInstance();
    shipDate.setTime(date);

    //gets today's date in calendar format
    Calendar today = Calendar.getInstance();

    //unsure if this will throw int or string
    long daysTillShip = getDateDiffInDays(today.getTime(), shipDate.getTime());

    if(confirmNumber != 0 &&
        daysTillShip > 5) {
            showPage(response, "Your order was successfully canceled.");

    }  else {
        showPage(response, "Either it is less than 5 days before delivery " + 
                            "or your confirmation number does not exist");
    }
} 

/**
 * Actually shows the <code>HTML</code> result page
 */
protected void showPage(HttpServletResponse response, String message)
throws ServletException, java.io.IOException {
    response.setContentType("text/html");
    java.io.PrintWriter out = response.getWriter();
    out.println("<html>");
    out.println("<head>");
    out.println("<title>Status of Your Order</title>");  
    out.println("</head>");
    out.println("<body>");
    out.println("<h2>" + message + "</h2>");
    out.println("</body>");
    out.println("</html>");
    out.close();

}

/** Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
    processRequest(request, response);
} 

/** Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
    processRequest(request, response);
}
}
编辑:正如其他人所问,这是日志文件的内容:

May 03, 2014 12:20:02 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [cancelOrder] in context with path [/csj] threw exception
java.lang.NumberFormatException: null
    at java.lang.Integer.parseInt(Integer.java:454)
    at java.lang.Integer.parseInt(Integer.java:527)
    at cancelOrder.processRequest(cancelOrder.java:37)
    at cancelOrder.doPost(cancelOrder.java:118)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)

May 03, 2014 12:20:45 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [cancelOrder] in context with path [/csj] threw exception
java.lang.NumberFormatException: null
    at java.lang.Integer.parseInt(Integer.java:454)
    at java.lang.Integer.parseInt(Integer.java:527)
    at cancelOrder.processRequest(cancelOrder.java:37)
    at cancelOrder.doGet(cancelOrder.java:109)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)

1 个答案:

答案 0 :(得分:0)

我发现,正如stacktrace所示,问题与int有关。我最后的方向略有不同,但我希望它更清洁:

/*
 * cancelOrder servlet looks for the cookies associated
 * with the shopping cart confirmation page
 */

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

public class cancelOrder extends HttpServlet {

        //sets initial values for what we will retrieve
        //from the cookie
        String confirmNumber = "0";
        String shippingDate = "01/01/2000";
        Date date;

    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
    public static long getDateDiffInDays(Date date1, Date date2) {
        long diffInMillis = date2.getTime() - date1.getTime();
        return TimeUnit.MILLISECONDS.toDays(diffInMillis);
    }
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, java.io.IOException {
        //gets the fields that the user filled
        String enteredConfirmNumber = request.getParameter("ConfirmationNumber");


        //gets cookie value if it matches user input
        Cookie[] cookies = request.getCookies();
        Cookie cookie;
        for(int i=0; i<cookies.length; i++) {
            cookie = cookies[i];
            String foundCookie = cookie.getName();
            //converts cookie name from string
            //to integer 


            if(foundCookie.equals(enteredConfirmNumber)) {
                confirmNumber = foundCookie;
                shippingDate = cookie.getValue();
            }
        }

        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); 

        try {
            date = formatter.parse(shippingDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        //converts shippingDate from string
        //to calendar format
        Calendar shipDate = Calendar.getInstance();
        shipDate.setTime(date);

        //gets today's date in calendar format
        Calendar today = Calendar.getInstance();

        //unsure if this will throw int or string
        long daysTillShip = getDateDiffInDays(today.getTime(), shipDate.getTime());

        if(confirmNumber.equals(enteredConfirmNumber) &&
            daysTillShip > 5) {
                showPage(response, "Your order was successfully canceled.");

        }  else {
            showPage(response, "Either it is less than 5 days before delivery " + 
                                "or your confirmation number does not exist");
        }
    } 

    /**
     * Actually shows the <code>HTML</code> result page
     */
    protected void showPage(HttpServletResponse response, String message)
    throws ServletException, java.io.IOException {
        response.setContentType("text/html");
        java.io.PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Status of Your Order</title>");  
        out.println("</head>");
        out.println("<body>");
        out.println("<h2>" + message + "</h2>");
        out.println("</body>");
        out.println("</html>");
        out.close();

    }

    /** Handles the HTTP <code>GET</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, java.io.IOException {
        processRequest(request, response);
    } 

    /** Handles the HTTP <code>POST</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, java.io.IOException {
        processRequest(request, response);
    }
}