• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

java.lang.IllegalStateException: Cannot call sendRedirect()

 
Ranch Hand
Posts: 99
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

I was reading several forums about this error, but still dont get it and i still cant fix the issue.

This is my web.xml


So when i access for the first time, a login page will be open.. I log in properly,
This is my url when I run my project: http://localhost:8080/Company/
When i log in properly my url change to: localhost:8080/Company/DBservlet <-- this is my welcome.jsp

But if someone copy this url(localhost:8080/Company/DBservlet) recieve the following:
org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [DBservlet] in context with path [/Company] threw exception
java.lang.IllegalStateException: Cannot forward after response has been committed

If they put this url localhost:8080/Company/DBservlet need to redirect to login.jsp or this url http://localhost:8080/Company/, so they can log in properly.

This is my servlet code:


Any opinion is welcome.


Thanks & Regards,
-M
 
Mauro Trevigno
Ranch Hand
Posts: 99
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, the only way that works was the following:


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

RequestDispatcher requestDispatcher;
requestDispatcher = request.getRequestDispatcher("/login.jsp");
requestDispatcher.forward(request, response);

}


@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
// doPost(request, response); // I removed the doPost.
}



Do you know if there is a better approach for this?

Thanks guys.
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You did not post all of the servlet code. Does it forward to a JSP?
 
Mauro Trevigno
Ranch Hand
Posts: 99
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:You did not post all of the servlet code. Does it forward to a JSP?



Yes Bear,



Thanks,
-M
 
Bear Bibeault
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see no sendRedirect() call in that servlet.
 
Mauro Trevigno
Ranch Hand
Posts: 99
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the complete Servlet file.



So when i access for the fist time i go to the url localhost:8080/Company
When i log succesfull my url change to localhost:8080/DBservlet

But if someone try to copy and paste directly localhost:8080/DBservlet recieve the error.
Thats why i was adding the response.sendRedirect in the process Request.

Thanks,
-M
 
reply
    Bookmark Topic Watch Topic
  • New Topic