getParameter causing problem with response.sendRedirect
Hi all,
Have a problem with the response.sendRedirect and the evaluation of getParameter
Got a form with several fields which gets sent to a servlet inside the form I also have a field:
<input type="hidden" name="sendme" value="yes">
The form get submitted and I get the value from the sendme field via
String send = (String) request.getParameter("sendme");
int ans = 0;
If (send != null){
ans=1;
}
if (ans ==1){
String url = http://www.google.com;
response.sendRedirect(url);
}
This will not redirect to the external url, however, if I change int ans = 1; then the page get redirected. I have inserted out.print() statements and verified that I am actually getting into the if (ans==1) block which is an affirmative.
I cannot for the life of me figure out this freaky behaviour. Does anyone have any suggestions or another way of doing the automatic redirect. It would not be a problem if I just have to redirect to a local jsp page or servlet then I would use the RequestDispatcher.forward() but I have to access an outside url.
TIA,
xnyl
[1159 byte] By [
xnyla] at [2007-11-26 21:55:10]

# 1
Well if the redirect works when you set the condition to "ans=1" then the redirect works.
When you set the condition to "ans=1" it is doing an assignment and not a compare. An assignment will always eveluate to true which is why the redirect statement gets called.
This lets me to believe that the ans variable is not being set to the value '1' whih means that the parameter sendme is not being sent and the request.getParameter("sendme") is returning null.
A simple check is t o put a system out to validate this:
String send = (String) request.getParameter("sendme");
System.out.println("send = " + send);
# 2
It is to test if the redirect works that I set the ans=1 so yes it should be true and I get into the block.
I have previously had a print statement inside the if (send != null) block and also in the (ans == 1) block where the redirect is, just to output so we can verify that we are actually getting into the block but the redirect does not redirect at that point.
So to summarize: The redirect works if we set the code block to be true manually (if it is set before send=! null) but if anything (send != null is true and we set ans =1) is evaluated in the process before the redirect code then the redirect gets broken.
Regards,
xnyl
xnyla at 2007-7-10 3:50:47 >

# 3
send is never null, the method getParameter("sendme") returns an empty String if your hidden input has no value.
String send = request.getParameter("sendme");
if (send != null && !"".equals(send)) {
response.sendRedirect("http://www.google.com");
}
# 4
I agree with you regarding the pointer there, but still even though I am able to get into the code block with the redirect the sendRedirect doesn抰 function and it seems to be the problem, the sendRedirect get stumped by something in the evaluation of the if statements which causes it to cave.
How do you do redirect between urls when the sendRedirect doesn抰 function properly? I am just trying to find a solution how to get to an external url in my jsp.
Regards,
xnyl
xnyla at 2007-7-10 3:50:47 >

# 5
Are you doing something after your if statement that contains the response.sendRedirect(...)?
If you are, it should be in an else block
if (i == 1) {
response.sendRedirect(...);
} else {
//output, forward, include, or redirect here
}