Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Can I define a funtion in .jsp file?

Posted on 2004-04-22
7
Medium Priority
?
248 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
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 200 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
Technology Partners: 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 28

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

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!

Question has a verified solution.

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

In a use case, a user needs to close an opened report by simply pressing the Escape (Esc) key. This can be done by adding macro code in Report_KeyPress or Report_KeyDown event.
There can be many situations demanding the conversion of Outlook OST files to PST format and as such, there is no shortage of automated tools to perform this conversion. However, what makes Stellar OST to PST converter stand above the rest? Let us e…
Integration Management Part 2
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

963 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