2012-07-08 118 views
0

我在Java EE的初學者,我在創建登錄page.I作出了共同的登錄頁面,通過該工程師可以登錄到他/她的account.Engineers用戶名有問題開始與「ENGG」。所以,我已經對輸入錯誤的用戶名,如「uname100」,這是不存在於我的表下面的代碼並獲得IllegalStateException異常錯誤。IllegalStateException異常錯誤

import java.io.IOException; 
import java.io.PrintWriter; 
import java.sql.ResultSet; 
import java.sql.SQLException; 

import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

import sun.java2d.pipe.GeneralCompositePipe; 

import accessdb.Dao; 
import accessdb.Getset; 

/** 
* Servlet implementation class AdminLogIn 
*/ 
public class AdminLogIn extends HttpServlet { 
    private static final long serialVersionUID = 1L; 

    /** 
    * @return 
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 
    */ 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     // TODO Auto-generated method stub 
     try { 
      String username=request.getParameter("username"); 
      String password=request.getParameter("password"); 
      Getset g=new Getset(); 
      Dao dao=new Dao(); 
      g.setuname(username); 
      String newuname=username.substring(0,4); 
      // System.out.println(""+username); 
      if(newuname.equals("engg")) 
      { 
       ResultSet rs=dao.EnggLogInUseridCheck(g); 

       System.out.println(""+newuname); 

       while(rs.next()) 
       { 
        String uname=rs.getString("uname"); 
        System.out.println(""+uname); 
        if(uname.equals(username)) 
        { 
         g.setuname(username); 
         System.out.println(""+username); 
         System.out.println(""+uname); 
         ResultSet rs1=dao.EnggLogInPasswordCheck(g); 
         while(rs1.next()) 
         { 
          String password1=rs1.getString("password"); 
          if(password1.equals(password)) 
          { 
           System.out.println(""+password1); 
           response.sendRedirect("engghome.jsp"); 
           break; 
          } 
          else 
          { 
           response.sendRedirect("login.jsp"); 
           return; 
          } 
         } 
         break; 
        } 
        else 
        { 
         response.sendRedirect("login.jsp"); 
         return; 
        } 
       } 
      } 
      else 
      { 
       response.sendRedirect("login.jsp"); 
       return; 
      } 
     } catch (SQLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (ClassNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
} 
+2

和異常的堆棧跟蹤? – 2012-07-08 20:55:57

回答

1

通過我已經解決了疑難問題修改了code.And工作代碼的方式是:

import java.io.IOException; 
import java.io.PrintWriter; 
import java.sql.ResultSet; 
import java.sql.SQLException; 

import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

import sun.java2d.pipe.GeneralCompositePipe; 

import accessdb.Dao; 
import accessdb.Getset; 

/** 
* Servlet implementation class AdminLogIn 
    */ 
public class Login extends HttpServlet { 
private static final long serialVersionUID = 1L; 

/** 
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 
*/ 
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    // TODO Auto-generated method stub 

    String username=request.getParameter("username"); 
    System.out.println(""+username); 

    String password=request.getParameter("password"); 
    System.out.println("test user"+username+"GGG"+password); 
    if(username.equals("") || password.equals("") || username.equals(" ") || password.equals(" ")) 
    { 
     response.sendRedirect("login.jsp"); 

    } 
    else 
    { 
     try { 
    Getset g=new Getset(); 
    Dao dao=new Dao(); 
    g.setuname(username); 

    String newuname=username.substring(0,4); 


    if(newuname.equals("engg")) 
    { 

      ResultSet rs; 
      rs = dao.EnggLogInCheck(g); 
      while(rs.next()) 
      { 
       String uname=rs.getString("uname"); 
       String password1=rs.getString("password"); 
       if(uname.equals(username) && (password.equals(password1))) 
       { 
        response.sendRedirect("engghome.jsp"); 
        break; 
       } 

      } 

      if(rs.next()==false) 
      { 
       response.sendRedirect("login.jsp"); 
       return; 
      } 


    } 

    else if(newuname.equals("empl")) 
     { 

       ResultSet rs; 
       rs = dao.EmplLogInCheck(g); 
       while(rs.next()) 
       { 
        String uname=rs.getString("uname"); 
        String password1=rs.getString("password"); 
        if(uname.equals(username) && (password.equals(password1))) 
        { 
         response.sendRedirect("empcmp.jsp"); 
         break; 
        } 

       } 

       if(rs.next()==false) 
       { 
        response.sendRedirect("login.jsp"); 
        return; 
       } 


     } 
    else if(username.equals("system")) 
     { 

       ResultSet rs; 
       rs = dao.LogInAdmin(g); 
       while(rs.next()) 
       { 

        String password1=rs.getString(1); 
        if(password.equals(password1)) 
        { 
         response.sendRedirect("adminhome.jsp");System.out.println("ffffff"); 
         break; 
        } 
        else 
        { 
         response.sendRedirect("login.jsp"); 
         return; 
        } 
       } 




      } 


    else 
      { 
       response.sendRedirect("login.jsp"); 
       return; 

      } 



    } 
     catch (SQLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (ClassNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

} 
} 
    } 
相關問題