简体   繁体   English

在jsp中调用Java方法

[英]Calling a java method in jsp

I have a java class which performs some operations on files. 我有一个Java类,对文件执行一些操作。 Since the java code is huge I don't want to write this code in jsp. 由于Java代码庞大,因此我不想在jsp中编写此代码。 I want to call the methods in jsp whenever required. 我想在需要时调用jsp中的方法。

Please tell me the path where I need to keep this file. 请告诉我保存此文件的路径。 Also some example code how to use it would be helpful. 另外一些示例代码如何使用它也会有所帮助。

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

I think the question is, how do you make Java code available to a JSP? 我认为问题是,如何使Java代码可用于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. 您将使其像任何其他Java代码一样可用,这意味着需要将其编译为.class文件并放在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. 在Web应用程序中,这意味着类文件必须存在于应用程序的.war文件或目录中的WEB-INF / classes下,并且位于与其包匹配的常规目录结构中。 So, compile and deploy this code along with all of your other application Java code, and it should be in the right place. 因此,将此代码与所有其他应用程序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 <%! 您还可以使用<%!在其他实用程序JSP中声明一个方法。 %> syntax (note the !), import the JSP, and then call the method declared in such a block. %>语法(注意!),导入JSP,然后调用在这样的块中声明的方法。 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. 根据您要调用的操作类型,通常会在其中使用taglib,EL函数或servlet。 Java code really, really doesn't belong in JSP files, but in Java classes. Java代码实际上确实不属于JSP文件,而是属于Java类。

If you want to preprocess a request, use the Servlet doGet() method. 如果要预处理请求,请使用Servlet doGet()方法。 Eg 例如

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. 如果要在表单提交后对请求进行后处理,请改用Servlet doPost()方法。

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 . 如果要控制页面流和/或HTML输出,请使用JSTL core taglib之类的taglib或创建自定义标签

If you want to execute static/helper functions, use EL functions like JSTL fn taglib or create custom functions . 如果要执行静态/辅助函数,请使用JSTL fn taglib之类的EL函数或创建自定义函数

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

Actually, jsp is not the right place to 'performs some operations on files'. 实际上,jsp不是执行“对文件执行某些操作”的正确位置。 Did you hear about MVC pattern? 您是否听说过MVC模式?

If you still interested in calling java method from jsp you can do it, for example: 如果您仍然对从jsp调用java方法感兴趣,可以这样做,例如:
1. <% MyUtils.performOperation("delete") %> ( scriptlet ) 1. <% MyUtils.performOperation("delete") %>scriptlet
2. <my-utils:perform operation="delete"/> (custom tag) 2. <my-utils:perform operation="delete"/> (自定义标签)

Anyway I recomend you to google about scriptlets, jsp custom tags and MVC pattern. 无论如何,我建议您向Google提出有关scriptlet,jsp自定义标记和MVC模式的信息。
Best Regards, Gedevan 最好的问候,杰德万

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM