Link to home
Start Free TrialLog in
Avatar of PhilAI
PhilAIFlag for United Kingdom of Great Britain and Northern Ireland

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.getParameter("cust_firstname") + " " + request.getParameter("cust_surname")%></em><br>
Telephone Number = <em><%=request.getParameter("cust_phone_intl") + " " +
                          request.getParameter("cust_phone_area") + " " +
                          request.getParameter("cust_phone_number")%></em><br>
Email Address = <em><%=request.getParameter("cust_email")%></em><br>
Query = <em><%=request.getParameter("trans_objective")%></em><br>
Callback Time = <em><%=DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.SHORT).format( ((Calendar)session.getAttribute("cust_callback_datetime")).getTime() )%></em><br>
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

<%
    if( request.getParameter("cust_phone_intl") != null &&
        request.getParameter("cust_phone_area") != null &&
        request.getParameter("cust_phone_number") != null )
    {
%>
Customer Name = <em><%=request.getParameter("cust_firstname") + " " + request.getParameter("cust_surname")%></em><br>
Telephone Number = <em><%=request.getParameter("cust_phone_intl") + " " +
                          request.getParameter("cust_phone_area") + " " +
                          request.getParameter("cust_phone_number")%></em><br>
Email Address = <em><%=request.getParameter("cust_email")%></em><br>
Query = <em><%=request.getParameter("trans_objective")%></em><br>
Callback Time = <em><%=DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.SHORT).format( ((Calendar)session.getAttribute("cust_callback_datetime")).getTime() )%></em><br>
<%
    } // end of if statement
%>
Avatar of PhilAI

ASKER

Am I missing something?

It is printing all of that text on the page whether request.getParameter("cust_phone_intl") is null or not.
I even tried request.getParameter("cust_phone_intl") != ""
Avatar of PhilAI

ASKER

By the way, I only need to check the request.getParameter("cust_phone_intl") parameter, so changed it to read:

<%
    if( request.getParameter("cust_phone_intl") != null )
    {
%>
Customer Name = <em><%=request.getParameter("cust_firstname") + " " + request.getParameter("cust_surname")%></em><br>
Telephone Number = <em><%=request.getParameter("cust_phone_intl") + " " +
                          request.getParameter("cust_phone_area") + " " +
                          request.getParameter("cust_phone_number")%></em><br>
Email Address = <em><%=request.getParameter("cust_email")%></em><br>
Query = <em><%=request.getParameter("trans_objective")%></em><br>
Callback Time = <em><%=DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.SHORT).format( ((Calendar)session.getAttribute("cust_callback_datetime")).getTime() )%></em><br>
<%
    } // end of if statement
%>
cool :-)  That should work :-)
Avatar of PhilAI

ASKER

It doesn't, it stills outputs all the text to the page...
Avatar of PhilAI

ASKER

<%@ page language="java" %>
<%@ page import="java.util.*" %>
<%@ page errorPage="InteractError.jsp" %>
<%@ page import="java.text.DateFormat" %>
<%@ 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/styles.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(begin, 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.setCharacterEncoding("utf-8");
%>

<body background="/Brands/PC/images/motortop.gif" leftmargin="0" topmargin="100" marginwidth="25" marginheight="0">

<h2><center>Your Transaction Number is <%=(String)session.getAttribute("trans_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.getParameter("cust_firstname") + " " + request.getParameter("cust_surname")%></em><br>
<%
    if (request.getParameter("cust_phone_intl") != "")
    {
%>
Telephone Number = <em><%=request.getParameter("cust_phone_intl") + "! " +
                          request.getParameter("cust_phone_area") + " " +
                          request.getParameter("cust_phone_number")%></em><br>
<%
    }
%>
Email Address = <em><%=request.getParameter("cust_email")%></em><br>
Query = <em><%=request.getParameter("trans_objective")%></em><br>
<!--
Callback Time = <em><%=DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.SHORT).format( ((Calendar)session.getAttribute("cust_callback_datetime")).getTime() )%></em><br>
-->

</b></ul>

<table align="center">
  <tr>
    <td><a href="javascript:window.close();">Close Window</a></td>
  </tr>
</table>

</body>
</html>
why have you put

    if (request.getParameter("cust_phone_intl") != "")

instead of

    if (request.getParameter("cust_phone_intl") != null)

?
Avatar of PhilAI

ASKER

Null was not working, so I thought I'd try ""
ASKER CERTIFIED SOLUTION
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
oh, and just so you know, you can't compare strings like

    if (request.getParameter("cust_phone_intl") != "")

that will always return true as != will compare the String objects, not their contents...

The better way of doing that is:

    if ( !request.getParameter("cust_phone_intl").equals( "" ) )

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.getParameter("cust_firstname") + " " + request.getParameter("cust_surname")%></em><br>
<%
    String intl = request.getParameter("cust_phone_intl") ;
    if( intl != null )
        intl = intl.trim() ; // get rid of any spaces
    if( intl != null && intl.length() > 0 )
    {
%>
Telephone Number = <em><%=request.getParameter("cust_phone_intl") + "! " +
                          request.getParameter("cust_phone_area") + " " +
                          request.getParameter("cust_phone_number")%></em><br>
<%
    }
%>
Email Address = <em><%=request.getParameter("cust_email")%></em><br>
Query = <em><%=request.getParameter("trans_objective")%></em><br>

Good luck with it!!

Tim