2012-05-28 33 views
-3

我寫了一個servlet,其目的是隻有在查詢執行時才登錄到應用程序......現在用於無效用戶名和id的條件是什麼...我無法寫出條件..請幫助我出... servlet是...重定向到不同頁面的條件?

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     // TODO Auto-generated method stub 
     try{ 
      Class.forName("oracle.jdbc.driver.OracleDriver"); 
      Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl ","scott","tiger"); 
      System.out.println("cnnection est"); 
     int Id = Integer.parseInt(request.getParameter("id")); 
     String Name=request.getParameter("firstname"); 
     boolean b=true; 

     //Connection con =JdbcConnectionUtil.getConnection(); 

     PreparedStatement pst = con.prepareStatement("select * from login where id=? and firstname=?"); 
     pst.setInt(1, Id); 
     pst.setString(2, Name); 

     ResultSet rs = pst.executeQuery(); 
     if(rs!=null && rs.next()) 
     { 

     //while(rs.next()){ 
      PrintWriter pw = response.getWriter(); 
      System.out.println("here"); 
      pw.println("hello"); 
      pw.println(rs.getInt(1)); 
      pw.println(rs.getString(2)); 
      pw.println(rs.getString(3)); 

     } 
     //} 
     else 
     { 
      RequestDispatcher rd = request.getRequestDispatcher("/LoginFailed.html"); 

     } 
//  


     } 
     catch(Exception ex){ 

      ex.printStackTrace(); 

     } 
    } 

回答

1

使用rd.forward將解決我認爲的問題。

How to forward requests from Servlet to JSP

+0

此條件是否有效? if(rs!= null && rs.next()) – Anil

+0

是的。它會檢查你的結果集rs是否爲空/空。在'userid'和'firstname'不匹配的情況下,結果集'rs'將是空的,因此條件將失敗,導致else塊中的語句被執行。 – Logan

+0

@Anil:'rs!= null'檢查沒有用。 'executeQuery()'方法永遠不會返回空結果集。 –

1

首先,你檢查正確的參數,然後你做的邏輯。另外不要忘記關閉語句和連接以避免內存泄漏。

這裏被重構代碼:

@Override 
protected void doGet(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException { 
    //get parameters from request 
    try { 
     String idParam = request.getParameter("id"); 
     String name = request.getParameter("firstname"); 

     //check if request contains such parameters 
     if (idParam == null || name == null) { 
      throw new IllegalArgumentException(
        "Id and Name parameters must not be null."); 
     } 

     //try casting idParam to int 
     Integer id = null; 
     try { 
      id = Integer.parseInt(idParam); 
     } catch (NumberFormatException nfe) { 
      throw nfe; 
     } 

     PreparedStatement pst = null; 
     Connection con = null; 
     try { 
      Class.forName("oracle.jdbc.driver.OracleDriver"); 
      con = DriverManager.getConnection(
        "jdbc:oracle:thin:@localhost:1521:orcl ", "scott", "tiger"); 

      pst = con.prepareStatement(
        "select * from login where id=? and firstname=?"); 
      pst.setInt(1, id); 
      pst.setString(2, name); 

      //check if result returned any data 
      ResultSet rs = pst.executeQuery(); 
      if (!rs.next()) { 
       throw new Exception(
         "No such user for id: " + id + " and name: " + name); 
      } 

      PrintWriter pw = response.getWriter(); 
      pw.println("hello"); 
      pw.println(rs.getInt(1)); 
      pw.println(rs.getString(2)); 
      pw.println(rs.getString(3)); 

     } catch (Exception ex) { 
      throw ex; 
     } finally { 
      try { 
       if (pst != null) { 
        pst.close(); 
       } 
       if (con != null) { 
        con.close(); 
       } 
      } catch (SQLException sqle) { 
       throw sqle; 
      } 
     } 
    } catch (Exception ex) { 
     ex.printStackTrace(); 
     RequestDispatcher rd = request.getRequestDispatcher("/LoginFailed.html"); 
     rd.forward(request, response); 
    } 
} 

喜歡的東西,這將是合適的,我認爲。