illegal state exception in Servlet/JSP


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.util.*;
    import java.sql.*;

    public class MyServlet extends HttpServlet {
                  public void init(ServletConfig config) throws ServletException {

                  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 {
                        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);
                        ResultSet rs = stmt.executeQuery();

                        while ( {
                        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);

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

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


                  catch (ClassNotFoundException e) {
                  catch (SQLException e) {
                  finally {
                             if (con!=null) {
                                                try {
                                          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.
  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