2012-09-23 137 views
0

當我調用servlet時,它不會轉到所需的jsp頁面。下面是兩個代碼當servlet被調用時沒有轉發

有在日誌中說這樣的錯誤,但是這不是一個真正的理由不轉發到目的地

 java.sql.SQLException: Bad format for number 'bb' in column 4. 

調用這個代碼的servlet,可以肯定的URL映射

<a href="/Store/DisplayOrders"> Display Orders</a> 



     package admin; 

     import data.ConnectionPool; 
     import data.DBUtil; 
     import java.io.IOException; 
     import java.io.PrintWriter; 
     import java.sql.Connection; 
     import java.sql.PreparedStatement; 
     import java.sql.ResultSet; 
     import java.sql.SQLException; 
     import java.util.ArrayList; 
     import javax.servlet.RequestDispatcher; 
     import javax.servlet.ServletException; 
     import javax.servlet.http.HttpServlet; 
     import javax.servlet.http.HttpServletRequest; 
     import javax.servlet.http.HttpServletResponse; 

     /** 
     * 
     * @author One 
     */ 
     public class DisplayOrdersServlet extends HttpServlet { 

      @Override 
      public void doPost(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, IOException { 
       response.setContentType("text/html"); 
       PrintWriter out = response.getWriter(); 

       ConnectionPool pool = ConnectionPool.getInstance(); 
       Connection connection = pool.getConnection(); 
       PreparedStatement ps = null; 
       ResultSet rs = null; 

       ArrayList al = null; 
       ArrayList ordersList = null; 
       ordersList = new ArrayList(); 

       String query = "select * from orders order by order_id"; 

       try { 
       ps = connection.prepareStatement(query); 
       rs = ps.executeQuery(query); 


       while (rs.next()) { 

        al = new ArrayList(); 

        al.add(rs.getString("order_id")); 
        al.add(rs.getString("product_quantity")); 
        al.add(rs.getString("product_name")); 
        al.add(rs.getDouble("user_name")); 
        al.add(rs.getDouble("user_address")); 
        out.println("al :: " + al); 
        ordersList.add(al); 
       } 

       request.setAttribute("ordersList", ordersList); 

       String url = "/admin/orders.jsp"; 

        RequestDispatcher dispatcher = 
        getServletContext().getRequestDispatcher(url); 
        dispatcher.forward(request, response); 
      } 

      catch (SQLException e) { 
        e.printStackTrace(); 
       } finally { 
        DBUtil.closeResultSet(rs); 
        DBUtil.closePreparedStatement(ps); 
        pool.freeConnection(connection); 

     } 
      } 

      @Override 
      protected void doGet(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, IOException { 
      doPost(request, response); 

      } 
     } 

jsp頁面

  <%@page contentType="text/html" pageEncoding="UTF-8"%> 
      <%@ page language="java" import="java.util.*;"%> 
      <!DOCTYPE html> 
      <HTML> 
      <HEAD> 
      <TITLE>Orders CRUD</TITLE> 

      </HEAD> 

      <BODY> 
      <br> 
      <table> 

      </table> 
      <br> 
      <table > 
      <tr><td></td></tr> 

       <tr><td ><a href="/Store/admin/#.jsp">#</a></td></tr> 
      <tr><td></td></tr> 
      <tr> 
       <td>order_id</td><td>product_quantity</td><td>product_name</td> 
        <td>user_name</td><td>user_address</td> 
        <td>Edit</td><td>Delete</td> 
      </tr> 
        <% 
        int count=0; 
        List viewList = new ArrayList(); 
        Iterator viewItr; 

        if(request.getAttribute("ordersList")!=null && request.getAttribute("ordersList")!="") 
        { 
          List orderList = (ArrayList)request.getAttribute("ordersList"); 
          Iterator itr = orderList.iterator(); 

          while(itr.hasNext()) 
          { 



            viewList = (ArrayList)itr.next(); 
            int product_id = Integer.parseInt(viewList.get(0).toString()); 
            viewItr = viewList.iterator(); 
            %> 
            <tr> 
            <% 
            while(viewItr.hasNext()) 
            { 

              %> 
              <td><%=viewItr.next()%></td> 

              <% 

            } 
            count++; 
            %> 
            <td><input type="button" name="edit" value="Edit" onclick="editRecord(<%=product_id%>);" ></td> 
              <td><input type="button" name="delete" value="Delete" onclick="deleteRecord(<%=product_id%>);"></td> 
            </tr> 
            <% 
          } 
        } 
        if(count==0) 
        { 
          %> 
          <tr><td > </td></tr> 
         <tr><td>No Record Avaliable</td></tr> 
          <% 
        } 
        %> 
       <tr><td></td></tr> 
      </table>  
      </BODY> 
      </HTML> 

回答

1
try { 
    ... 
    al.add(rs.getDouble("user_name")); <-- Exception thrown 
    al.add(rs.getDouble("user_address")); 
     ... 
    String url = "/admin/orders.jsp"; 
    RequestDispatcher dispatcher = 
    getServletContext().getRequestDispatcher(url);       
    dispatcher.forward(request, response); 
} catch (SQLException e) { <-- Exception catch 
    e.printStackTrace();  

當你得到一個異常,異常之後的代碼被跳過,直至例外情況很嚴重。重定向代碼根本不被執行。也就是說,將名稱或地址作爲雙重名稱是不尋常的。

+0

謝謝!我想我是通過複製displayproduct servlet而落在那裏的,並且真的忽略了每次我一直在檢查最後一小時:))現在多數民衆贊成複製代碼的經典案例。 – TrackmeifYouCan

+0

這就是爲什麼'e.printStackTrace()'是一個非常糟糕的管理異常的方法... – SJuan76

+0

我怎樣才能瞭解更多關於這個例外業務?或任何建議? – TrackmeifYouCan

1

有一個很好的理由爲了讓servlet不轉發到JSP頁面:forward之前的語句之一拋出SQLException,因此其餘指令被跳過,並且catch塊被執行。由於轉發是拋出異常的語句之後的指令的一部分(因此異常而被跳過),因此不進行轉發。

的異常可能是由

rs.getDouble("user_name"); 

引起我懷疑你的用戶被命名爲3.452和67.43 ;-)

+0

謝謝!我想我是通過複製displayproduct servlet而落在那裏的,並且真的忽略了每次我一直在檢查最後一小時:))現在多數民衆贊成複製代碼的經典案例。 – TrackmeifYouCan

+0

男人你真的是一個java怪物,我看到你的個人資料和忍者隊(你的個人資料有趣哈哈)我希望自大學以來我不斷編碼,我停下來,在不同的領域工作,現在回到大好時機。想知道他們是否已經發明瞭一個java安裝程序,用於大腦os haha​​ha – TrackmeifYouCan

+0

好吧,謝謝。很高興知道原始法文文本的英文翻譯已被閱讀至少一次:-) –

相關問題