File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Help needed, in Servelet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Help needed, in Servelet " Watch "Help needed, in Servelet " New topic
Author

Help needed, in Servelet

Somnath Das
Greenhorn

Joined: Jun 06, 2006
Posts: 11
Hi,
i have a register page, when i fill in the zip code on my jsp page i pass this info to a servelet and after that process some thing from the database and i redirect the same page using
response.sendRedirect("register.jsp?cityname="+city_name+"&statename="+state_name);

but after this statement is executed i am facing the error as shown below(the entire stack trace i am giving for referance)

11:40:54,801 INFO [STDOUT] java.lang.IllegalStateException
11:40:54,801 INFO [STDOUT] at org.apache.coyote.tomcat5.CoyoteResponseFacade.sendRedirect(CoyoteResponseFacade.java:352)
11:40:54,801 INFO [STDOUT] at ZipcodeGenerator.doPost(ZipcodeGenerator.java:163)
11:40:54,817 INFO [STDOUT] at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
11:40:54,817 INFO [STDOUT] at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
11:40:54,817 INFO [STDOUT] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
11:40:54,817 INFO [STDOUT] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
11:40:54,817 INFO [STDOUT] at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:75)
11:40:54,817 INFO [STDOUT] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:186)
11:40:54,817 INFO [STDOUT] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
11:40:54,817 INFO [STDOUT] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
11:40:54,817 INFO [STDOUT] at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
11:40:54,817 INFO [STDOUT] at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
11:40:54,817 INFO [STDOUT] at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
11:40:54,833 INFO [STDOUT] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
11:40:54,833 INFO [STDOUT] at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
11:40:54,833 INFO [STDOUT] at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:44)
11:40:54,833 INFO [STDOUT] at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
11:40:54,833 INFO [STDOUT] at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:169)
11:40:54,833 INFO [STDOUT] at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
11:40:54,833 INFO [STDOUT] at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
11:40:54,833 INFO [STDOUT] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
11:40:54,833 INFO [STDOUT] at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
11:40:54,833 INFO [STDOUT] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
11:40:54,848 INFO [STDOUT] at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
11:40:54,848 INFO [STDOUT] at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
11:40:54,848 INFO [STDOUT] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
11:40:54,848 INFO [STDOUT] at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
11:40:54,848 INFO [STDOUT] at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
11:40:54,848 INFO [STDOUT] at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
11:40:54,848 INFO [STDOUT] at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
11:40:54,848 INFO [STDOUT] at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
11:40:54,848 INFO [STDOUT] at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
11:40:54,864 INFO [STDOUT] at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
11:40:54,864 INFO [STDOUT] at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
11:40:54,864 INFO [STDOUT] at java.lang.Thread.run(Thread.java:534)



i would be really gratefull, if any one could solve this problem

Thank you,
Somnath Das.


Sun Certified Java Programmer (1.4)<br /> <br />(Progress isn't made by early risers, But by lazy people, trying to find easier ways to do the same)
Amit Ghai
Ranch Hand

Joined: Mar 24, 2004
Posts: 35
Can you post your ZipcodeGenerator servlet.


Reasonable people adapt themselves to the world. Unreasonable people attempt to adapt the world to themselves. All progress, therefore, depends on unreasonable people.
Somnath Das
Greenhorn

Joined: Jun 06, 2006
Posts: 11
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {


ResultSet rs = null;
Statement stmt;
String sql;
DatabaseConnection db = new DatabaseConnection();


response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();








try
{

sql="select city_info.City, state_info.State_name from city_zip_area_code_master as city_info," +
"country_state_code_master as state_info where city_info.Zip='"+ value + "' "+
"and city_info.State_ID=state_info.State_ID";

Connection conn =db.getConnection();

stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
if(rs.next())
{

response.sendRedirect("register.jsp?cityname="+rs.getString("City")+"&statename="+rs.getString("State_name"));



}

}
catch(Exception ex)
{
ex.printStackTrace();
}


}



Thank you,
Somnath Das.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

You've already written, flushed and closed the output, so you can't redirect anymore.
Remove the following code (Or put it in a "else" statement.) and try again.



I recommend you to call "return;" after the sendRedirect to avoid further writing.


[My Blog]
All roads lead to JavaRanch
naidu gulla
Greenhorn

Joined: Jul 19, 2006
Posts: 14
IllegalStateException is generated when we flush the response before doing a redirect
Somnath Das
Greenhorn

Joined: Jun 06, 2006
Posts: 11
Thanks for our support..., I removed the flush .., but still facing the same problem

Any idea?

Thank you,
Somnath Das.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Not exactly. IllegalStateException is generated when we redirect after the response has been committed.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Thanks for our support..., I removed the flush .., but still facing the same problem


Really ? You removed everything except the db stuff and the redirect ?
Did you recompile ? (try to restart the server just in case)
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

There is an entry in our Servlets FAQ that covers IllegalStateException and some of the most common causes.
You can see it here.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help needed, in Servelet