Mixing HTML and JSP

R

r

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
 
P

praveen

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

r

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

r

one addition: code which starts with "out.println...." does not break,
everything is in one line.
 
P

PeanutPete

Try this.

<%
if( theWords != null && theWords.equals("") == false )
{
out.println ("<meta name=\"x-mpwe-Sitesearch\" content=\" & theWords
&\">");
}
%>
 
S

steen

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:eek:ut
value="${param.theWords}" />">
</c:if>

(Here I assume that you have a request parameter called theWords)
 
P

praveen

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

r

@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
 
R

Roedy Green

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
 
R

Roedy Green

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.
 
B

Bruce Lewis

steen said:
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:eek:ut
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

r

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"
 
B

Bruce Lewis

Roedy Green said:
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.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top