0

我是 Java EE 的初学者,在创建登录页面时遇到问题。我制作了一个通用登录页面,工程师可以通过该页面登录到他/她的帐户。工程师的用户名以“engg”开头。因此,我编写了以下代码,并在输入错误的用户名(例如我的表中不存在的“uname100”)时出现 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();
        }
    }
}
4

1 回答 1

1

顺便说一句,我已经解决了这个问题。我修改了代码。工作代码是:

 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();
    }

}
}
    }
于 2012-07-24T03:06:46.403 回答