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
# 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].
# 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