How to execute and incorporate the Java API in a web application project

This seems like problems with the java parameter in jsp: include as well as https://stackoverflow.com/questions/6677981/eclipse-project-from-ant-build-file-for-a-java-web-project-on-svn -doesnt-import

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

I use

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

I have a Tomcat JSP web application that searches for an XML content repository and returns the results to the user.

I was asked to enable the Java API, which crawls predefined websites using the same request. This should remain separate from the web application.

I need to send a user request in the Java API and return the content to JSP WebAp. Can someone give me some hits on publishing a request to an external Java API?

The JSP file already calls other .jsp files in the same WebAp, for example.

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

I tried the following and more than a few other options to make it at least connect to an external java file, but can't break it.

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

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

Any suggestions or help are greatly appreciated.

IN

0
source share
2 answers

Look at the BalusC solution at
How to programmatically compile and instantiate a Java class? To test, copy and paste the following JSP. I hope BalusC does not mind that I changed its 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; %> 
0
source

Incorrect inclusion of the Java source file on the JSP page. You must execute the java program (as a class or JAR file) from a server component, for example a servlet (you can do this from the JSP page, but such things should be done at the controller level).

+1
source

All Articles