Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2005

Tip: Looking for answers? Try searching our database.

Mixing HTML and JSP

Thread view: 
r@gnar.de - 25 Oct 2005 10:46 GMT
Hi

please help me to find the bug here somewhere in the quotes:

<body>
... bla bla ....

<%
if(theWords != "")
{
 out.println ("<meta name="x-mpwe-Sitesearch" content="  & theWords  &
">");

}
%>
</body>

As you can see I want to write out the println-line only if theWords
has got a value

regards
Ragnar
praveen - 25 Oct 2005 10:52 GMT
The correct form of the conditional statement should be:
<%
if (!theWords.equals(""))
{
 out.println ("<meta name="x-mpwe-Sitesearch" content=" + theWords +
">");
}
%>

!= will compare the references of the two string objects and not their
actual contents.

praveen
r@gnar.de - 25 Oct 2005 11:24 GMT
Thank you, Praveen, for fast reply

But I get this Generated servlet error:
/usr/share/jakarta-tomcat-5.0.28/work/Catalina/localhost/_/org/apache/jsp/de/functions/suche/default2_jsp.java:365:
')' expected
 out.println ("<meta name="x-mpwe-Sitesearch" content=" + theWords +
">");

Ragnar
r@gnar.de - 25 Oct 2005 11:27 GMT
one addition: code which starts with "out.println...." does not break,
everything is in one line.
praveen - 25 Oct 2005 12:35 GMT
Ragnar,
Sorry, I overlooked the code.
The error is occurring because of the use of the double-quotes inside
the string argument to out.println.
You need to use the escape character to prevent this.
The code should look like this:
<%
if (!theWords.equals(""))
{
 out.println ("<meta name=\"x-mpwe-Sitesearch\" content=" + theWords +
">");
}
%>
r@gnar.de - 25 Oct 2005 13:42 GMT
@praveen & peanutPete

I tried your code and it works fine if "theWords" has got a value
If not, I get this error:

cannot find symbol
if( theWords != null && theWords.equals("") == false )
    ^

Background: I get "theWords" as a request.parameter from the
search-form. My meta-tag code is used on every page (because it is part
of main template) but only one page (search.jsp) really needs it
Roedy Green - 25 Oct 2005 14:04 GMT
> if( theWords != null && theWords.equals("") == false )

Your first job it to get a String called theWords.

Then you can write code like this

if ( theWords != null && theWords.length() > 0 )

or

if ( theWords != null && theWords.equals( "elephant" ) );

or

if ( theWords != null && ! theWords.equals( "elephant" ) );

== false is the equivalent of Java babytalk.   It has that connotation
because at the JVM level == false requires loading a constant, and
doing a compare to get back to boolean which you had in the first
place.  Better do it with a single character ! , the not operator
which is one very fast JVM instruction, and one that can be easily
optimised totally out of existence,  than ramble on with 9 characters.
Hotspot might optimise the goofiness of == false away, but then again
in might not since it would only occur in newbie code.

The main reason to avoid == true or == false is not speed but social.
It labels you as a newbie. It is something a person who understood the
JVM would unlikely write.



See http://mindprod.com/jgloss/newbie.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Roedy Green - 25 Oct 2005 15:39 GMT
On Tue, 25 Oct 2005 13:04:10 GMT, Roedy Green
<my_email_is_posted_on_my_website@munged.invalid> wrote, quoted or
indirectly quoted someone who said :

>if ( theWords != null && theWords.equals( "elephant" ) );

you can shorten that to:

if ( "elephant".equals( theWords) )

This effectively does the null test for free.  The catch is, this code
tends to baffle novice programmers.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Bruce Lewis - 25 Oct 2005 19:16 GMT
> On Tue, 25 Oct 2005 13:04:10 GMT, Roedy Green
> <my_email_is_posted_on_my_website@munged.invalid> wrote, quoted or
[quoted text clipped - 8 lines]
> This effectively does the null test for free.  The catch is, this code
> tends to baffle novice programmers.

More likely it surprises programmers with experience in languages where
strings are not objects with methods.  For a truly novice programmer I
don't see why this should be more baffling than any other idiom.

Even for programmers surprised at this code, it's obvious what it does.
I think this style is a useful way to gain conciseness.
PeanutPete - 25 Oct 2005 11:28 GMT
Try this.

<%
if( theWords != null && theWords.equals("") == false )
{
 out.println ("<meta name=\"x-mpwe-Sitesearch\" content=\"  & theWords
&\">");
}
%>
steen - 25 Oct 2005 12:25 GMT
Actually you could look into using some standard tags for this, to
avoid cluttering up your jsp code with java-code. You could try :

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"%>
...
<c:if test="${!empty param.theWords}">
<meta name="x-mpwe-Sitesearch" content="<c:out
value="${param.theWords}" />">
</c:if>

(Here I assume that you have a request parameter called theWords)
Bruce Lewis - 25 Oct 2005 16:27 GMT
> Actually you could look into using some standard tags for this, to
> avoid cluttering up your jsp code with java-code. You could try :
[quoted text clipped - 5 lines]
> value="${param.theWords}" />">
> </c:if>

This is a great example for why angle brackets are a bad syntax for
server-side directives.  Especially the value= line that ends with three
server-side punctuation marks and two client-side punctuation marks.

With my own BRL this would be

[(brl-when (brl-nonblank? the-words)]
<meta name="x-mpwe-Sitesearch" content="[the-words]">
[)]

Notice how much easier it is to see what's client-side vs server-side,
even without syntax highlighting.

You might end up with more readable JSP code if you start with BRL then
translate:

<% if (theWords != null && !theWords.equals("")) { %>
<meta name="x-mpwe-Sitesearch" content="<%= theWords %>">
<% } %>
r@gnar.de - 25 Oct 2005 18:32 GMT
Thank you all for your great response! Now I got it working.

Very interesting to learn more about best practises and "advanced
JSP/java coding-style"
Roedy Green - 25 Oct 2005 12:55 GMT
>theWords != "")

see http://mindprod.com/jgloss/gotchas.html#STRINGCOMPARISON
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.