Java 相当于 ASP.net 中的代码隐藏吗?
我有一个相当好奇的场景,我确信在 stackoverflow 上浏览问题的优秀人才会毫不费力地帮助我解决这个问题。
我有一个 Java 项目,它的主要功能是获取文件并将其上传到成像系统中。该代码的工作方式是给定文件的路径,它会获取该文件并将其上传到我想要的系统(这包括许多特定于业务的逻辑,这些逻辑没有任何披露的目的)。
现在,我想要一个带有 FILE 输入的 HTML 页面,该页面获取一个文件,然后调用我编写的代码,该代码将完成获取文件并将其上传到系统的工作。
这意味着我必须将我的 Java 项目迁移到可以“可调用”的项目中。
有很多第三方 jar 文件可以帮助我的代码运行。
第一个场景是以某种方式将代码放入 scriptlet - 所有其他代码都可以编译到 jar 中。我讨厌使用 jsp 来完成这项工作,但为了让它达到正常工作的阶段,我愿意考虑这个选项。 我现在的问题是 jar 文件去哪里? 这将在 Apache Tomcat 中运行,所以我想我只需将 jar 放在 lib 目录中并重新启动 Tomcat 就可以了?是否需要修改任何类路径文件才能使 Tomcat 服务器识别新的 jar 文件?
第二种情况是有一个 servlet,然后 jar 放在 WEB-INF 目录中?请有人指导我这个。
您认为将执行业务逻辑的 Java 代码“迁移”到可以从 html 页面调用的内容的最佳方法是什么? 相当于代码隐藏的是什么?
预先非常感谢您,希望您能为我稍微解释一下这个问题。
I have a rather curious scenario, which I am sure that the immense talent browsing through the questions here at stackoverflow will have very little trouble helping me out.
I have a project in Java - which pretty much does something with taking a file and uploading it in an imaging system. The code works in a way that given the path of the file, it takes it and uploads to the system I want it (this includes a lot of business-specific logic which serves no purpose to be disclosed).
Now, I would like to have an HTML page with a FILE input, that takes a file and then CALLS the code that I wrote which will do the job of taking the file and uploading it to the system.
This means that I have to migrate my Java project into something that can be "callable".
There are a lot of third party jar files that help my code run.
First scenario would be to somehow make the code into a scriptlet - all the other code can be compiled into a jar. I hate having a jsp that does the deal, but just to get it to a stage where it is working, I would like to entertain this option.
My question now is where do the jar files go?
This will be running in Apache Tomcat, so I guess I just place the jars in the lib directory and restart Tomcat and that should do the deal? Is there any classpath file that needs to be modified to make the Tomcat server be aware of the new jar files?Second scenario would be to have a servlet, and then the jars go in the WEB-INF directory? Please somebody guide me with this one.
What do you think would be the best way to "migrate" my Java code that does the business logic to something that can be called from an html page?
What would be the equivalent of a code-behind?
Thank you very much in advance and hope you can elucidate this matter for me a little bit.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这实际上是一个非常标准的场景,您可以采取多种方法,具体取决于您希望该服务的可扩展性、安全性等。我将从需要您完成最少工作量的方法开始:
更彻底的方法是让您的 servlet 直接调用后端 Java 代码。如果您已经有一个可以调用的接口,那么您可以将 jar 文件放入 web 应用程序的 WEB-INF/lib 文件夹中,您的 servlet 将能够引用 jar 文件中的类,以便您可以调用无论您需要什么课程,都可以直接进行。如果这个过程非常耗时,那么您不想阻止 servlet,从而阻止请求,因此最好将其交给异步线程,该线程将负责处理需要使用 servlet 完成的任何事情。文件并随后清理。
This is actually a pretty standard scenario, and there are tons of approaches you can take depending on how scalable you want this service to be, how secure, etc. I'll start with the approach that requires the least amount of work on your end:
ProcessBuilder
and pass the path to it as a command-line argument. Wait for it to complete.A more thorough approach will involve having your servlet directly call the backend Java code. If you already have an interface you can call then you can put the jar file into your webapp's
WEB-INF/lib
folder and your servlet will be able to reference the classes in the jar file so you can call whatever class you need to directly. If this process is going to be time consuming then you do not want to hold up the servlet, and consequently the request, so it's good practice to hand it off to an asynchronous thread that will take care of doing whatever needs to be done with the file and cleaning up afterwards.也许最接近的等价物是 servlet。在
WEB-INF/web.xml
中,您可以设置 servlet 来处理传入某些 URL 的 Web 请求。一旦您在 servlet 中处理了用户的请求,您通常会将响应转发到 .jsp 文件。3rd 方库位于 WEB-INF/lib 中。 (另一种方法是将第 3 方 jar 放入 tomcat 的 lib 文件夹中。我不推荐这种方法,因为假设您决定迁移到不同版本的 Tomcat。您如何记住要复制哪些 jar?)
Perhaps the closest equivalent would be a servlet. In
WEB-INF/web.xml
You can set up servlets to handle web requests coming to certain URLs. Once you have handled the user's request in the servlet, you usually forward the respone to a .jsp file.3rd party libraries go in
WEB-INF/lib
. (Another approach is to put the 3rd party jars into tomcat's lib folder. I wouldn't recommend this approach because, say you decide to migrate over to a different version of Tomcat. How do you remember which jars to copy?)