iT邦幫忙

1

jsp +mysql 資料過多就無法轉跳頁面

大家好,想請教一個問題
我有2個jsp頁面,我index頁面可以勾選資料(資料是從mysql撈出)
在check.jsp 呈現勾選資料
我本來實作都沒問題,
但mysql資料超過100筆後
index頁面就突然不能跳到check.jsp了....
請問是不是還要做甚麼設定呢?

<%@ page language="java" contentType="text/html; charset=UTF-8" 
pageEncoding="UTF-8"%>
<%@ include file = "db.jsp"%> 
<!DOCTYPE html>
<html>
<body>
<div class="container">

<form action = "index.jsp" method = "post">
<table>
<thead>
 <tr><th>choose</th><th>id</th><th>name</th></tr></b>
</thead>
<tbody id="myTable">
    <%     
      String sql = "select * from send"; 
        pstmt = conn.prepareStatement(sql);  
         ResultSet rs = pstmt.executeQuery(); 
         while(rs.next()){ 
                        String name = rs.getString("name");
                        String c_id = rs.getString("c_id");         
 out.print("<tr><td><center>"+"<input type=checkbox name='a' 
value="+c_id+">"+"</center></td><td>"+ c_id + "</td><td>"+name+"</td></tr> "); 
               }
                rs.close(); 
        %>

   </tbody>
 </table></br>
 <center>  <input type = "submit" name = "index" value = "send"> 
 </form>
</div>
<%    String[]  a = request.getParameterValues("a"); 

    int i;
     if(a!=null)
       {  sql = "select* from send where  "; 
          for(i=0;i<a.length;i++)
             {
                sql=sql+   " c_id ='" + a[i] + "' ";
             if(i!=a.length-1){ sql=sql+   " or  "; } 
             }
              session.setAttribute("sql", sql);
              response.sendRedirect("check.jsp");
      } 
  %> 
</body>
</html>
</html>

謝謝

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
2
暐翰
iT邦大師 1 級 ‧ 2019-03-26 12:18:14
最佳解答

代表有錯誤在圖片中的位子,可以提供該段sql這樣才能判斷問題出在哪

另外詢問這系統會提供外網使用嗎
看到樓主使用字串拼接SQL,這有SQL注入問題

看更多先前的回應...收起先前的回應...
joker iT邦新手 5 級 ‧ 2019-03-26 12:27:37 檢舉
 String sql = "select* from send where  ";
for(i=0;i<a.length;i++){
   sql=sql+   " c_id ='" + a[i] + "' "; 
     if(i!=a.length-1){ sql=sql+   " or  "; } 
                }
             out.print(sql+"");
           
                  session.setAttribute("sql", sql);
                  response.sendRedirect("check.jsp");
                  /////

輸出的字串會是這樣

select* from send where c_id ='A335' or c_id ='A336' or c_id ='A338'

其實我這程式碼之前就在用(但之前資料少,都OK)
但破百筆資料後開始有問題......

我的程式單純內網使用而已喔

暐翰 iT邦大師 1 級 ‧ 2019-03-26 12:45:31 檢舉

star1530

推測
1.慢查詢原因,導致timeout,可以換成一分鐘驗證一下

pstmt.setQueryTimeout(60);

2.C_ID中有特殊字串導致

joker iT邦新手 5 級 ‧ 2019-03-26 13:25:46 檢舉

@暐翰
1/第一個方法還是不行....
2.我有測試過了,只要我刪除第101筆資料,他就能轉跳明細了....
https://ithelp.ithome.com.tw/upload/images/20190326/20116290dXuybmx2ck.png
但若我輸入第101筆資料(不管事甚麼資料)他都會掛掉.....

joker iT邦新手 5 級 ‧ 2019-03-26 13:59:17 檢舉

暐翰
我發現我無法直接轉跳頁面,
但是,當我在index.jsp勾選選項後
自己到網址欄打check.jsp按enter
就可以看到我的清單明細了....(但一定要自己手動轉跳

joker iT邦新手 5 級 ‧ 2019-03-26 15:28:55 檢舉

暐翰
我解決了~
我用

  <script>  window.open("check.jsp"); </script> 

就解決了
不知為何

response.sendRedirect("check.jsp");

就是不行....

暐翰 iT邦大師 1 級 ‧ 2019-03-26 15:39:15 檢舉

star1530

想到了
sendRedirect會使用Get不是Post方式,所以組成的參數URL太長導致錯誤
舉例: http://xxx.x.x/check.jsp?a='A001'.....a='A101'
以IE來講,超過url 2083長度限制

可以改用forward才能將Form的參數帶過去

joker iT邦新手 5 級 ‧ 2019-03-26 15:54:07 檢舉

默默的試了一下

 RequestDispatcher rd;
       rd = getServletContext().getRequestDispatcher("check.jsp");
       rd.forward(request,response);

forward似乎也無法..

謝謝暐翰

0
Darwin Watterson
iT邦好手 1 級 ‧ 2019-03-26 13:27:49

坦白說, 直接把SQL語法赤裸裸的寫進JSP裡非常不建議 !
一般都會分層架構吧!
form -> DAO -> DB 寫入
DB -> DAO -> view 撈出

Java 開發網頁還是採用一些 MVC 的框架比較好 ! 例如: Spring

joker iT邦新手 5 級 ‧ 2019-03-26 15:55:53 檢舉

因為是小程式,所以就沒用MVC了~

0

因為你用request有一定量的限制
通常應該是要用request去傳條件再去sql搜尋

你可以改用將要傳的值組成一個字串再傳過去(使用js)
說不定就可以成功了

joker iT邦新手 5 級 ‧ 2019-03-26 15:55:17 檢舉

可是....我現在也是傳一組字串耶,我會在試試看,謝謝

我要發表回答

立即登入回答