Applet與Servlet通信的具體實(shí)現(xiàn)
通信過程包括下述兩個(gè)方面:
2.1 Applet對(duì)Servlet的訪問及參數(shù)傳遞的實(shí)現(xiàn)
2.1.1創(chuàng)建URL對(duì)象
在JAVA程序中,可以利用如下的形式創(chuàng)建URL對(duì)象
URL servletURL = new URL( "http://localhost:8080/servlet/dbServlet.DbServlet" );
2.1.2 與URL地址建立連接
在成功的創(chuàng)建了URL對(duì)象之后,可以調(diào)用URL類中的openConnection()函數(shù)來建立連接。openConnection()函數(shù)在建立連接的同時(shí),進(jìn)行通信連接的初始化工作。
URLConnection servletConnection = servletURL.openConnection();
2.1.3 利用URLConnection對(duì)象進(jìn)行讀寫操作
2.1.3.1 利用URLConnection對(duì)象讀取Servlet返回的信息
在獲得URLConnection對(duì)象之后,如果Servlet向Applet傳送的是JAVA對(duì)象,則可以利用URLConnection對(duì)象的openStream()方法獲得輸入流,然后新生成一個(gè)ObjectInputStream對(duì)象,利用ObjectInputStream對(duì)象的readObject()方法即可得到Servlet傳回的JAVA對(duì)象。
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對(duì)象的getInputStream()方法獲得輸入流,然后新生成一個(gè)DataInputStream對(duì)象,利用DataInputStream對(duì)象的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對(duì)象對(duì)Servlet的傳值操作
Applet向Servlet的有關(guān)參數(shù)傳遞,可以通過下面兩種方法實(shí)現(xiàn):
可以通過在URL地址后附加參數(shù)以GET的方式實(shí)現(xiàn)參數(shù)的傳遞:
servletURL = new URL( "http://localhost:8080/servlet/dbServlet.DbServlet?sql=select * from hklhzsj where total>100" );
另一種方法是從URLConnection連接獲得輸出流,這個(gè)輸出流被連接到公共網(wǎng)關(guān)程序的(服務(wù)器端)的標(biāo)準(zhǔn)輸入流上,然后把要傳送的有關(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ù)傳遞的實(shí)現(xiàn)
可以通過Servlet對(duì)象的request參數(shù)的getParameter()獲得Applet傳遞過來的參數(shù):String sql=request.getParameter("sql");
通過Servlet對(duì)象的request參數(shù)的getOutputStream()所得到的輸出流新生成一個(gè)對(duì)象輸出流ObjectOutputStream類型的對(duì)象,然后通過該對(duì)象的writeObject()方法輸出JAVA類型的對(duì)象。
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類型的輸出,通過此對(duì)象的println()方法可以從Servlet想Applet輸出文本:
PrintWriter out = response.getWriter();
out.println("<head><title>DataCenter</title></head>");
通過上面的分析我們通過Servlet 實(shí)現(xiàn)了對(duì)非宿主機(jī)上的數(shù)據(jù)庫的訪問,Servlet 與Applet通信提供了語言級(jí)別上的互相傳遞JAVA對(duì)象的便利,我們同樣可以利用Applet通過CGI對(duì)各種服務(wù)器端的CGI程序或其它腳本應(yīng)用程序(如ASP、JSP等)提供訪問,以文本的方式實(shí)現(xiàn)通信。
posted on 2006-06-19 09:03 ASONG 閱讀(1394) 評(píng)論(0) 編輯 收藏 所屬分類: JAVA