• 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>

More from my site

  • Java: How To Select Top N Objects From A ListJava: How To Select Top N Objects From A List
  • Java: How To Create A Simple Web Server Using HttpServerJava: How To Create A Simple Web Server Using HttpServer
  • Java: Format Integer Into Fixed Width StringJava: Format Integer Into Fixed Width String
  • Java: Continuously Read Data From FileChannel Without MappedByteBufferJava: Continuously Read Data From FileChannel Without MappedByteBuffer
  • Java: Loading Large Data into JTable or JListJava: Loading Large Data into JTable or JList
  • Java: Format Long Integer Into Hexadecimal StringJava: Format Long Integer Into Hexadecimal String

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 Reply Cancel reply

Advertisement

  • Facebook
  • Google+
  • Instagram
  • Twitter

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

Adsense advertisement advertising apache blog blogging tips C# EGPC error estimation format format Integer Gmail Google Google Adsense Google Chrome Google Search Engine Google search result how to HTTP internet marketing Java JavaScript Linux money password performance PHP programming search engine optimization secure security short URL SQL static constructor String tiny URL Tips and Tricks twitter video Windows Vista Wordpress wordpress plugin wordpress theme Youtube

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