jsp调用java的数据,在JSP中调用Java方法

Setup

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

I asked a similar question to this before at How to execute and include a Java API from a Web App project but the set up has since changed in that i now have the Java code in the WebAp

I am trying to invoke a Java Method from a JSP page and return the results. I have looked at a lot of posts but I’m afraid my lack of experience with either language is the main problem.

I have a JSP WebAp that searches an XML database and returns content to the user. I have been asked to integrate additional java code that searches predefined Websites and returns content.

I’ve played around with similar code to the below but I think I’m looking for something else

Can anyone give me a better idea of what I’m looking for?

Also do you know if I have to edit anything else in the WebAp to connect the two files? The class files in the WebINF folder?

Any help is very 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 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 sliceHits) {

sliceCreator.setSliceHits(sliceHits);

if (sliceCreator.run()) {

SlicePackage slicePackage = sliceCreator.getNextSlicePackage();

return slicePackage.getSliceContent();

} else {

return sliceCreator.getErrorMessage();

}

}

private List 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;

}

}

}

解决方案

First, the ugly way (maybe because so similar to php?):

It is called a scriptlet and is considred a bad practice. In fact this is so wrong that I am ashamed of even writing this. What you should do instead is to have a front controller (a simple servlet will do the trick), place results in the request attributes and forward to a JSP, which in turns uses jstl or jsp el for output. Much more work, but better by an order of magnitude:

In 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 amount of boilerplate (spring-mvc is a simple example).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值