Catharine,

You need to understand the difference between declarations: <%!
method-or-field-declaration %>, scriptlets <% java-code-fragment %> and
expressionts: <%= output-producing-expression %>.

Declarations (<%! %>) put new field declarations or method definitions into
the body of the Servlet class that is produced from the JSP page.

Scriptlets (<% %>) are Java code fragments that do not produce output (one
or more statements but not declarations of fields or methods--though local
variable declarations are OK). Scriptlets are injected unchanged into the
method that responds to incoming client requests and produces the response.

Expressions (<%= %>) must be Java expressions (not statements--no "if"
statements, no loops, no {braces}, no semicolons) and the value of the
expression is automatically converted to a String and added into the response.

So what looks to you like a single piece of JSP code is getting chopped up
and treated in different ways. In particular, parts of it are going into
the Servlet class body outside the response method and part is going inside
the response method.

That'll never work. You can define (<%! %>) a method that helps in
computation of the result you need to include in the response. Then you can
call that method from scriptlets and / or expressions, which by definition
are part of request processing.

Randall Schulz
Mountain View, CA USA


At 03:11 2002-10-11, you wrote:
>I've added a method to my JSP, which in a simplified form looks something
>like the following:
>
><%!
>     private void output(String text)
>     {
>          System.out.println(text);
>     }
>%>
>
>I want to change this so that it also displays 'text' on the html page, but
>I can't work out how this can be done from within the method.
>
>If I do the following:
><%!
>private void output(String text)
>{
>     System.out.println(text);
>%>
>     <p><%= text %></p>
><%
>}
>%>
>
>I get an error because it thinks the method is not terminated with a }.
>
>If I do the following:
><%!
>private void output(String text)
>{
>     System.out.println(text);
>%>
>     <p><%= text %></p>
><%!
>}
>%>
>
>it thinks that 'text' is no longer in scope.
>
>Can anyone help?
>Thanks,
>Catharine

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://archives.java.sun.com/jsp-interest.html
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.jsp
 http://www.jguru.com/faq/index.jsp
 http://www.jspinsider.com

Reply via email to