How to output HTML from JSP <%! … %> block?

2019-01-14 05:33发布

I just started learning JSP technology, and came across a wall.

How do you output HTML from a method in <%! ... %> JSP declaration block?

This doesn't work:

<%! 
void someOutput() {
    out.println("Some Output");
}
%>
...
<% someOutput(); %>

Server says there's no “out”.

U: I do know how to rewrite code with this method returning a string, but is there a way to do this inside <%! void () { } %> ? Though it may be non-optimal, it's still interesting.

标签: jsp
8条回答
该账号已被封号
2楼-- · 2019-01-14 06:03

You can do something like this:

<%

out.print("<p>Hey!</p>");    
out.print("<p>How are you?</p>");

%>
查看更多
放我归山
3楼-- · 2019-01-14 06:07

All you need to do is pass the JspWriter object into your method as a parameter i.e.

void someOutput(JspWriter stream)

Then call it via:

<% someOutput(out) %>

The writer object is a local variable inside _jspService so you need to pass it into your utility method. The same would apply for all the other built in references (e.g. request, response, session).

A great way to see whats going on is to use Tomcat as your server and drill down into the 'work' directory for the '.java' file generated from your 'jsp' page. Alternatively in weblogic you can use the 'weblogic.jspc' page compiler to view the Java that will be generated when the page is requested.

查看更多
Viruses.
4楼-- · 2019-01-14 06:07

A simple alternative would be the following:

<%!
    String myVariable = "Test";
    pageContext.setAttribute("myVariable", myVariable);
%>

<c:out value="myVariable"/>
<h1>${myVariable}</h1>

The you could simply use the variable in any way within the jsp code

查看更多
仙女界的扛把子
5楼-- · 2019-01-14 06:08

You can't use the 'out' variable (nor any of the other "predeclared" scriptlet variables) inside directives.

The JSP page gets translated by your webserver into a Java servlet. Inside tomcats, for instance, everything inside scriptlets (which start "<%"), along with all the static HTML, gets translated into one giant Java method which writes your page, line by line, to a JspWriter instance called "out". This is why you can use the "out" parameter directly in scriptlets. Directives, on the other hand (which start with "<%!") get translated as separate Java methods.

As an example, a very simple page (let's call it foo.jsp):

<html>
    <head/>
    <body>
        <%!
            String someOutput() {
                return "Some output";
            }
        %>
        <% someOutput(); %>
    </body>
</html>

would end up looking something like this (with a lot of the detail ignored for clarity):

public final class foo_jsp
{
    // This is where the request comes in
    public void _jspService(HttpServletRequest request, HttpServletResponse response) 
        throws IOException, ServletException
    {
        // JspWriter instance is gotten from a factory
        // This is why you can use 'out' directly in scriptlets
        JspWriter out = ...; 

        // Snip

        out.write("<html>");
        out.write("<head/>");
        out.write("<body>");
        out.write(someOutput()); // i.e. write the results of the method call
        out.write("</body>");
        out.write("</html>");
    }

    // Directive gets translated as separate method - note
    // there is no 'out' variable declared in scope
    private String someOutput()
    {
        return "Some output";
    }
}
查看更多
6楼-- · 2019-01-14 06:08

too late to answer it but this help others

<%! 
     public void printChild(Categories cat, HttpServletResponse res ){
       try{
          if(cat.getCategoriesSet().size() >0){
              res.getWriter().write("") ; 
          }
       }catch(Exception exp){

       }
     }

%>
查看更多
放我归山
7楼-- · 2019-01-14 06:11

You can do something like this:

<%!
String myMethod(String input) {
    return "test " + input;
}
%>

<%= myMethod("1 2 3") %>

This will output test 1 2 3 to the page.

查看更多
登录 后发表回答