illegal state exception in Servlet/JSP

Discussions

Web tier: servlets, JSP, Web frameworks: illegal state exception in Servlet/JSP

  1. illegal state exception in Servlet/JSP (4 messages)

    Hi I'm getting the following errpr in my servlet container:

    IllegalStateException in: R( + /mygallery.jsp + null) Cannot forward as OutputStream or Writer has already been obtained

    I'm not sure what the problem is or how to solve it. THe code for my servlet is below. THanks for your help

    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
    import java.util.*;
    import java.sql.*;

    public class MyServlet extends HttpServlet {
                  public void init(ServletConfig config) throws ServletException {
                      super.init(config);
                  }

                  protected void doPost(HttpServletRequest request, HttpServletResponse response)
                  throws ServletException, IOException {

                  HttpSession session = request.getSession(true);
                  Integer loginID = (Integer)session.getAttribute("loginID");
                  ServletContext context = getServletContext();
                  String userPhotoName = null;
                  String firstName1 = null;
                  int quantity = 0;

                  if (loginID == null) {
                       String referringURL = "/servlet/MyServlet";
                       session.setAttribute("referringURL", referringURL);
                       RequestDispatcher dispatcher = context.getRequestDispatcher("/login.jsp");
                       dispatcher.forward(request, response);
                 }
                 else {

                  Photo[] photoArray;
                  Collection photos = new ArrayList();
                  Connection con = null;

                  try {
                        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                        con = DriverManager.getConnection("jdbc:odbc:digitalfoto");
                        String sqlOutput = "select PhotoID, Description, photoName, "
                        + "userPhotoName, MyGallery.CustomerID, firstName from MyGallery,"
                        + "Customer where Customer.CustomerID=MyGallery.CustomerID "
                        + "and MyGallery.CustomerID=?";

                        PreparedStatement stmt = con.prepareStatement(sqlOutput);
                        stmt.clearParameters();
                        stmt.setInt(1,loginID.intValue());
                        ResultSet rs = stmt.executeQuery();

                        while (rs.next()) {
                        int photoID = (int)rs.getInt("photoID");
                        String description = (String)rs.getString("Description");
                        String photoName = (String)rs.getString("photoName");
                        userPhotoName = (String)rs.getString("userPhotoName");
                        firstName1 = (String)rs.getString("firstName");
                        //int customerID = (int)rs.getInt(4);
                        Photo photo = new Photo (photoID, userPhotoName, photoName,quantity);
                        photos.add(photo);
                        //request.setAttribute("userPhotoName",userPhotoName);
                        }

                        String firstName = firstName1 + "'s";
                        photoArray = (Photo [])photos.toArray(new Photo [0]);
                        request.setAttribute("photos", photoArray);
                       
                        request.setAttribute("firstName",firstName);

                        RequestDispatcher dispatcher = context.getRequestDispatcher("/mygallery.jsp");
                        dispatcher.forward(request, response);
                        rs.close();
                        stmt.close();


                  }

                  catch (ClassNotFoundException e) {
                          e.printStackTrace();
                  }
                  catch (SQLException e) {
                            e.printStackTrace();
                  }
                  finally {
                             if (con!=null) {
                                                try {
                                          con.close();
                                          }
                                          catch (Exception e) {}
                              }
                    }
                  }
                  }
                        public String getServletInfo() {
    return "MyGalleryServlet Information";
    }

    }
  2. This exception is thrown when you try to forward to another servlet, after starting to write the response. Try include instead of forward, or ensure that no output is written before the forward.
    Usha
  3. The illegal state exception is coming from the loop that processes your result sets.  The first time you go through this loop, everything works.  The second time through the loop, you have probably written enough data to force the output buffer to flush.  From that point on, you will throw an exception every time you forward a request to a JSP.

  4. illegalstateexception[ Go to top ]

     take return away at the end of code and try..

  5. illegalstateexcexception[ Go to top ]

    after every "forward()" method in the code add "return" statement