[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 221
  • Last Modified:

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>
0
PhilAI
Asked:
PhilAI
  • 6
  • 5
1 Solution
 
TimYatesCommented:
<%
    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
%>
0
 
PhilAIAuthor Commented:
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") != ""
0
 
PhilAIAuthor Commented:
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
%>
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
TimYatesCommented:
cool :-)  That should work :-)
0
 
PhilAIAuthor Commented:
It doesn't, it stills outputs all the text to the page...
0
 
PhilAIAuthor Commented:
<%@ 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>
0
 
TimYatesCommented:
why have you put

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

instead of

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

?
0
 
PhilAIAuthor Commented:
Null was not working, so I thought I'd try ""
0
 
TimYatesCommented:
Strange...  if no value is being sent through to this page then null should work...

Try:

    if( request.getParameter("cust_phone_intl") != null && request.getParameter("cust_phone_intl").length() > 0 )
0
 
TimYatesCommented:
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...
0
 
TimYatesCommented:
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
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now