-
illegal state exception in Servlet/JSP (4 messages)
- Posted by: mick fgyggggya
- Posted on: May 31 2001 14:48 EDT
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";
}
}Threaded Messages (4)
- illegal state exception in Servlet/JSP by Usha Iengar on June 06 2001 18:26 EDT
- illegal state exception in Servlet/JSP by Arthur Kevin MvGrath on November 22 2011 00:06 EST
- illegalstateexception by karthik merugu on March 12 2013 04:23 EDT
- illegalstateexcexception by karthik merugu on March 14 2013 10:28 EDT
-
illegal state exception in Servlet/JSP[ Go to top ]
- Posted by: Usha Iengar
- Posted on: June 06 2001 18:26 EDT
- in response to mick fgyggggya
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 -
illegal state exception in Servlet/JSP[ Go to top ]
- Posted by: Arthur Kevin MvGrath
- Posted on: November 22 2011 00:06 EST
- in response to mick fgyggggya
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.
-
illegalstateexception[ Go to top ]
- Posted by: karthik merugu
- Posted on: March 12 2013 04:23 EDT
- in response to mick fgyggggya
take return away at the end of code and try..
-
illegalstateexcexception[ Go to top ]
- Posted by: karthik merugu
- Posted on: March 14 2013 10:28 EDT
- in response to mick fgyggggya
after every "forward()" method in the code add "return" statement