我有一个 JSP 文件向 Java 类输入一个字符串,并且应该从它返回一个 ArrayList。我是否需要一个公共类(即void main(String[] args))才能返回调用 JSP,还是public Scopus()足以返回值?
Scopus.java(接受 scopusID,返回 scopusList)
package newpackage1;
import org.w3c.dom.Document;
import org.w3c.dom.CharacterData;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import java.util.ArrayList;
import java.net.URL;
import java.net.MalformedURLException;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
public class Scopus {
String scopusID;
URL url;
ArrayList<String> scopusList = new ArrayList<String>();
NodeList nodes;
DocumentBuilder builder;
Document doc;
public void setScopusList(ArrayList scopusList) {
this.scopusList = scopusList;
}
public ArrayList getScopusList() {
return scopusList;
}
public void setScopusID(String url) {
this.scopusID = url;
}
public String getScopusID() {
return scopusID;
}
public Scopus(String scopusID) {
String fTitle, fLink;
try {
URL url = new URL( "http://syndic8.scopus.com/getMessage?registrationId=" + scopusID );
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
try {
builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
doc = builder.parse(url.openStream());
nodes = doc.getElementsByTagName("item");
} catch (ParserConfigurationException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (SAXException e) {
throw new RuntimeException(e);
}
//Only output if at least one is found
int counter = 0;
if ( nodes.getLength() > 0 ) {
//Place all results into an array list first
for(int i=0;i<nodes.getLength();i++) {
Element element = (Element)nodes.item(i);
fTitle = getElementValue(element, "title");
fLink = getElementValue(element, "link");
scopusList.add("<a href=\"" + fLink + "\" target=\"_blank\">" + fTitle + "</a>");
counter++;
}
}
}
public static void main(String[] args) {
}
private String getElementValue(Element parent,String label) {
return getCharacterDataFromElement((Element)parent.getElementsByTagName(label).item(0));
}
private String getCharacterDataFromElement(Element e) {
try {
Node child = e.getFirstChild();
if(child instanceof CharacterData) {
CharacterData cd = (CharacterData) child;
return cd.getData();
} //if
} //try
catch(Exception ex) {
}
return " ";
} //private String getCharacterDataFromElement
}
output.jsp(调用上面的类)
<%@page import="java.util.ArrayList,org.w3c.dom.Node,org.w3c.dom.NodeList" %>
<%
String feedID = "HEDCIHLCIGDKPFHHJEEEHJDEIEGJIKJHKWFQWLHFJH";
%>
<jsp:useBean id="scopus" scope="page" class="newpackage1.Scopus">
<jsp:setProperty name="scopus" property="scopusID" value="<%= feedID %>" />
</jsp:useBean>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<jsp:getProperty name="scopus" property="scopusList" />
<%
ArrayList sl = scopus.getScopusList();
//Will do output later
%>
</body>
</html>