Deepend
Deepend

Reputation: 4155

Displaying Java content in JSP

Setup - Tomcat 6.0.16 - MacBook

I have a JSP page that is invoking a separate Java application that searches the web and returns content. I am looking to display this content on my JSP page.

My JSP code - Which invokes the below JAVA

System.out.println("Test3");
Injector injector = Guice.createInjector(new GuiceInjector());
Run r = injector.getInstance(Run.class);
r.runSliceConsumer();   

My JAVA - This works and prints my content to the Terminal window.

if (sliceHits == null) {
    System.err.println("Timeout occurred while fetching slices");
    return;
}
if (!sliceHits.isEmpty()) {
    System.out.println("Found some slices Yuhuuuu ! :-) ");
    String sliceContent = createSlices(sliceHits);
    System.out.println("sliceContent:");
    System.out.println(sliceContent);
} 
else {
    System.out.println("No Slices were found for this query");
}

My issue is that i want to display the content above in my JSP web page and not just the Terminal window. I assume that as the connection is made one way, my JSP invokes my JAVA that i should be able to just display the results but im having a few problems, i hope its just with my Syntax.

My attempt in JSP

<div id="result-page-query" align='center'>
<%
    sliceContent = createSlices(sliceHits);
    out.println(sliceContent);
%>
</div>

I’m not sure if I’m explaining this right, but essentially I’m trying to display the content of "sliceContent" on my Webpage

Thanks

EDIT:

Hi as suggest below by jddsantaella and Hardik Mishra I had to import the necessary packages. I then created an object in this case "kContent" and executed the method.

The solution was similar to the below

<%

    Run kContent = injector.getInstance(Run.class);
    kContent.runSliceConsumer();
    out.println(kContent);
%>

Upvotes: 1

Views: 5518

Answers (2)

Hardik Mishra
Hardik Mishra

Reputation: 14877

It is not advised to use JAVA code in JSP. JSPs are mainly for presentation.

Second, "org.apache.jasper.JasperException: Unable to compile class for JSP is a runtime exception. When you run a JSP and if there is a change then previously compiled JSP, your web container compiles JSP at run time.

Also, You should add the necessary import statements at the beginning of the jsp

<%@ page import="java.util.List" %>

<%@ page import="yourpackage.slicer" %> 

And Last,

<%
    sliceContent = createSlices(sliceHits);
    out.println(sliceContent);
%>

You can print String returned value from your method. Just check in calling method like myObj.myMethod()

Upvotes: 1

jddsantaella
jddsantaella

Reputation: 3687

You should not use Java code in your JSP, it is not recommendable. Anyway, you can print the value returned by a method doing something like this:

...
<%=myObjetct.myMethod(...)%>
...

In your attempt is not clear what createSlices is. I think it could be

<%=r.runSliceConsumer()%>

assuming that your runSliceConsumer method is returning something

Upvotes: 1

Related Questions