1、大体说明
在查询机上访问web应用进行查询,所以采用在查询机上运行桌面程序(swt中嵌入browser 访问web服务),并在查询机开启时运行该程序。
2、桌面程序源码
public class QueryBrowser { Display display = new Display(); Shell shell = new Shell(display, SWT.NO_TRIM); public static final String DEFAULT_URL = "http://192.168.1.101:8080/test/hello.html"; Browser browser; //读取配置文件中url属性 public static String getQueryUrl() { Properties prop = new Properties(); try { InputStream is = new FileInputStream(Utils.APP_INI_FILE); prop.load(is); is.close(); } catch (Exception e) { e.printStackTrace(); } return Utils.getProperty(prop, "url", DEFAULT_URL); } public QueryBrowser() { shell.setLayout(new GridLayout()); browser = new Browser(shell, SWT.BORDER); browser.setLayoutData(new GridData(GridData.FILL_BOTH)); //判断页面是否可以正常访问 if (URLAvailability.isConnect(getQueryUrl())) browser.setUrl(getQueryUrl()); else browser.setText("<HTML><BODY><table border='0' width='100%' height='600'><tr><td align='center'><h1>无法访问页面,请检查网络...</h1></td></tr></table></BODY></HTML>"); // initialize(display, browser); shell.setMaximized(true); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { // If no more entries in event queue display.sleep(); } } display.dispose(); } public static void main(String[] args) { new QueryBrowser(); } }
说明:
shell.setMaximized(true); //这里是关键,可以铺满整个显示屏幕
2、Utils.java 、URLAvailability.java
读取配置文件XXX.ini中url属性值
public class Utils { public static final File APP_PATH = new File("").getAbsoluteFile(); public static final File APP_INI_FILE = new File(APP_PATH, "XXX.ini").getAbsoluteFile(); static { //APP_TEMP.mkdir(); try { if (!APP_INI_FILE.exists()) APP_INI_FILE.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } public static String getProperty(Properties prop, String key,String defaultValue) { String s = prop.getProperty(key); if (s == null) { s = defaultValue; } return s; } }
判断url是否可以正常访问
public class URLAvailability { private static URL urlStr; private static HttpURLConnection connection; private static int state = -1; private static boolean connec = false; public static boolean isConnect(String url) { if (url == null || url.length() <= 0) { connec = false; } try { urlStr = new URL(url); connection = (HttpURLConnection) urlStr.openConnection(); state = connection.getResponseCode(); if (state == 200) { connec = true; } } catch (Exception ex) { connec = false; } return connec; } }
3、XXX.ini中url属性值设置
url=http\://192.168.1.101\:8080/test/hello.html
4、制作运行程序,并将运行启动文件(如run.bat)加入系统启动项中。