1

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);
%>
Deepend
  • 4,057
  • 17
  • 60
  • 101
  • what results do you see from your attempt, using <% out.println(sliceContent) %>? – pb2q May 29 '12 at 16:35
  • @pb2q Hey there. That gives me a Tomcat error message below with the error right at that line "org.apache.jasper.JasperException: Unable to compile class for JSP:" – Deepend May 29 '12 at 17:52
  • @dystroy: `out` is implicitly available in JSP and can be used to print like above given code. – Hardik Mishra May 30 '12 at 03:46

2 Answers2

1

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

jddsantaella
  • 3,657
  • 1
  • 23
  • 39
  • Hi jddsantaella I think you are on to somthing here. Ill play around with it and see what i come up with. JSP is not my strong point so ill be trying random syntax combinations – Deepend May 29 '12 at 17:58
  • Hi jddsantaella Just wanted to say thanks fr your help. I have posted my solution based on your suggestion above. – Deepend Jun 05 '12 at 11:54
1

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()

Community
  • 1
  • 1
Hardik Mishra
  • 14,779
  • 9
  • 61
  • 96