2012-05-28 2 views
-3

쿼리를 실행 한 경우에만 응용 프로그램에 로그인하는 것이 목적 인 서블릿을 작성했습니다 ... 이제 유효하지 않은 사용자 이름과 ID에 사용할 조건이 무엇입니까 ... 조건을 작성할 수 없습니다 .pls 도움말 나와 서블릿은 ... 서블릿이 ...다른 페이지로 리디렉션하기위한 조건은 무엇입니까?

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'가 null/empty인지 확인합니다. '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); 
    } 
} 

뭔가 내가 생각 적절할 것 : 여기

코드 리팩토링한다.

관련 문제