Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

jsp declaration bug?

  Asked By: Rainhard    Date: Jul 17    Category: Java    Views: 621
  

While this line works fine on expression:
<%= java.net.URLEncoder.encode("Hello World", "UTF-8") %>

It doesn't under declaration:
<%! String result = java.net.URLEncoder.encode("Hello World", "UTF-
8"); %>

You may try for yourselves.

Share: 

 

2 Answers Found

 
Answer #1    Answered By: Douglas Sullivan     Answered On: Jul 17

When you start the expression  with "<%=", you are
asking to print the result of the expression enclosed.
But "<%!" is used to declare member variables.
When you write a jsp, the web container converts it in
a java source class (.java) and then is compiled to a
.class .
All the "<%= expression %>" statements are replaced
with "out.print(expression)". But the "<%! expresion
%>" is copied directly in the body of the class, as
member variable for example.
Finally, if you use "<% expresion %>", the expression
is copied directly in the main method body, generally
called doService(HTTPRequest, HTTPResponse).
If you are using Tomcat, check out the
$CATALINA_HOME/work directory, here you can find the
generated classes, and look at the generated code.

 
Answer #2    Answered By: Cambria Lopez     Answered On: Jul 17

Thanks and I understand that. But my question is, why can't i simply
declare that line?
Try these 2 line  of codes:

<%! String result = java.net.URLEncoder.encode("Hello World", "UTF-
8"); %>
<%= result %>

It won't work, but directly print the value and it works  fine:

<%= java.net.URLEncoder.encode("Hello World", "UTF-8") %>

 
Didn't find what you were looking for? Find more on jsp declaration bug? Or get search suggestion and latest updates.




Tagged: