如何从Web App项目执行和包含Java API [英] How to execute and include a Java API from a Web App project

查看:93
本文介绍了如何从Web App项目执行和包含Java API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这类似于
中的问题 jsp:include中的java参数

https://stackoverflow.com/questions/6677981/eclipse-project-from-ant-build-file-for-a-java-web-project-on-svn-doesnt-import

我希望从JSP文件(response.jsp)执行Java文件(run.java)并将其输出返回到JSP文件。

I am looking to execute a Java file (run.java) from a JSP file (response.jsp) and return its output to the JSP file.

我正在使用

Mac OSX 10.6.8,
Apache Tomcat 6.0.16,
Java1.6.0_29,
Eclipse IDE Indigo,

Mac OSX 10.6.8, Apache Tomcat 6.0.16, Java1.6.0_29, Eclipse IDE Indigo,

我在Tomcat中部署了一个JSP Web App,用于搜索XML内容存储库并将结果返回给用户。

I have a JSP Web App deployed in Tomcat that searches an XML content repository and returns results to the user.

我被要求包含一个使用相同查询抓取预定义网站的Java API。这必须与Web App分开

I have been asked to include a Java API that crawls predefined websites using the same query. This has to stay separate from the Web App

我需要将用户Query发送到Java API并将内容返回给JSP WebAp。任何人都可以给我一些关于将查询发布到外部Java API的点击吗?

I need to send the users Query to the Java API and return the content to the JSP WebAp. Can anyone give me a few hits about posting a query to an external Java API?

JSP文件已经在同一个WebAp中调用其他.jsp文件,例如

The JSP file already calls other .jsp files in the same WebAp e.g.

<%@ include file="siteheader.jsp" %>
<jsp:include page="<%= session.getAttribute(\"device\") + \"header.jsp\"%>">
<jsp:param name="title" value="Response" />
</jsp:include>

我已尝试过以下内容以及其他一些变体,以便至少连接到外部java文件,但无法破解它。

I have tried the below and more then a few other variations to get it to at least connect to the external java file but cant crack it.

<%@ include file="/Users/me/Documents/workspace/Slicer/src/slicer/Run.java"     %>

我一直收到tomcat错误,文件Macintosh HD / Users / me / Documents / workspace / Slicer /src/slicer/Run.java找不到

I keep getting the tomcat error, File "Macintosh HD/Users/me/Documents/workspace/Slicer/src/slicer/Run.java" not found

任何建议或帮助都非常感谢

Any suggests or help is much appreciated

B

推荐答案

请查看BalusC的解决方案,价格为
如何以编程方式编译和实例化Java类?

对于测试,复制并粘贴以下JSP。我希望BalusC不介意我改变他的代码。

Please look at BalusC's solution at
How do I programmatically compile and instantiate a Java class?
For testing, copy and paste the following JSP. I hope BalusC doesn't mind that I changed his code.

<%@ page import="java.util.*,java.io.*,javax.tools.*,java.net.*" %><%
System.setOut(new PrintStream(response.getOutputStream()));
// Prepare source somehow. 
String source = "package test; public class Test { static { System.out.println(\"hello\"); } public Test() { System.out.println(\"rickz\"); } }";  
// Save source in .java file. 
File root = new File("/testJava"); 
// On Windows running on C:\, this is C:\testJava. 
File sourceFile = new File(root, "test/Test.java"); 
sourceFile.getParentFile().mkdirs(); 
new FileWriter(sourceFile).append(source).close();  
// Compile source file. 
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); 
compiler.run(null, null, null, sourceFile.getPath());  
// Load and instantiate compiled class. 
URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { root.toURI().toURL() }); 
Class<?> cls = Class.forName("test.Test", true, classLoader); // Should print "hello". 
Object instance = cls.newInstance(); // Should print "world". 
System.out.println(instance); // Should print "test.Test@hashcode". 
if(out != null) return;
%>

这篇关于如何从Web App项目执行和包含Java API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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