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

前端 未结 8 787
温柔的废话
温柔的废话 2020-12-01 10:25

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

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

This do

相关标签:
8条回答
  • 2020-12-01 10:44
    <%!
    private void myFunc(String Bits, javax.servlet.jsp.JspWriter myOut)
    {  
      try{ myOut.println("<div>"+Bits+"</div>"); } 
      catch(Exception eek) { }
    }
    %>
    ...
    <%
      myFunc("more difficult than it should be",out);
    %>
    

    Try this, it worked for me!

    0 讨论(0)
  • 2020-12-01 10:49

    I suppose this would help:

    <%! 
       String someOutput() {
         return "Some Output";
      }
    %>
    ...
    <%= someOutput() %>
    

    Anyway, it isn't a good idea to have code in a view.

    0 讨论(0)
  • 2020-12-01 10:50

    You can do something like this:

    <%
    
    out.print("<p>Hey!</p>");    
    out.print("<p>How are you?</p>");
    
    %>
    
    0 讨论(0)
  • 2020-12-01 10:51

    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.

    0 讨论(0)
  • 2020-12-01 10:55

    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";
        }
    }
    
    0 讨论(0)
  • 2020-12-01 10:56

    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){
    
           }
         }
    
    %>
    
    0 讨论(0)
提交回复
热议问题