JSP methods

In this example we are going to show you how we can declare a method and how we can used it.

JSP methods


In this example  we are going to show you how we can declare a method and how we can used it. In this example we are making a method named as addNum(int i, int b) which will take two numbers as its parameters and return integer value. We have declared this method inside a declaration directive i.e. <%! -----------%> this is a declaration tag. This tag is used mainly for declaration the variables and methods. In the method we are adding to numbers. To print the content of the method we are using scriptlet tag inside which we are going to use the out implicit object .<% ------- %> This tag is known as  Scriptlets . The main purpose of using this tag is to embed a java code in the jsp page. 

The code of the program is given below:



    <TITLE>Creating a Method</TITLE>
    <font  size="6" color ="#330099"> Method in Jsp </font><br>
    int addNum(int n, int m)
      return n + m;
    out.println("6 + 2 = " + addNum(6, 2));

Output of the program is given below:

Download this example.

Share on Google+Share on Google+

JSP methods

Posted on: February 6, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: JSP methods   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
Raja P
April 10, 2011
Ur Coding simple but i ve one doubt..

I dont know how execute the jsp program in tomcat 5.0 server..... when start to execute the jsp program i had some error.. Error is: HTTP Status 500 - type Exception report message description The server encountered an internal error () that prevented it from fulfilling this request. exception org.apache.jasper.JasperException: /SaveName.jsp(1,6) Invalid standard action org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:39) org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:407) org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:90) org.apache.jasper.compiler.Parser.parseStandardAction(Parser.java:1249) org.apache.jasper.compiler.Parser.parseElements(Parser.java:1559) org.apache.jasper.compiler.Parser.parse(Parser.java:126) org.apache.jasper.compiler.ParserController.doParse(ParserController.java:220) org.apache.jasper.compiler.ParserController.parse(ParserController.java:101) org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:203) org.apache.jasper.compiler.Compiler.compile(Compiler.java:470) org.apache.jasper.compiler.Compiler.compile(Compiler.java:451) org.apache.jasper.compiler.Compiler.compile(Compiler.java:439) org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:511) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:295) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236) javax.servlet.http.HttpServlet.service(HttpServlet.java:802) note :The full stack trace of the root cause is available in the Apache Tomcat/5.0.27 logs. how can solve this error help me to solve this error.....
April 15, 2011
Clarification required

Hi, Should I always declare and define a method in the declaration tag<%!... %> only of JSP? Can I define a method in jsp using Scripting element? <%...%> If yes where exactly does it fit into the servlet(JSP Converted Servlet)
June 6, 2012
JSP Methods

Can't we print some data in a method?
Kiran Mohan Reddy
July 8, 2013

i am doing widgets code replace into user.jsp file by using spring frame work there i have to select one multiple field like <ind><us><uk> can tell me the example code for this one how to set this code in user.jsp file