tags:

views:

304

answers:

6

I have a java class which performs some operations on files. Since the java code is huge I don't want to write this code in jsp. I want to call the methods in jsp whenever required.

Please tell me the path where I need to keep this file. Also some example code how to use it would be helpful.

+2  A: 

In the servlet (which runs before the JSP):

Person p = new Person(); // instantiate business object
p.init(...); // init it or something
request.setAttribute("person", p); // make it available to the template

In the template you can use this:

your age is: ${p.age}  <%-- calls person.getAge() --%>
cherouvim
You don't *have* to run a separate servlet prior to the JSP, although the JSP file itself will be compiled to a servlet on-the-fly.
Rob
yes, it's not mandatory, just good practice
cherouvim
+3  A: 

Although I'll not advice you to do any java calls in JSP, you can do this inside your JSP:

<%
   //Your java code here (like you do in normal java class file.
%>

<!-- HTML/JSP tags here -->

In case you're wondering, the <% ... %> section is called scriptlet :-)

The Elite Gentleman
+2  A: 

I think the question is, how do you make Java code available to a JSP? You would make it available like any other Java code, which means it needs to be compiled into a .class file and put on the classpath.

In web applications, this means the class file must exist under WEB-INF/classes in the application's .war file or directory, in the usual directory structure matching its package. So, compile and deploy this code along with all of your other application Java code, and it should be in the right place.

Note you will need to import your class in the JSP, or use the fully-qualified class name, but otherwise you can write whatever Java code you like using the <% %> syntax.

You could also declare a method in some other utility JSP, using <%! %> syntax (note the !), import the JSP, and then call the method declared in such a block. This is bad style though.

Sean Owen
Thanks for your answer, i have done as you have said import java class and use it....thanks once again to all who answered...
Hara Chaitanya
+1  A: 

Actually, jsp is not the right place to 'performs some operations on files'. Did you hear about MVC pattern?

If you still interested in calling java method from jsp you can do it, for example:
1. <% MyUtils.performOperation("delete") %> (scriptlet)
2. <my-utils:perform operation="delete"/> (custom tag)

Anyway I recomend you to google about scriptlets, jsp custom tags and MVC pattern.
Best Regards, Gedevan

gedevan
A: 

If you want to abstract away the request-response nature of the web, and actually perform a chosen method on a click of a button (or other event), you can take a look at JSF. There you can do something like:

<h:commandButton action="#{myBean.myMethod}" /> 

and that method (public void myMethod()) of the bean (myBean, a registered managed bean) will get executed on click.

Bozho
+1  A: 

Depending on the kind of action you'd like to call, there you normally use taglibs, EL functions or servlets for. Java code really, really doesn't belong in JSP files, but in Java classes.

If you want to preprocess a request, use the Servlet doGet() method. E.g.

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // Preprocess request here.
    doYourThingHere();
    // And forward to JSP to display data.
    request.getRequestDispatcher("page.jsp").forward(request, response);
}

If you want to postprocess a request after some form submit, use the Servlet doPost() method instead.

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // Postprocess request here.
    doYourThingHere();
    // And forward to JSP to display results.
    request.getRequestDispatcher("page.jsp").forward(request, response);
}

If you want to control the page flow and/or HTML output, use a taglib like JSTL core taglib or create custom tags.

If you want to execute static/helper functions, use EL functions like JSTL fn taglib or create custom functions.

BalusC