PhilAI
asked on
IF statement, filling in the blanks
I have the following section of code, but I only want to display the Telephone Number when there is a telephone number.
This is a JSP page, how would I go about doing this? I know how to do it in ASP, but not JSP. I was basically looking for an IF statement, if telephone number exists, then output the following code...
Customer Name = <em><%=request.getParamete r("cust_fi rstname") + " " + request.getParameter("cust _surname") %></em><br >
Telephone Number = <em><%=request.getParamete r("cust_ph one_intl") + " " +
request.getParameter("cust _phone_are a") + " " +
request.getParameter("cust _phone_num ber")%></e m><br>
Email Address = <em><%=request.getParamete r("cust_em ail")%></e m><br>
Query = <em><%=request.getParamete r("trans_o bjective") %></em><br >
Callback Time = <em><%=DateFormat.getDateT imeInstanc e(DateForm at.MEDIUM, DateFormat .SHORT).fo rmat( ((Calendar)session.getAttr ibute("cus t_callback _datetime" )).getTime () )%></em><br>
This is a JSP page, how would I go about doing this? I know how to do it in ASP, but not JSP. I was basically looking for an IF statement, if telephone number exists, then output the following code...
Customer Name = <em><%=request.getParamete
Telephone Number = <em><%=request.getParamete
request.getParameter("cust
request.getParameter("cust
Email Address = <em><%=request.getParamete
Query = <em><%=request.getParamete
Callback Time = <em><%=DateFormat.getDateT
ASKER
Am I missing something?
It is printing all of that text on the page whether request.getParameter("cust _phone_int l") is null or not.
I even tried request.getParameter("cust _phone_int l") != ""
It is printing all of that text on the page whether request.getParameter("cust
I even tried request.getParameter("cust
ASKER
By the way, I only need to check the request.getParameter("cust _phone_int l") parameter, so changed it to read:
<%
if( request.getParameter("cust _phone_int l") != null )
{
%>
Customer Name = <em><%=request.getParamete r("cust_fi rstname") + " " + request.getParameter("cust _surname") %></em><br >
Telephone Number = <em><%=request.getParamete r("cust_ph one_intl") + " " +
request.getParameter("cust _phone_are a") + " " +
request.getParameter("cust _phone_num ber")%></e m><br>
Email Address = <em><%=request.getParamete r("cust_em ail")%></e m><br>
Query = <em><%=request.getParamete r("trans_o bjective") %></em><br >
Callback Time = <em><%=DateFormat.getDateT imeInstanc e(DateForm at.MEDIUM, DateFormat .SHORT).fo rmat( ((Calendar)session.getAttr ibute("cus t_callback _datetime" )).getTime () )%></em><br>
<%
} // end of if statement
%>
<%
if( request.getParameter("cust
{
%>
Customer Name = <em><%=request.getParamete
Telephone Number = <em><%=request.getParamete
request.getParameter("cust
request.getParameter("cust
Email Address = <em><%=request.getParamete
Query = <em><%=request.getParamete
Callback Time = <em><%=DateFormat.getDateT
<%
} // end of if statement
%>
cool :-) That should work :-)
ASKER
It doesn't, it stills outputs all the text to the page...
ASKER
<%@ page language="java" %>
<%@ page import="java.util.*" %>
<%@ page errorPage="InteractError.j sp" %>
<%@ page import="java.text.DateForm at" %>
<%@ page contentType="text/html; charset=ISO-8859-1" %>
<%@ page buffer="8kb" %>
<%@ page info="InteractConfirm" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/1998/REC-html40-19980424/loose.dtd">
<html>
<head>
<title>Interaction</title>
<link href="/Brands/PC/css/style s.css" rel="stylesheet" type="text/css">
<meta http-equiv="Content-Type" content="text/html;charset =iso-8859- 1">
<script language = "JavaScript">
<!--
// Use this function to create a cookie
function setCookie(name, value, expires)
{
document.cookie = name + "=" + escape(value) + ";path=/" +
((expires == null) ? "" : ";expires=" + expires.toGMTString());
}
// Use this function to retrieve a cookie
function getCookie(name)
{
var cname = name + "=";
var dc = document.cookie;
if (dc.length > 0)
{
var begin = dc.indexOf(cname);
if (begin != -1)
{
begin += cname.length;
var end = dc.indexOf(";", begin);
if (end == -1)
end = dc.length;
return unescape(dc.substring(begi n, end));
}
}
return null;
}
function isCookieEnable()
{
// check which browser is used (netscripe or IE)
var myBrowser = navigator.appName.substr(0 ,3);
if (myBrowser == "Net")
{
// This is a Netscape browser
var cookieName = "_SWCP_CookieCheck";
var cookieValue = "checkCookie";
setCookie(cookieName, cookieValue);
// Check to see if able to create cookie
var myCookie = getCookie(cookieName);
if (myCookie == null)
return false;
else
return true;
}
else
{
// This is a IE
if (navigator.cookieEnabled)
return true;
else
return false;
}
}
function custLogOn()
{
if (isCookieEnable())
{
// Create a cookie name _SWRS_StartPage and expired in an hour.
var exp = new Date();
exp.setTime(exp.getTime() + (60*60*1000));
var cookieName = "_SWCP_StartPage";
var cookieValue = window.location.href;
setCookie(cookieName, cookieValue, exp);
window.location = "financial/login.html";
}
else
{
// Cookies not enabled on browser
alert("This page requires browser cookies to be enabled.\n Please enable browser cookies and come back again!");
}
}
// -->
</script>
</head>
<%
//need to set the encoding of the incoming request to utf-8 because
//JRUN defaults to Latin-1 even though the client side web
//page might be encoded in utf-8
request.setCharacterEncodi ng("utf-8" );
%>
<body background="/Brands/PC/ima ges/motort op.gif" leftmargin="0" topmargin="100" marginwidth="25" marginheight="0">
<h2><center>Your Transaction Number is <%=(String)session.getAttr ibute("tra ns_id")%>< /center></ h2>
<hr>
<p><b><I>Note</I> : For your own records here is the information you have just sent to us.<br>
<ul>
Customer Name = <em><%=request.getParamete r("cust_fi rstname") + " " + request.getParameter("cust _surname") %></em><br >
<%
if (request.getParameter("cus t_phone_in tl") != "")
{
%>
Telephone Number = <em><%=request.getParamete r("cust_ph one_intl") + "! " +
request.getParameter("cust _phone_are a") + " " +
request.getParameter("cust _phone_num ber")%></e m><br>
<%
}
%>
Email Address = <em><%=request.getParamete r("cust_em ail")%></e m><br>
Query = <em><%=request.getParamete r("trans_o bjective") %></em><br >
<!--
Callback Time = <em><%=DateFormat.getDateT imeInstanc e(DateForm at.MEDIUM, DateFormat .SHORT).fo rmat( ((Calendar)session.getAttr ibute("cus t_callback _datetime" )).getTime () )%></em><br>
-->
</b></ul>
<table align="center">
<tr>
<td><a href="javascript:window.cl ose();">Cl ose Window</a></td>
</tr>
</table>
</body>
</html>
<%@ page import="java.util.*" %>
<%@ page errorPage="InteractError.j
<%@ page import="java.text.DateForm
<%@ page contentType="text/html; charset=ISO-8859-1" %>
<%@ page buffer="8kb" %>
<%@ page info="InteractConfirm" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/1998/REC-html40-19980424/loose.dtd">
<html>
<head>
<title>Interaction</title>
<link href="/Brands/PC/css/style
<meta http-equiv="Content-Type" content="text/html;charset
<script language = "JavaScript">
<!--
// Use this function to create a cookie
function setCookie(name, value, expires)
{
document.cookie = name + "=" + escape(value) + ";path=/" +
((expires == null) ? "" : ";expires=" + expires.toGMTString());
}
// Use this function to retrieve a cookie
function getCookie(name)
{
var cname = name + "=";
var dc = document.cookie;
if (dc.length > 0)
{
var begin = dc.indexOf(cname);
if (begin != -1)
{
begin += cname.length;
var end = dc.indexOf(";", begin);
if (end == -1)
end = dc.length;
return unescape(dc.substring(begi
}
}
return null;
}
function isCookieEnable()
{
// check which browser is used (netscripe or IE)
var myBrowser = navigator.appName.substr(0
if (myBrowser == "Net")
{
// This is a Netscape browser
var cookieName = "_SWCP_CookieCheck";
var cookieValue = "checkCookie";
setCookie(cookieName, cookieValue);
// Check to see if able to create cookie
var myCookie = getCookie(cookieName);
if (myCookie == null)
return false;
else
return true;
}
else
{
// This is a IE
if (navigator.cookieEnabled)
return true;
else
return false;
}
}
function custLogOn()
{
if (isCookieEnable())
{
// Create a cookie name _SWRS_StartPage and expired in an hour.
var exp = new Date();
exp.setTime(exp.getTime() + (60*60*1000));
var cookieName = "_SWCP_StartPage";
var cookieValue = window.location.href;
setCookie(cookieName, cookieValue, exp);
window.location = "financial/login.html";
}
else
{
// Cookies not enabled on browser
alert("This page requires browser cookies to be enabled.\n Please enable browser cookies and come back again!");
}
}
// -->
</script>
</head>
<%
//need to set the encoding of the incoming request to utf-8 because
//JRUN defaults to Latin-1 even though the client side web
//page might be encoded in utf-8
request.setCharacterEncodi
%>
<body background="/Brands/PC/ima
<h2><center>Your Transaction Number is <%=(String)session.getAttr
<hr>
<p><b><I>Note</I> : For your own records here is the information you have just sent to us.<br>
<ul>
Customer Name = <em><%=request.getParamete
<%
if (request.getParameter("cus
{
%>
Telephone Number = <em><%=request.getParamete
request.getParameter("cust
request.getParameter("cust
<%
}
%>
Email Address = <em><%=request.getParamete
Query = <em><%=request.getParamete
<!--
Callback Time = <em><%=DateFormat.getDateT
-->
</b></ul>
<table align="center">
<tr>
<td><a href="javascript:window.cl
</tr>
</table>
</body>
</html>
why have you put
if (request.getParameter("cus t_phone_in tl") != "")
instead of
if (request.getParameter("cus t_phone_in tl") != null)
?
if (request.getParameter("cus
instead of
if (request.getParameter("cus
?
ASKER
Null was not working, so I thought I'd try ""
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
oh, and just so you know, you can't compare strings like
if (request.getParameter("cus t_phone_in tl") != "")
that will always return true as != will compare the String objects, not their contents...
The better way of doing that is:
if ( !request.getParameter("cus t_phone_in tl").equal s( "" ) )
Buf of course, if it is null, that will throw an exception...
if (request.getParameter("cus
that will always return true as != will compare the String objects, not their contents...
The better way of doing that is:
if ( !request.getParameter("cus
Buf of course, if it is null, that will throw an exception...
Cool :-)
Glad I could help :-)
To make it look a bit neater, you can use a String variable to hold the value...
Customer Name = <em><%=request.getParamete r("cust_fi rstname") + " " + request.getParameter("cust _surname") %></em><br >
<%
String intl = request.getParameter("cust _phone_int l") ;
if( intl != null )
intl = intl.trim() ; // get rid of any spaces
if( intl != null && intl.length() > 0 )
{
%>
Telephone Number = <em><%=request.getParamete r("cust_ph one_intl") + "! " +
request.getParameter("cust _phone_are a") + " " +
request.getParameter("cust _phone_num ber")%></e m><br>
<%
}
%>
Email Address = <em><%=request.getParamete r("cust_em ail")%></e m><br>
Query = <em><%=request.getParamete r("trans_o bjective") %></em><br >
Good luck with it!!
Tim
Glad I could help :-)
To make it look a bit neater, you can use a String variable to hold the value...
Customer Name = <em><%=request.getParamete
<%
String intl = request.getParameter("cust
if( intl != null )
intl = intl.trim() ; // get rid of any spaces
if( intl != null && intl.length() > 0 )
{
%>
Telephone Number = <em><%=request.getParamete
request.getParameter("cust
request.getParameter("cust
<%
}
%>
Email Address = <em><%=request.getParamete
Query = <em><%=request.getParamete
Good luck with it!!
Tim
if( request.getParameter("cust
request.getParameter("cust
request.getParameter("cust
{
%>
Customer Name = <em><%=request.getParamete
Telephone Number = <em><%=request.getParamete
request.getParameter("cust
request.getParameter("cust
Email Address = <em><%=request.getParamete
Query = <em><%=request.getParamete
Callback Time = <em><%=DateFormat.getDateT
<%
} // end of if statement
%>