How to execute and include a Java API from a Web A

2019-09-09 09:23发布

This is similar to the problems at java parameter in jsp:include and https://stackoverflow.com/questions/6677981/eclipse-project-from-ant-build-file-for-a-java-web-project-on-svn-doesnt-import

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

I am using

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

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

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

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?

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>

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"     %>

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

标签: java jsp include
2条回答
ら.Afraid
2楼-- · 2019-09-09 09:45

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;
%>
查看更多
Animai°情兽
3楼-- · 2019-09-09 09:52

It's not correct to include a Java Source File in a JSP page. What you should do is execute the java program (as a Class or a JAR file) from a server component, like a Servlet (you can do it from a JSP page, but such stuff should be done in the Controller layer).

查看更多
登录 后发表回答