response.sendRedirect

<%@ page contentType="text/html; charset=iso-8859-1" language="java" %>

<%

String url = request.getParameter("urlid");

//out.println(url);

//url = "addc";

if(url=="addc")

{

response.sendRedirect("../client_reg/index.jsp");

}

if(url=="addp")

{

response.sendRedirect("../project_reg/index.jsp");

}

%>

I am sending a some string values to this url String.and based on that value i need to redirect them to two different pages.

if i assign a value manually like this line,

url ="addc";

Script is working fine.

but if the value is coming from the JS side its not redirecting.

I checked whether the values coming to this side by using this line.

out.println(url);

that also working. Its printing thisaddcand addpvalues.

so where is the problem.

[1339 byte] By [Ajaxranda] at [2007-11-27 0:27:17]
# 1

hi,

you have a little tricky problem and you may face this at time in web programming and else where. when you print "out.println(url);" you get the correct output right? All you have to do is use the trim() method for example:

String url = request.getParameter("urlid");

url = url.trim();

now try comparing the url with the string you have ("addc" or "addp"). I will work out. The trim method removes the trailing white spaces...

the root cause is "you might have whitespaces added up in your JS code"..

Regards,

-- Abdel Olakara

http://olakara.googlepages.com

Olakaraa at 2007-7-11 22:26:53 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Also use url.equalsIgnoreCase method instead of ==If you post the form js will not hold value. In this case you need hidden field and populate with getter setters then get it back in jsp.
skp71a at 2007-7-11 22:26:53 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Indeed, when you use the == operator to compare two objects, this will only return true when they are both of the same reference. It does not check if they have the same value. Rather use [url=http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Object.html#equals(java.lang.Object)]Object#equals()[/url], which is also implemented by [url=http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#equals(java.lang.Object)]String[/url]. If you want to do it case insensitive, then use [url=http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#equalsIgnoreCase(java.lang.String)]String#equalsIgnoreCase()[/url].

BalusCa at 2007-7-11 22:26:53 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

try this code

<%@ page contentType="text/html;

charset=iso-8859-1" language="java" %>

<%

String url = request.getParameter("urlid");

//out.println(url);

//url = "addc";

if(url.equals("addc"))

{

response.sendRedirect("../client_reg/index.jsp");

}

else if(url.equals("addp"))

{

response.sendRedirect("../project_reg/index.jsp");

}

%>

Message was edited by:

Eng.Mohammed

Eng.Mohammeda at 2007-7-11 22:26:53 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
Thanks for all the replies.I made my script based on your suggestions.Thanks again it works.
Ajaxranda at 2007-7-11 22:26:53 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...