aspose file tools*
The moose likes JSP and the fly likes sendRedirect(url) problem in jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "sendRedirect(url) problem in jsp" Watch "sendRedirect(url) problem in jsp" New topic
Author

sendRedirect(url) problem in jsp

mike mimmis
Ranch Hand

Joined: Feb 11, 2011
Posts: 57

Hi friends,


I am new to JSP I am using response.sendRedirect() in my jsp when a condition becomes true or false it will go to success.jsp. While executing this I am getting "IllgalStateException and the response is not forwarding to "success.jsp". why this is occuring i need your help .

code snipet is:

<%
if(rs.next()==true)
{
response.sendRedirect("../success.jsp?work=add");
}
else
{
response.sendRedirect("../success.jsp?work=addFailed");
}
%>


I am getting java.lang.IllegalStateException .

Please help me to resolve this exception..
Chinna Eranna
Ranch Hand

Joined: Dec 08, 2009
Posts: 174

First.. use code tags

Second.. Search First

And Final, you can find your solution here


- Chinna
mike mimmis
Ranch Hand

Joined: Feb 11, 2011
Posts: 57

ya i tried both

1. return;

2. <% --------
Chinna Eranna
Ranch Hand

Joined: Dec 08, 2009
Posts: 174

What do you mean by I tried both ?
mike mimmis
Ranch Hand

Joined: Feb 11, 2011
Posts: 57

Chinna Eranna wrote:What do you mean by I tried both ?


i mean after writing response.sendRedirect("../success.jsp?work=add");

i add a line i.e return; I am getting same exception thats it.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60053
    
  65

Step 1 is to refactor your JSP to contain no Java code. The problems you are having are just one of the many reasons that this practice has been discredited for almost 10 years now. Time to update your knowledge.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1016
Have you already done output on this page? If so then sending a redirect will often result in an Illegal State Exception.

Any redirects should be done BEFORE outputting any text. That way you don't have to cancel the stuff you have written already.
mike mimmis
Ranch Hand

Joined: Feb 11, 2011
Posts: 57

Stefan Evans wrote:Have you already done output on this page? If so then sending a redirect will often result in an Illegal State Exception.

Any redirects should be done BEFORE outputting any text. That way you don't have to cancel the stuff you have written already.


hi stefen how it is achievable?
mike mimmis
Ranch Hand

Joined: Feb 11, 2011
Posts: 57

Bear Bibeault wrote:Step 1 is to refactor your JSP to contain no Java code. The problems you are having are just one of the many reasons that this practice has been discredited for almost 10 years now. Time to update your knowledge.



hi sir,

I am new to jsp. I knew a little about jsp so i cant understand what you are saying can you give me a example
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60053
    
  65

If you are new to JSP, it is important to start off on the right foot.

And that means NO JAVA CODE in the JSP. None. Nada. Zero.

That's a practice from last decade before JSP 2 and is now discredited.

Get yourself a modern JSP reference that concentrates on the JSTL (JSP Standard Tag Library) and the EL (Expression Language).

That vast majority of problems in JSP reported in this forum are due to people insisting on doing it wrong when doing it right is so much easier.
mike mimmis
Ranch Hand

Joined: Feb 11, 2011
Posts: 57

Bear Bibeault wrote:If you are new to JSP, it is important to start off on the right foot.

And that means NO JAVA CODE in the JSP. None. Nada. Zero.

That's a practice from last decade before JSP 2 and is now discredited.

Get yourself a modern JSP reference that concentrates on the JSTL (JSP Standard Tag Library) and the EL (Expression Language).

That vast majority of problems in JSP reported in this forum are due to people insisting on doing it wrong when doing it right is so much easier.



can you suggest me any book or blog to know more..
Chinna Eranna
Ranch Hand

Joined: Dec 08, 2009
Posts: 174

Head First Servlets And JSP is a good book.

Raghu Chary
Greenhorn

Joined: May 12, 2011
Posts: 9
thanks for book
mike mimmis
Ranch Hand

Joined: Feb 11, 2011
Posts: 57

thank you very much for sharing book name ....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sendRedirect(url) problem in jsp
 
Similar Threads
Exception creating bean of class sample.web.form.UserRegForm: {1}
need help in spring security process
Not able to navigate the user to another jsp in servlets using RequestDispatcher
Issues with HttpServletResponse.sendRedirect to JSP/JSF file
Display table rows using JSTL with different colors based on condition