Applet與Servlet通信的具體實現(xiàn)
通信過程包括下述兩個方面:
2.1 Applet對Servlet的訪問及參數(shù)傳遞的實現(xiàn)
2.1.1創(chuàng)建URL對象
在JAVA程序中,可以利用如下的形式創(chuàng)建URL對象
URL servletURL = new URL( "http://localhost:8080/servlet/dbServlet.DbServlet" );
2.1.2 與URL地址建立連接
在成功的創(chuàng)建了URL對象之后,可以調(diào)用URL類中的openConnection()函數(shù)來建立連接。openConnection()函數(shù)在建立連接的同時,進行通信連接的初始化工作。
URLConnection servletConnection = servletURL.openConnection();
2.1.3 利用URLConnection對象進行讀寫操作
2.1.3.1 利用URLConnection對象讀取Servlet返回的信息
在獲得URLConnection對象之后,如果Servlet向Applet傳送的是JAVA對象,則可以利用URLConnection對象的openStream()方法獲得輸入流,然后新生成一個ObjectInputStream對象,利用ObjectInputStream對象的readObject()方法即可得到Servlet傳回的JAVA對象。
ResultSet rs = null; // Temporary storage for the data.
URL servletURL = null; // The URL to the servlet.
URLConnection servletConnection = null; // The connection to the servlet.
ObjectInputStream dbStream = null; // The stream from the servlet.
try
{
servletURL = new URL( "http://localhost:8080/servlet/dbServlet.DbServlet" );
servletConnection = servletURL.openConnection();
dbStream = new ObjectInputStream( servletURL.openStream() );
// Read an object from the servlet stream and cast it to a DataSetData
// object.
data = (ResultSet) dbStream.readObject();
}
catch( Exception e )
{
。。。
}
如果Servlet向Applet傳送的是普通的文本,則可以利用URLConnection對象的getInputStream()方法獲得輸入流,然后新生成一個DataInputStream對象,利用DataInputStream對象的readLine()方法依次取得Servlet傳回的文本。
DataInputStream dbStream = new DataInputStream ( servletURL.getInputStream() );
// Read text from the servlet stream line by line.
While((inputLine= dbStream.readLine())!=null)
{
??????.
}
2.3.1.2利用URLConnection對象對Servlet的傳值操作
Applet向Servlet的有關(guān)參數(shù)傳遞,可以通過下面兩種方法實現(xiàn):
可以通過在URL地址后附加參數(shù)以GET的方式實現(xiàn)參數(shù)的傳遞:
servletURL = new URL( "http://localhost:8080/servlet/dbServlet.DbServlet?sql=select * from hklhzsj where total>100" );
另一種方法是從URLConnection連接獲得輸出流,這個輸出流被連接到公共網(wǎng)關(guān)程序的(服務(wù)器端)的標準輸入流上,然后把要傳送的有關(guān)數(shù)據(jù)寫入輸出流,發(fā)送完畢關(guān)閉輸出流。
servletConnection.setDoOutput(true);
PrintStream outStream=new PrintStream(servletURL.openConnection());
outStream.println(“sql=select * from hklhzsj where total>100”);
outStream.close();
2.2 Servlet向Applet的數(shù)據(jù)傳遞的實現(xiàn)
可以通過Servlet對象的request參數(shù)的getParameter()獲得Applet傳遞過來的參數(shù):String sql=request.getParameter("sql");
通過Servlet對象的request參數(shù)的getOutputStream()所得到的輸出流新生成一個對象輸出流ObjectOutputStream類型的對象,然后通過該對象的writeObject()方法輸出JAVA類型的對象。
Class.forName(“sun:jdbc:odbcdriver”);
Connection conn =DriverManager.getConnection(connetionString)
Statement st=conn.createStatement()
ResultSet rs=st.execute(sql)
dbStream = new ObjectOutputStream( response.getOutputStream() );
// Write the object...
dbStream.writeObject(rs );
通過request參數(shù)的getWriter ()方法得到PrintWriter類型的輸出,通過此對象的println()方法可以從Servlet想Applet輸出文本:
PrintWriter out = response.getWriter();
out.println("<head><title>DataCenter</title></head>");
通過上面的分析我們通過Servlet 實現(xiàn)了對非宿主機上的數(shù)據(jù)庫的訪問,Servlet 與Applet通信提供了語言級別上的互相傳遞JAVA對象的便利,我們同樣可以利用Applet通過CGI對各種服務(wù)器端的CGI程序或其它腳本應(yīng)用程序(如ASP、JSP等)提供訪問,以文本的方式實現(xiàn)通信。
posted on 2006-06-19 09:03 ASONG 閱讀(1401) 評論(0) 編輯 收藏 所屬分類: JAVA