Servlet字符串异常

时间:2011-12-10 21:09:43

标签: string jsp exception servlets

我为在线表单创建了一个Servlet,每次我尝试提交在线表单时,它都会给我以下异常:java.lang.NumberFormatException:对于输入字符串:“”。问题是我的jsp文件中没有任何String变量。还有一件事:当我尝试填写在线表单中的所有字段时,信息将被发送到数据库,但是当我只尝试使用其中的一些时,它再次给了我这个例外。这是我的代码:

    import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import database.databases;

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

   /**
    * @see HttpServlet#HttpServlet()
    */
   public feildsSERVLET()
   {
      super();
      // TODO Auto-generated constructor stub
   }

   /**
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
    *      response)
    */
   protected void doGet(HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException
   {
      try
      {
         Class.forName("com.mysql.jdbc.Driver");
         Connection connection = DriverManager.getConnection(
               "jdbc:mysql://localhost:3306/WhizzoChocolate", "root", "");
         try
         {
            /*
             * int customerID = Integer.parseInt(request
             * .getParameter("customerID"));
             */
            int frog = Integer.parseInt(request.getParameter("frog"));

            int redspring = Integer.parseInt(request.getParameter("redspring"));
            int bluespring = Integer.parseInt(request
                  .getParameter("bluespring"));
            int silverspring = Integer.parseInt(request
                  .getParameter("silverspring"));

            int ramsquare = Integer.parseInt(request.getParameter("ramsquare"));
            int ramoval = Integer.parseInt(request.getParameter("ramoval"));
            int ramhex = Integer.parseInt(request.getParameter("ramhex"));
            int rambutt = Integer.parseInt(request.getParameter("rambutt"));

            int product_id = Integer.parseInt(request
                  .getParameter("product_id"));
            Statement statement = connection.createStatement();

            int upd = statement
                  .executeUpdate("INSERT INTO `product`(`product_id`, `RWA`, `BWA`,`SWA`, `OSA`,`SSA`,`HSA`,`BSA`)"
                        + "VALUES" + "(" + product_id
                        + frog

                        + ", "

                        + redspring

                        + ", "

                        + bluespring

                        + ", "

                        + silverspring

                        + ", "

                        + ramoval

                        + ", "

                        + ramsquare

                        + ", "

                        + ramhex

                        + ", " + rambutt + ")");

            request.setAttribute("product_id", product_id);
            request.setAttribute("frog", frog);

            request.setAttribute("redspring", redspring);
            request.setAttribute("bluespring", bluespring);
            request.setAttribute("silverspring", silverspring);

            request.setAttribute("ramsquare", ramsquare);
            request.setAttribute("ramoval", ramoval);
            request.setAttribute("ramhex", ramhex);
            request.setAttribute("rambutt", rambutt);

         }
         finally
         {
            connection.close();

         }
      }
      catch (Exception e)
      {
         throw new ServletException(e);
      }

      ServletContext context = getServletContext();
      RequestDispatcher dispatcher = context
            .getRequestDispatcher("/feildsjsp.jsp");
      dispatcher.forward(request, response);
   }

   /**
    * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
    *      response)
    */
   protected void doPost(HttpServletRequest request,
         HttpServletResponse response) throws ServletException, IOException
   {
      // TODO Auto-generated method stub
   }

}

2 个答案:

答案 0 :(得分:2)

我猜这个问题来自其中一条类似于这一行:

int redspring = Integer.parseInt(request.getParameter("redspring"));

如果request.getParameter("redspring")的值是空字符串,它将为您提供此异常。

答案 1 :(得分:0)

根据文件说明:

  

如果有任何类型,则抛出NumberFormatException类型的异常   发生以下情况:

     

第一个参数为null或是一个长度为零的字符串。

可能在您不发送所有参数时,您解析空字符串