-- 作者:admin
-- 发布时间:2009/5/27 22:46:52
-- 利用HTTP请求方式获取eXist查询结果的窗体程序
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection;
import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.ScrollPaneConstants;
public class Exec { public static void main(String[] args) { DemoWindow dw = new DemoWindow("XQuery"); Toolkit theKit = dw.getToolkit(); Dimension wndSize = theKit.getScreenSize(); dw.setBounds(wndSize.width / 3, wndSize.height / 3, wndSize.width / 3, wndSize.height / 3); dw.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); dw.setVisible(true); } }
class DemoWindow extends JFrame implements ActionListener { JTextArea jta1 = new JTextArea(); JTextArea jta2 = new JTextArea(); JButton jb = new JButton("确认"); JPanel jp = new JPanel(); int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED; int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED; JScrollPane jsp = new JScrollPane(jta2, v, h);
public DemoWindow(String title) { super(title); setLayout(new BorderLayout()); jp.add(jb); jb.addActionListener(this); jta2.setBackground(Color.yellow); add(jta1, BorderLayout.NORTH); add(jsp, BorderLayout.CENTER); add(jp, BorderLayout.SOUTH); }
public void actionPerformed(ActionEvent e) { GetXMLInfo gxi = new GetXMLInfo(); jta2.setText(gxi.getResult(jta1.getText())); } }
class GetXMLInfo { public String getResult(String query) { char[] data = null; FileOutputStream fos; URL url; URLConnection urlConnection; InputStream is; int i; try { fos = new FileOutputStream("results.xml"); url = new URL("http://localhost:8080/exist/servlet/db/?_query=" + query); urlConnection = url.openConnection(); is = urlConnection.getInputStream();
i = is.read(); while (i > 0) { fos.write(i); i = is.read(); } fos.close(); is.close(); File file = new File("results.xml"); int size = (int) file.length(); FileReader in = new FileReader(file); data = new char[size]; in.read(data, 0, size); in.close(); } catch (IOException e) { } return new String(data); }
}
[此贴子已经被作者于2010-12-14 09:42:19编辑过]
|