0

I am creating a login page where Engineer can login through their username started with "engg". The problem is in the login page when I am giving the right input with right password it gives "Illegal State Exception".In wrong inputs it works fine.Like When i am giving "engg140" present in my oracle table it is giving the exception.the code is:

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

The console output for a right input is as follows:

                     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)
Lion
  • 18,729
  • 22
  • 80
  • 110
Mistu4u
  • 5,132
  • 15
  • 53
  • 91

2 Answers2

3

You did not included the "caused by" sections of the stack trace, which are the most important part (actually, the WHOLE stack trace is important).

An, IllegalStateException on a sendRedirect() has to do with the HTTP protocol, and occurs when the response to the client has already been committed. At this point it is not possible to send a redirect any more as the HTTP headers have been sent (or at least queued to be sent) and cannot be changed. Somewhere in your code, in a method you haven't shown, you have written something to the output stream and caused the response to be committed.

This has nothing to do with the database.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
3

I think the response is already committed which is not allowing you to do redirect.

Look here for more information:

Community
  • 1
  • 1
Ramesh PVK
  • 15,200
  • 2
  • 46
  • 50