• Post Reply Bookmark Topic Watch Topic
  • New Topic

java.lang.IllegalStateException: Cannot call sendRedirect()  RSS feed

 
Mauro Trevigno
Ranch Hand
Posts: 99
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • 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.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see no sendRedirect() call in that servlet.
 
Mauro Trevigno
Ranch Hand
Posts: 99
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!