2011-12-10 89 views
0

我爲一個在線表單創建了一個Servlet,每次我嘗試提交在線表單時,它都會給我以下異常:java.lang.NumberFormatException:對於輸入字符串:「」。問題是我的jsp文件中沒有任何字符串變量。還有一件事:當我嘗試填寫在線表單中的所有字段時,信息會發送到數據庫,但是當我只用其中的一些嘗試時,它會再次給我這種異常。這是我的代碼:Servlet字符串異常

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 
    } 

} 
+0

每當你有'用request.getParameter(「XXX」)'你有一個字符串。 –

回答

2

我猜想,這個問題是從類似這些線路中的一個來此一:

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

如果request.getParameter("redspring")值是一個空字符串,它會給你這個例外。

0

根據documantation:

第一個參數是空值或零長度的字符串:

NumberFormatException類型的異常,如果發生任何的 以下情況被拋出。

可能當你不把所有的參數,你解析空字符串