在jsp中调用java方法 [英] Calling a java method in jsp

查看:88
本文介绍了在jsp中调用java方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

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

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.

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

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.

请注意,您需要导入您的类JSP,或者使用完全限定的类名,但是你可以使用< %%>语法编写你喜欢的任何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中的方法,使用<%! %>语法(注意!),导入JSP,然后调用在这样的块中声明的方法。这是不好的风格。

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中调用java方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆