I have a JSP file inputs a string to a Java class, and should get an ArrayList returned from it. Do I need a public class (i.e. void main(String[] args) ) to be able to return to the calling JSP, or is public Scopus() enough to return the values?
Scopus.java (accepts in scopusID, returns 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 (Calls the above class)
<%@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>
The class you specify in class must not be abstract and must have a public, no-argument constructor which you don’t have.
Update:: You don’t need main() method, a public Scopus() non-argument constructor will be fine. The JSP container will create an instance of this class using reflection using the zero-argument constructor. If you don’t create any constructor, Java compiler will add one to the compiled class. You only need to rename your original constructor with parameter to make it a method to do all those business logic things and call it somewhere after you have set scopusID property and before the
getScopusList()method is called (when you try to access the scopusList property).As for the presentation of the list data, it is not a good idea to use:
You better use JSP EL and JSTL core taglib and do something like this: