This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

response.sendRedirect not working  RSS feed

 
Mike London
Ranch Hand
Posts: 1441
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a relatively simple data entry JSP page that won't redirect back to another page. It has some CSS that calls JS to put the user in the first field of the JSP.

Then, there's a JSP (html) form with a submit button.

The form submit button comes back, via the form action, to the same JSP for processing (Testing a session variable), then calls a class to populate an object, calls Hibernate, .... all that works fine. At the end of the processing, I get a return value back in the JSP from a server class, and populate a session variable before trying to redirect to index.jsp.

The problem is that my response.sendRedirect("../index.jsp") doesn't "redirect", I'm trapped in the same JSP data entry form.....back in the user first name field.

I've tried turning off the CSS that does the txtFname.focus(), but that didn't make any difference.

I did put a return; statement, per Ben Souther's suggestion, after the call to sendRedirect(). No help with this problem.

I also tried, after searching the JR forums, using the 'encodedURL' method of the response object, but that didn't help either.

I've also tried using a full URL path in the response.sendRedirect(...) call with no difference.

-----

If I put the response.sendRedirect call up near the top of the JSP, it works fine, as expected.

Something in the combination of input fields and such is causing the problem, but it's quite difficult to figure out where the problem is.

There's clearly something going on I don't understand and I didn't see anything like this problem when I searched the JR forums.

Any suggestions?

Thanks.

Mike



 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you try to sendRedirect after one or more bufferloads of response get sent, it causes an exception. You cant redirect after the response headers have been sent.

Bill
 
Mike London
Ranch Hand
Posts: 1441
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bill,

I'm not sure what you're trying to say.

The issue appears to be in the CSS.

When I remove all the style info from the JSP, the response.sendRedirect works fine after the POST back.

I have no idea what's going on with the styles. They all are referenced in the JSP and all look correct.

Ideas?

Thanks.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66141
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It makes no sense to have a redirect mixed with content. So that fact that you are emitting anything, including CSS, in the same request as a redirect indicates a serious design flaw.
 
Mike London
Ranch Hand
Posts: 1441
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bear,

Maybe one of us is missing something.

It's like this....

You have an input page with text on it. That text is styled to look a certain way. When the JSP FIRST shows up, you see just the text and input fields.

You have a user who enters text and submits the form.

The form has logic to see if there's a session variable -- meaning that the user has submitted the form. If there is a session variable, then in that "if" part of the JSP, some additional processing occurs, then the form then redirects back to index.jsp.

Where's the design flaw?

How would you do this instead?

Thanks.

- Mike
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66141
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forms have no logic. They are HTML entities. So it's not clear to me what you mean when you say that "the form has logic".

If you are making decisions in a JSP on whether to show that JSP or not, that's a serious design flaw right there. Such decisions should be made before getting to the JSP.

As William pointed out, once content has started to be issued, the time for a redirect is long passed.
 
Mike London
Ranch Hand
Posts: 1441
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bear,

Thanks always for your reply. OK, I get your point.

But, haven't you ever created a simple JSP that posts back to itself as a shortcut for not having to create a mini-MVC architecture for one little JSP?

(I must admit though that I've never tried to do a POST back to the same JSP with CSS in the mix and a redirect.)

All that's happening in the JSP is after the user clicks SUBMIT, the form posts back to itself. Then, in the JSP, you test to see if there's a variable in the session. If it's there (if session.getAttribute(...) {}), you do some conditional logic that saves the values of the JSP).

After I save the JSP's form fields, I just want to redirect the user back to index.jsp (the "problem").

Without CSS in the mix, this shortcut approach works fine.

----

So, I guess what I'm hearing you say is to just bit the bullet and POST to a darn Servlet, do all the processing in the doPost(...) that's currently being done in the conditional part of the JSP (when the JSP posts back with data received from request.getParameter(....)). Then, from the Sevlet, do the re-direct back to the index.jsp, right?

From the user's point of view, there wouldn't be any difference.

From the program's point of view, I guess it would work with the CSS.

I got so incredibly frustrated since this POST back to the JSP works fine with no CSS and is cool for small JSPs with not that much logic. I'll re-factor to use a Servlet and do the redirect from there.

Thanks as always for your stern advice.

- Mike
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66141
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike London wrote:But, haven't you ever created a simple JSP that posts back to itself as a shortcut for not having to create a mini-MVC architecture for one little JSP?

No. Never. It's a recipe for disaster.

I got so incredibly frustrated since this POST back to the JSP works fine with no CSS and is cool for small JSPs

That's probably because your buffer isn't yet flushing and the addition of the CSS puts it over the line. Do you really want to bet the stability of your web app on such a roll of the dice?

Even for things that seem to be butt-simple, follow established patterns and procedures. You'll keep more of your hair that way (too late for mine).
 
Mike London
Ranch Hand
Posts: 1441
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're funny.

Thanks Bear....good points all....I'm firing up Eclipse now.

- Mike
 
Mike London
Ranch Hand
Posts: 1441
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And of course within 10 minutes, the Servlet redirect is working with no problems.

Thanks again Bear!

-- Mike
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!