6

I am a beginner in the world of Java EE. I have been trying to create a simple login System using Servlets and JSP following the guide provided here http://come2niks.com/?p=1589. This is how my doPost() look like.

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
     PrintWriter out = response.getWriter();
    processRequest(request, response);
     try
    {
        System.out.println("In the Login Servlet");
        LoginBean user = new LoginBean();
        user.setUserName(request.getParameter("username"));
        user.setPassword(request.getParameter("pass"));
        LoginDAO db = new LoginDAO();
        user = db.login(user);

        System.err.println("I am Back !");
        if(user.isValid())
        {
            System.err.println("VALIDED.. ReDirecting..");
            System.err.println("Getting Session");
            HttpSession session = request.getSession(true);
            System.err.println("Got Session");
            session.setAttribute("currentSessionUser",user);
            System.err.println("Attribute Set");
            response.sendRedirect("Login_Success.jsp");


        }else
        {
            System.err.println(" NOT VALIDED.. ReDirecting..");
            response.sendRedirect("Login_Failed.jsp");
           out.println(" NOT VALIDED.. ReDirecting.."); 

        }


    } catch (Throwable exc)
    {
        System.out.println(exc);
    }
}

I have added some System.err to get some more info on the flow of the code.

This is what my glassfish says

INFO: In the Login Servlet
INFO: Driver loaded!
INFO: Database connected!
INFO: Welcome Ramesh
SEVERE: I am Back !
SEVERE: VALIDED.. ReDirecting..
SEVERE: Getting Session
SEVERE: Got Session
SEVERE: Attribute Set
INFO: java.lang.IllegalStateException
    at org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:524)
    at groovers.LoginServlet.doPost(LoginServlet.java:100)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:688)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
    at java.lang.Thread.run(Thread.java:722)

Please help me solve this problem.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Genocide_Hoax
  • 843
  • 2
  • 18
  • 42
  • What is the body of processRequest()? If it's issuing a response, then the sendRedirect() will fail because a response has already been committed. As Sean's answer says, you can check if the response has already been committed using response.isCommitted(). – Adrian Mar 13 '13 at 17:44
  • processRequest is just Printing some statements using PrintWriter – Genocide_Hoax Mar 13 '13 at 18:00

3 Answers3

15

I think before you call sendRedirect() the response is getting committed. It means the server has already flush the headers to the client. This will usually happen when either the response buffer has reached max size or someone has called flush() explicitly.

Since the response is already committed you cannot add any more headers. And because sendRedirect() requires Location header to be added to the response.

The server will throw IllegalSateException exception.

How to Fix it?

Make sure you are not writing any data before you do sendRedirect(). In your code, i suspect processRequest() method is writing to the response.

Please find here why the Response will get committed.

Cause of Servlet's 'Response Already Committed'

Community
  • 1
  • 1
Ramesh PVK
  • 15,200
  • 2
  • 46
  • 50
  • Thanks a lot that was it. The Function processRequest() which came in as a part of the Netbeans 7.3.1 Servlet Template was sending some response. Thanks a lot.. – Genocide_Hoax Mar 13 '13 at 18:13
3

This can happen if you try to write to the response when it has already been committed.

Without seeing more of your code it's hard to identify exactly where this is happening, but you can check by using:

response.isCommitted()

If this returns true the output has been committed and you wont be able to redirect.

The response can be committed by flushing, the end of the servlet being reached or the outputstream having been written to

Sean Landsman
  • 7,033
  • 2
  • 29
  • 33
3

You need to add

return;

after

response.sendRedirect("Login_Success.jsp");

and

response.sendRedirect("Login_Failed.jsp");

It is a problem of forwarding after response.

You can understand the requirement from the link - java.lang.IllegalStateException: Cannot (forward | sendRedirect | create session) after response has been committed

Community
  • 1
  • 1
Piyas De
  • 1,786
  • 15
  • 27