• Home
  • About

Snippet IT

IT News, Programming, Internet and Blogging

  • Programming and Scripting
  • Tips and Tricks
  • Software and Hardware
  • New and Happening
You are here: Home / Programming and Scripting / JSP: How to Declare Methods and Inner Class in JSP

JSP: How to Declare Methods and Inner Class in JSP

May 11, 2009 by Sze Hau Leave a Comment

JSP itself is just a plaint text and will be translated into Java source file (actually a servlet), compiled into class file and only interpreted at the end during runtime. All these happen at the backend.

When the whole JSP is translated into Java source file, all the HTML codes will be translated and source lines in <% %> tag will be included in a function of the translated servlet source file.

Therefore we can’t simply declare methods and classes in JSP, even within the <% %> clause because we can’t declare a function or class within a function.

To declare Java methods and classes in JSP, you need to write the declaration in the <%! %> clause so the the translator know which part of the code should be handled differently.

For example declaring a method in JSP:

<body>
<h2>Declare function in JSP</h2>
<%!
  int hello(JspWriter out, String name) {
    out.print("Hello");
  } 
%>
<%
  // Call the hello() function in JSP
  hello(out, " world");
%>
</body>

For example declaring a inner class in JSP:

<body>
<h2>Declare class in JSP</h2>
<%!
  class Hello {
    JspWriter out;
    public Hello(JspWriter out) {
      this.out = out;
    }

    public void greet(String name) {
        out.print("Hello " + name);
    }
  }
%>
<%
  // Use the Hello class in JSP
  Hello hello = new Hello(out);
  hello.greet("Christine");
%>
</body>

Filed Under: Programming and Scripting Tagged With: class, how to, Java, JSP, methods

About Sze Hau

Geek. Love programming. Coffee addicted. Married with two children. Working towards financial freedom.

Leave a ReplyCancel reply

Advertisement

Email News Letter

Sign up to receive updates daily and to hear what's going on with us

Software and Hardware

MD5 and SHA1 Checksum Using Windows

July 5, 2017 By Sze Hau Leave a Comment

Blog Network

  • Personal Fincance Personal Finance – Personal Money Tips, Stock Investment, Small Business and Make Money Online
  • szehau's weblog Life, Internet, Software, Gadgets, Programming and Investments

Snippet IT

This is the place where I want to share anything about information technology.

Search

Recent

  • MD5 and SHA1 Checksum Using Windows
  • MD5 and SHA1 Checksum Using Linux
  • Java: Unlimited Strength Jurisdiction Policy
  • WordPress: How To Change Admin Username
  • Linux: How To Compress And Decompress Folders And Files

Tags

advertisement blog C# Chrome DecimalFormat EGPC Float format format Integer free icons Gmail Google Google Adsense Google Chrome Google Search Engine Google search result how to HTTP Java JavaScript Linux money password performance PHP programming search engine optimization secure security short URL site value spam SQL static constructor String thread tiny URL Tips and Tricks trackback twitter video Wordpress wordpress plugin wordpress theme Youtube

Copyright © 2025 · Magazine Pro Theme on Genesis Framework · WordPress · Log in