IllegalStateException错误

时间:2012-07-08 21:59:27

标签: java exception servlets

我正在创建一个登录页面,工程师可以通过以“engg”开头的用户名登录。 问题是在登录页面中,当我给出正确的密码输入时,它给出了“非法状态异常”。在错误的输入中它工作正常。就像当我在我的oracle表中给出“engg140”时,它给出了异常代码是:

     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;

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

               if(username.equals("") || password.equals(""))

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

             }
         else{
    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);
                         System.out.println(""+password);
                        response.sendRedirect("engghome.jsp");
                        break;

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

            }

        }
        response.sendRedirect("login.jsp");
    }
    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();
    }
    }
          }

右输入的控制台输出如下:

                     03:31:26,265 INFO  [STDOUT] engg
                           03:31:26,265 INFO  [STDOUT] engg172
                          03:31:26,265 INFO  [STDOUT] engg140
                          03:31:26,265 INFO  [STDOUT] engg140
                          03:31:26,265 INFO  [STDOUT] engg140
                         03:31:26,281 INFO  [STDOUT] 365125
                          03:31:26,281 INFO  [STDOUT] 365125
                           03:31:26,281 INFO  [STDOUT] engg141
                           03:31:26,281 ERROR [[AdminLogIn]] Servlet.service() for 
                              servlet                      
                          AdminLogIn threw exception
                           java.lang.IllegalStateException
                           at                                                                                       org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:435)
                         at AdminLogIn.doGet(AdminLogIn.java:80)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:241)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:580)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown Source)

2 个答案:

答案 0 :(得分:3)

您没有包含堆栈跟踪的“由...引起”部分,这是最重要的部分(实际上,整个堆栈跟踪很重要)。

IllegalStateException上的sendRedirect()与HTTP协议有关,并且在已经提交了对客户端的响应时发生。此时,由于HTTP标头已被发送(或至少排队等待发送)并且无法更改,因此无法再发送重定向。在代码中的某个地方,在您未显示的方法中,您已经向输出流写了一些内容并导致响应被提交。

这与数据库无关。

答案 1 :(得分:3)

我认为响应已经提交,不允许您进行重定向。

点击此处了解更多信息: