13,100
社区成员
发帖
与我相关
我的任务
分享
ObjectOutputStream outputToServlet = null;
ObjectInputStream inputFromServlet = null;
try {
URL servlet = new URL(urladdress);
URLConnection servletConnection = servlet.openConnection();
//inform the connection that will send output and accept input
servletConnection.setDoInput(true);
servletConnection.setDoOutput(true);
//Don't use a cached version of URL connection.
servletConnection.setUseCaches (false);
servletConnection.setDefaultUseCaches (false);
//Specify the content type that will send binary data
servletConnection.setRequestProperty ("Content-Type", "application/octet-stream");
//send the object to the servlet using serialization
outputToServlet = new ObjectOutputStream(servletConnection.getOutputStream());
outputToServlet.writeObject(Some_Query_Data);
outputToServlet.flush();
//accept the object from the servlet using reserialization
inputFromServlet = new ObjectInputStream(servletConnection.getInputStream());
Some_Result = inputFromServlet.readObject();
//处理结果
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
outputToServlet.close();
inputFromServlet.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
{
ObjectInputStream inputFromApplet = null;
inputFromApplet = new ObjectInputStream(request.getInputStream());
Some_Query_Data = (String)inputFromApplet.readObject();
//处理输入,查询
ObjectOutputStream outputToApplet = null;
try {
outputToApplet = new ObjectOutputStream(response.getOutputStream());
outputToApplet.writeObject(Some_Result);
outputToApplet.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
outputToApplet.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}