0

我正在创建一个登录页面,工程师可以通过以“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)
4

2 回答 2

3

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

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

这与数据库无关。

于 2012-07-09T03:35:26.490 回答
3

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

在这里查看更多信息:

于 2012-07-09T05:59:50.793 回答