在jsp中调用java方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2354293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:29:13  来源:igfitidea点击:

Calling a java method in jsp

javajsp

提问by Hara Chaitanya

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.

我有一个 java 类,它对文件执行一些操作。由于java代码很大,我不想在jsp中编写这段代码。我想在需要时调用 jsp 中的方法。

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

请告诉我需要保存此文件的路径。还有一些示例代码如何使用它会很有帮助。

采纳答案by Sean Owen

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.

我认为问题是,您如何使 JSP 可以使用 Java 代码?您将使它像任何其他 Java 代码一样可用,这意味着它需要被编译成一个 .class 文件并放在类路径中。

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.

在 web 应用程序中,这意味着类文件必须存在于应用程序的 .war 文件或目录中的 WEB-INF/classes 下,在与其包匹配的通常目录结构中。因此,编译并部署此代码以及所有其他应用程序 Java 代码,它应该位于正确的位置。

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.

请注意,您需要在 JSP 中导入您的类,或使用完全限定的类名,否则您可以使用 <% %> 语法编写您喜欢的任何 Java 代码。

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.

您还可以使用 <%! %> 语法(注意!),导入 JSP,然后调用在这样的块中声明的方法。虽然这是不好的风格。

回答by cherouvim

In the servlet (which runs before the JSP):

在 servlet(在 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() --%>

回答by Buhake Sindi

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

尽管我不建议您在 JSP 中执行任何 java 调用,但您可以在 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:-)

如果您想知道,该<% ... %>部分称为scriptlet:-)

回答by gedevan

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

实际上,jsp 不是“对文件执行某些操作”的正确位置。你听说过MVC模式吗?

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)

如果您仍然有兴趣从jsp调用java方法,您可以这样做,例如:
1. <% MyUtils.performOperation("delete") %>scriptlet
2. <my-utils:perform operation="delete"/>(自定义标签)

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

无论如何,我建议您在 google 上搜索有关 scriptlet、jsp 自定义标签和 MVC 模式的信息。
最好的问候, 格德万

回答by BalusC

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.

根据您要调用的操作类型,您通常使用 taglib、EL 函数或 servlet。Java 代码真的、真的不属于 JSP 文件,而是属于 Java 类。

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

如果要预处理请求,请使用 ServletdoGet()方法。例如

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.

如果您想在某个表单提交后对请求进行后处理,请改用 ServletdoPost()方法。

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 taglibor create custom tags.

如果您想控制页面流和/或 HTML 输出,请使用像JSTL 核心标记库这样的标记或创建自定义标记

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

如果要执行静态/辅助函数,请使用 EL 函数,如JSTL fn taglib或创建自定义函数