Call Java Method in JSP

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

I asked a similar question before in How to execute and enable the Java API from a web application project , but since then its change has changed in that I now have Java code in WebAp

I am trying to call a Java method from a JSP page and return the results. I looked through a lot of posts, but I'm afraid that my lack of experience with any of the languages ​​is the main problem.

I have a JSP WebAp that searches for an XML database and returns content to the user. I was asked to integrate additional Java code that searches for predefined websites and returns content.

Ive played with similar code below, but I think Im looking for something else

<%@ include file="/Applications/Tomcat/apache-tomcat-6.0.16/webapps/myWebApp/program.java" %> 

Can someone give me a better idea of ​​what I'm looking for?

Also do you know if I need to edit anything else in WebAp to include two files? Class files in the WebINF folder?

Any help is much appreciated

Deepend


 package sliceClient; import java.util.List; public class Run { @Inject private SliceSearcher sliceSearcher; @Inject private SliceCreator sliceCreator; /** * @param args */ public static void main(String[] args) { Injector injector = Guice.createInjector(new GuiceInjector()); Run r = injector.getInstance(Run.class); r.runSliceConsumer(); } private void setSlicepediaProductionMode(boolean productionMode){ sliceSearcher.setProductionMode(productionMode); sliceCreator.setProductionMode(productionMode); } public void runSliceConsumer() { System.out.println("Starting Slice Consumer"); //Remove this line if the real slicepedia server is to be used setSlicepediaProductionMode(true); List<SliceHit> sliceHits = searchForSlices(); 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("Slice content:"); System.out.println(sliceContent); } else { System.out.println("No Slices were found for this query"); } System.out.println("Slice Consumer stopped activity"); } private String createSlices(List<SliceHit> sliceHits) { sliceCreator.setSliceHits(sliceHits); if (sliceCreator.run()) { SlicePackage slicePackage = sliceCreator.getNextSlicePackage(); return slicePackage.getSliceContent(); } else { return sliceCreator.getErrorMessage(); } } private List<SliceHit> searchForSlices() { SlicepediaQuery sliceQuery = new SlicepediaQuery(); sliceQuery.paramANNOTATION_READING_DIFFICULTY(new Double(30), "<"); //Works // String dbConcept = "http://dbpedia.org/resource/human_rights"; // sliceQuery.paramANNOTATION_CONCEPT_FEATURE_HAS_DBPEDIA(dbConcept,0.5, ">"); // sliceQuery.paramHAS_NBR_OF_PARAGRAPHS(1,">"); // sliceQuery.paramIsAnOpenSlice(true); // sliceQuery.paramHasNumberOfToken(80, ">"); sliceSearcher.setSliceQuery(sliceQuery); if (sliceSearcher.run()) { return sliceSearcher.getSliceHits(); } else { return null; } } 

}

+7
source share
4 answers

Firstly, an ugly way (perhaps because it is so similar to php ):

 <%= com.example.MyUtility.getSomething() %> 

It is called a and is considered bad practice. In fact, it is so wrong that I am ashamed to even write it. Instead, you should have a front controller (a simple servlet will do the trick), put the results in the request attributes and forward the JSP, which in turn uses or for output. Much more work, but better by an order of magnitude:

In the servlet:

 request.setAttribute("someData", MyUtility.getSomething()) RequestDispatcher dispatcher = request.getRequestDispatcher("page.jsp"); dispatcher.forward(request, response); 

In page.jsp :

 ${someData} 

There are various frameworks that can reduce the number of templates ( is a simple example).

+11
source

Well, the easiest way is to import your class into JSP and call its methods through scripts.

To enable a class:

 <%@page import="package.subPackage.TheClassName"%> 

Please note that TheClassName does not contain any suffixes such as .java or .class

Then

 <% TheClassName theClassInstance = new TheClassName(); theClassInstance.doSomething(); %> 

Effectively here you are writing plain old Java code between the <% and %> tags.

This, however, is not the best way to go in terms of how your code is organizational. In the presentation should go only the logic of the presentation. Consolidation using such scriptlets is bad.

If you need the help of an instance method of an instance class to do something in your view, simply create a tag that makes a method call (and any other logic that is not related to viewing), and then returns only what should be displayed, Here are the official docs about it

http://docs.oracle.com/javaee/5/tutorial/doc/bnalj.html

+7
source

I do not know that I probably understand your question.

You need to import java class and run java code in JSP

 <%@ page import="package1.myClass1,package2.myClass2,....,packageN.myClassN" %> 

import the class and then

 <% new ClassXXX().methodYYY(); %> 

However, running java code in jsp is not a good idea ...

+3
source

Create a JAVA class in the src folder and import this class on a JSP page like this.

 <%@ page import="com.MyClass"%> 

Now let's assume that you have a function in the class that returns String, then you will write this in JSP as:

 <% String stringFromJava = MyClass.getStringForJSPPage(); %> 

Now you can use stringFromJava anywhere in the JSP . You can also get List in the same way.

+1
source

All Articles