How to create a function in a jsp page?

Hi,

Is it possible to create functions within a jsp page? I'm trying something like the following attached code, but no luck. How do we do this?

Thanks
<%!
  private void makeSomeUiStuff() {
      %> <p> Hello world! </p> <%
  }
%>

<%
  makeSomeUiStuff();
%>

Open in new window

DJ_AM_JuiceboxAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BitsqueezerCommented:
Hi,

sure, here's a thread which answers your question:

http://www.velocityreviews.com/forums/t140523-jsp-functions-methods-page-question.html

Cheers,

Christian
0
rrzCommented:
When a JSP is translated to java code(a servlet), code written in scriptlets is placed in the _jspService method. When we declare a method in a JSP it is translated to a method in the servlet. Implicit JSP objects are defined in the _jspService method.  Therefore if you want to print something from your method, then you must send "out"(a implicit object) as a parameter. All this is clear if you look at the translation file. In Tomcat, look in its work folder for that file. Here is an example.
<%@ page import="java.io.IOException" %>
<%!
  private void makeSomeUiStuff(JspWriter out) throws IOException{
       out.print("<html><body><p> Hello world! </p></body></html>"); 
  }
%>
<%
  makeSomeUiStuff(out);
%>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rrzCommented:
You could also do it this way.
<%@ page import="java.io.IOException" %>
<%!
  private String makeSomeUiStuff() throws IOException{
       return "<html><body><p> Hello world2! </p></body></html>"; 
  }
%>
<%
  out.print(makeSomeUiStuff());
%>

Open in new window

0
jocafiCommented:
In order to make your jsp more organized and cleaner, you should create your own java library and put your function there.

Do not forget to put your library under ${your_web_app}/WEB-INF/lib

See the example below how to import and use your class.
<%@page import="org.mylib.MyClass"%>

...
<%
  MyClass instance = new MyClass();
  // make one thing
  instance.makeSomeUiStuff(out);
  // make other thing
  out.print(instance.makeOtherUiStuff());
%>

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSP

From novice to tech pro — start learning today.