-1

I am trying to include HTML inside a java scriptlet method. A pseudocode example:

<%! public void doSomething() {
        %>
        <p>Hello World</p>
        <%   
} %>

The above example doesn't work because the compiler thinks that the method ends right after it's declared.

Is there an alternate way?

theK_S
  • 45
  • 2
  • 12
  • Have a look at out.println(...) – El Guapo Aug 19 '14 at 14:31
  • The above is just an example; I know I could just do something like out.println(..), but I'm trying to learn how I would add html to my scriptlet method – theK_S Aug 19 '14 at 14:33
  • 2
    Have a look at out.println(...). But you really don't want to do this anyway, regardless of how you think you want to. – Dave Newton Aug 19 '14 at 14:38
  • That is the ONLY way you would add HTML to your scriptlet method, but echoing @DaveNewton that's a BAD IDEA – El Guapo Aug 19 '14 at 14:40

1 Answers1

3

First, you really shouldn't be using scriptlets and especially shouldn't be defining methods in scriptlets.

Nevertheless, you can print HTML with out.print() too, and even in a method defined in a scriptlet. You will run into the issue that in your declaration block you don't have access to out unless you declare a global JspWriter and set it to out lower down in your scriptlet, like below:

<%! 
JspWriter jout = null;
public void doSomething() 
{
   jout.print("<p>Hello World</p>");
}
%>
<%
jout = out;
doSomething();
%>
developerwjk
  • 8,619
  • 2
  • 17
  • 33