2014-05-18 5 views
0

이 jsp 코드를 작성했습니다. 기본 로그인 컨트롤입니다. 내 데이터베이스로 입력을 확인하려고합니다. 그들이 올바른지 아무 문제가 있지만 입력이 null이거나 데이터베이스에없는 경우 오류를 제공하고 게스트를 로그인 페이지로 리디렉션하려고합니다. 이 코드 두 줄에 JSP의 out.println

<%@ page import ="java.sql.*" %> 
<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<!DOCTYPE html> 
<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <title>Login Process</title> 
</head> 
<body> 
<% 
    String userid = request.getParameter("username");  
    String pwd = request.getParameter("password"); 
    Class.forName("com.mysql.jdbc.Driver"); 

    Connection con = DriverManager.getConnection("jdbc:mysql://localhost/ecommerce", "root1" , "1234"); 
    Statement st = con.createStatement(); 
    ResultSet rs; 
    rs = st.executeQuery("select * from users where UserFirstName='" + userid + "' and UserPassword='" + pwd + "'"); 
    while(rs.next()){ 
     if(userid.length()!= 0 || pwd.length()!= 0){ 
      if (rs.getInt("Admin") == 1) { 
       session.setAttribute("userid", userid); 
       response.sendRedirect("Admin.jsp"); 
      } else if(rs.getInt("Admin") == 0){ 
       session.setAttribute("userid", userid); 
       response.sendRedirect("User.jsp"); 
      } 
      else { 
       out.println("Invalid username or password <a href='index.jsp'>try again</a>"); 
      } 
     } 
     else{ 
      out.println("Empty username or password <a href='index.jsp'>try again</a>"); 
     } 
    } 
%> 
</body> 

else { 
      out.println("Invalid username or password <a href='index.jsp'>try again</a>"); 
      } 

작동하지 않습니다이 하나

else{ 
      out.println("Empty username or password <a href='index.jsp'>try again</a>"); 
     } 

브라우저는 대신 나에게 다른 범위를 빈 페이지를 제공합니다.

+0

테스트 사례를 공유하십시오. 문제를 재현하는 단계는 무엇입니까? – Braj

+0

손님이 비어있는 사용자 이름 또는 비밀번호를 인쇄해야하는 것보다 빈 줄을 제출하는 경우 잘못된 사용자 이름 또는 비밀번호를 입력하면 잘못된 사용자 이름 또는 비밀번호가 인쇄됩니다. 그게 전부 – TheHost

+0

단순히 데이터베이스 쿼리를 실행하기 전에'null'과 빈 문자열을 확인하십시오 – Braj

답변

1

입력이 널 (null) 또는 그들이 데이터베이스에없는 경우 나 오류를 더 기록을 확인하는 대신 while (rs.next())

사용 if (rs.next())을주고 싶다.


샘플 코드 : (당신의 필요 조건에 따라 그것을 수정)

String userid = request.getParameter("username"); 
String pwd = request.getParameter("password"); 

// check for empty username or password before making a call to database 
if (userid == null || userid.trim().length() == 0 || pwd == null 
     || pwd.trim().length() == 0) { 
    out.println("Invalid username or password <a href='index.jsp'>try again</a>"); 
} else { 
    ... // request for database query 
    if (rs.next()) { // use if instead of while to check for no record 
    ... 
    } else { 
    out.println("Empty username or password <a href='index.jsp'>try again</a>"); 
    } 
} 
0

는 시도의 코드를 동봉 - catch 블록을 어떤 예외가 발생했을 경우 로그인 페이지로 리디렉션됩니다. 이 디버깅을 위해 도움이 될 것입니다하지만 프로덕션 시스템에있는 경우 민감한 정보를 공개 할 수 있기 때문에

try 
{ 
String userid = request.getParameter("username");  
String pwd = request.getParameter("password"); 
Class.forName("com.mysql.jdbc.Driver"); 

Connection con = DriverManager.getConnection("jdbc:mysql://localhost/ecommerce", "root1" , "1234"); 
Statement st = con.createStatement(); 
ResultSet rs; 
rs = st.executeQuery("select * from users where UserFirstName='" + userid + "' and UserPassword='" + pwd + "'"); 

while(rs.next()) 
{ 
    if(userid.length()!= 0 || pwd.length()!= 0){ 
    if (rs.getInt("Admin") == 1) { 
      session.setAttribute("userid", userid); 
      response.sendRedirect("Admin.jsp"); 
     } else if(rs.getInt("Admin") == 0){ 
      session.setAttribute("userid", userid); 
      response.sendRedirect("User.jsp"); 
     } 
     else { 
      out.println("Invalid username or password <a href='index.jsp'>try again</a>"); 
     } 
    } 
    else{ 
     out.println("Empty username or password <a href='index.jsp'>try again</a>"); 
    } 
} 
} 
    catch (Exception e) 
    { 
      out.println("Exception: "+e.getMessage()+" :Invalid username or password <a href='index.jsp'>try again</a>"); 
     } 

e.getMessage(), 예외 메시지가 표시됩니다, 선택 사항입니다.