Solved

Can I define a funtion in .jsp file?

Posted on 2004-04-22
7
245 Views
Last Modified: 2010-04-01
For example, in this simple page.
----------------------------------------------------------------------------
<%@ page contentType="text/html;%>
<html>
<body>
<%
    String name = "test name";
    doSomeThing(name);  // <<------------------ question line.
    out.println(name);
 %>
</body>
</html>
----------------------------------------------------------------------------
I want to create a function and call it, such as doSomething(name).

Can I define a function in .jsp file?

such as :
-------------------------------
void doSomeThing(name){
   out.println("name" + name);
}
-------------------------------

Where should I declare function in .jsp file?

Thank you.
0
Comment
Question by:tsongwei
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 10890608
<!%
public void doSomething(String me)
{
me=something;
}
  %>

Rest of jsp
0
 

Author Comment

by:tsongwei
ID: 10891845
I have this. but it says:
Method getThis() not found in class org.apache.jsp.test$jsp.getThis();
----------------------------------------------------------------
<!%
    public void getThis(){
        out.println("oojoij");
    }
%>
<%@ page contentType="text/html"%>
<html>
<body>
<%
     getThis();
%>
</body>
</html>
-----------------------------------------------------------------
0
 
LVL 19

Accepted Solution

by:
Kuldeepchaturvedi earned 50 total points
ID: 10891937
ooops my fault....:-)

<!%
 should be...

<%! sorry abt that....

and one more thing..


out.println("oojoij");
will throw the error in the method for the simple reason that out is jsp variable and hence will not be available to the method....
you will  have to right it like


<%!
    public void getThis(JspWriter out){
        out.println("oojoij");
    }
%>
<%@ page contentType="text/html"%>
<html>
<body>
<%
     getThis(out);
%>
</body>
</html>


0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:PaulS_III
ID: 10894876
I have had the same problem. Remove the "public" from the function definition. The function is scoped to the JSP as it is, so public isn't needed.

Hope that helps

Paul
0
 
LVL 27

Expert Comment

by:rrz
ID: 10895578
The exception needed to handled in oder for compilation.

<%!
    public void getThis(JspWriter out) throws java.io.IOException{
        out.println("oojoij");
    }
%>
<%@ page contentType="text/html"%>
<html>
<body>
<%
     getThis(out);
%>
</body>
</html>
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 11131307
Well I thought I gave the answer there..
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
squirrelPlay java challenge 40 160
memory representation of int[][][] a=new int[2][][]; 10 60
struts spring hibernate example 12 143
Unhandled exception type Exception 18 65
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
An introduction to the wonderful sport of Scam Baiting.  Learn how to help fight scammers by beating them at their own game. This great pass time helps the world, while providing an endless source of entertainment. Enjoy!
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question