开发者

Calling a java method in jsp

开发者 https://www.devze.com 2022-12-22 00:07 出处:网络
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.

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 开发者_如何学Gokeep this file. Also some example code how to use it would be helpful.


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 as 'person'

In the template you can use this:

your age is: ${person.age}  <%-- calls person.getAge() --%>


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.


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.


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 :-)


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

0

精彩评论

暂无评论...
验证码 换一张
取 消