1

In my servlet I have this code:

protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub

    Utils.CheckSession(request,response);

    String op = request.getParameter("op");
    int DaysToAdd = request.getParameter("daysToAdd") != null ? Integer.valueOf(request.getParameter("daysToAdd")) :  0;

    ArrayList<Giorno> giorni = new ArrayList<Giorno>();

    /*HERE I FILL ArrayList giorni and calculate other variables*/


    if ("cal".equals(op))
    {
        request.setAttribute("giorni", giorni);
        request.setAttribute("daysToAdd", DaysToAdd);
        request.getRequestDispatcher("GestioneCalendario.jsp").forward(request, response);

    }
    else if("utente".equals(op))
    {
        // ricavare abbonamento dell'utente
        String idu = (String) request.getAttribute("idu");
        Abbonamento a = null;
        int iDa = Utils.getIdAbbonamentoAttivoUtente(idu);
        a = Utils.getAbbonamentoFromId(iDa);
        request.setAttribute("abbonamento", a);
        request.getRequestDispatcher("Lezioni.jsp").forward(request, response);

    }
    else
    {
        request.setAttribute("giorni", giorni);
        request.setAttribute("daysToAdd", DaysToAdd);       
        request.getRequestDispatcher("GestioneLezioniNuovoLayout.jsp").forward(request, response);

    }

}

Maybe the problem is in the method CheckSession??

public static void CheckSession(HttpServletRequest request,
        HttpServletResponse response) {
    // TODO Auto-generated method stub
     HttpSession session = request.getSession(true);
     String logged = null;
     if (session!=null)
          logged = (String) session.getAttribute("logined");
     if(logged == null)
     {
         request.setAttribute("errore", "Devi loggarti!");
        try {
            request.getRequestDispatcher("Admin.jsp")
            .forward(request, response);
            return;
        } catch (ServletException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
     }

but it gives me this exception:

GRAVE: Servlet.service() for servlet [TakeDates] in context with path [/Spinning] threw     exception
java.lang.IllegalStateException: Cannot forward after response has been committed
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:349)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:339)
at servlet.TakeDates.doGet(TakeDates.java:368)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
at java.lang.Thread.run(Thread.java:695)

the servlet is called with this url

http://localhost:8080/Spinning/TakeDates?op=cal

Anyone can help me? thanks in advance!

Martina
  • 1,852
  • 8
  • 41
  • 78
  • It would be helpful to see the entire method. Can you please provide. – robbmj Mar 30 '14 at 12:27
  • @robbmj: the methos is really really long :| – Martina Mar 30 '14 at 12:30
  • Fair enough, just look for a line where you have written to the response writer before forwarding the request. If you find one then that is the problem – robbmj Mar 30 '14 at 12:32
  • @robbmj I edited the question with my code, maybe the problem is in the CheckSession Method that I call initially in my servlet? – Martina Mar 30 '14 at 12:35
  • @robbmj I'm quite sure that the problem is with CheckSession. If the user is not logged, the code after istruction Utils.CheckSession() is executed. – Martina Mar 30 '14 at 12:39
  • Okay I see your problem in the `CheckSession` method. See answers for details – robbmj Mar 30 '14 at 12:47
  • @MartinaF what line of code you have at `TakeDates.doGet(TakeDates.java:368)` ? – Abhishek Nayak Mar 30 '14 at 12:51

2 Answers2

5

Somewhere in your code you have committed some response to the response object.

You have to dispatch to your jsp page before you commit any output to the response object.

From the Documentation

forward should be called before the response has been committed to the client (before response body output has been flushed). If the response already has been committed, this method throws an IllegalStateException. Uncommitted output in the response buffer is automatically cleared before the forward.

The problem is that you are forwarding to the admin.jsp page when you should be redirecting in the Utils.CheckSession method

request.getRequestDispatcher("Admin.jsp").forward(request, response);

Should be

response.sendRedirect("Admin.jsp");
return false;

// in the doGet method
if (!Utils.CheckSession(request,response)) {
    return;
}

Redirects do not happen immediately, the servlet will continue execution and when it hits the next RequestDispatcher.forward call the exception is raised.

The server needs to send the http: redirect status code in the http response, the browser then receives the response and requests resource specified by the redirect url.

robbmj
  • 16,085
  • 8
  • 38
  • 63
0

Include a foward slash ie '/' on your urls. I had the same problem and a simple foward slash came to my rescue.

Mwangi Thiga
  • 1,339
  • 18
  • 22