applet与servlet通信的时候遇到了一个问题

06-01-23 fxwolf
大家好:

我在做applet与servlet通信的时候遇到了一个问题,请各位朋友帮忙解决一下。

问题描述如下:

在applet端部分代码

urlServlet = new URL(url);

conn = urlServlet.openConnection();

conn.setDoInput(false);

conn.setDoOutput(true);

conn.setUseCaches(false);

conn.setDefaultUseCaches(false);

conn.setRequestProperty("Content-Type", "application/octet-stream");

ObjectOutputStream outputToHost = new ObjectOutputStream(

conn.getOutputStream());

// serialize the object

outputToHost.writeObject(defaultClientEnv);

outputToHost.flush();

outputToHost.close();

在servlet端部分代码

ServletInputStream inStream = request.getInputStream();

inputFromClient = new ObjectInputStream(inStream); //这行代码出错。

Object obj = inputFromClient.readObject();

出错提示:

java.io.EOFException

at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2232)

at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2698)

at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:750)

at java.io.ObjectInputStream.<init>(ObjectInputStream.java:268)

at finance.servlets.TransmitServlet.doPost(Unknown Source)

at finance.servlets.TransmitServlet.doGet(Unknown Source)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)

at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)

at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)

at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)

at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)

at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)

at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)

at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:868)

at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:663)

at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)

at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)

at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)

at java.lang.Thread.run(Thread.java:595)

希望大家能帮我解决。。。

先谢谢了。

fxwolf
2006-01-24 08:47
各位达人。希望大家能帮帮我啊。

fxwolf
2006-01-24 20:24
????

不解啊。。。

望高人指点

mellon
2006-01-27 00:03
Applet端:

URL destURL=new URL("ServletURL");

URLConnection conn=destURL.openConnection() ;

conn.setDoOutput(true);

conn.setDoInput(true);

conn.setUseCaches(false);

ObjectOutputStream out=new ObjectOutputStream(conn.getOutputStream());

out.writeObject(request);//request是发送到服务器端的对象

out.flush() ;

out.close() ;

//取返回信息.

ObjectInputStream in=new ObjectInputStream(new BufferedInputStream(conn.getInputStream()));

Response response=(Response)in.readObject() ;//Response是服务器端反馈的对象

in.close();

return response;

服务端程序:

public void service(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{

Response dcRep=process(request);

//输出返回对象

response.setContentType("applcation/octet-stream");

OutputStream output=response.getOutputStream() ;

ObjectOutputStream objOut=new ObjectOutputStream(output);

objOut.writeObject(dcRep);

}

/**根据类型调用对应的方法。*/

private Response process(HttpServletRequest request){

try{

ObjectInputStream in = new ObjectInputStream(request.getInputStream());

//取参数容器对象

Request req = (Request) in.readObject();

in.close();

if(req!=null){

return HelperFactory.factory(req).process(req);

}else{

return new MessageResponse("未知的Request.");

}

//HttpSession session=request.getSession() ;

//UserInSession user=(UserInSession)session.getAttribute(SysConstant.USER_SESSION_ATTRIBUTEID);

}catch(Exception e){

e.printStackTrace();

}

return new MessageResponse("处理Request失败.");

}

这段代码一直再用,从JDK1.3到1.5, 但愿你能p。

你的问题我好像当时也遇到了,最后收到成效的关键点有点奇怪,不符合我的理解和逻辑。三年前的事了,现在只有一个模糊的印象,那个关键点实在不记得了,因为当时我也没明白到底为什么。

fxwolf
2006-02-06 16:13
你好,看到你年前在jdon上给我解答的applet与servlet通信的问题,非常感谢你。

但我用你说的方法做了一次并没有解决问题,现将我理解后的代码贴出来,还请你能多多指教,谢谢!!!

Applet 端:

URL urlServlet;

HttpURLConnection conn;

try {

String url = getDocumentBase().toString()

+ "servlets/TransmitServlet";

urlServlet = new URL(url);

conn = (HttpURLConnection) urlServlet.openConnection();

conn.setRequestMethod("POST");

conn.setDoInput(true);

conn.setDoOutput(true);

conn.setUseCaches(false);

conn.setRequestProperty("Content-Type", "application/octet-stream");

OutputStream os = conn.getOutputStream();

ObjectOutputStream outputToHost = new ObjectOutputStream(os);

// serialize the object

outputToHost.writeObject(defaultClientEnv);

outputToHost.flush();

if (outputToHost != null) {

outputToHost.close();

}

getAppletContext().showDocument(urlServlet, "_top");

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

Servlet 端:

ClientEnv clientEnv = null;

ObjectInputStream inputFromClient = null;

try {

InputStream inStream = request.getInputStream();

inputFromClient = new ObjectInputStream(inStream);

Object obj = inputFromClient.readObject();

inputFromClient.close();

if (obj instanceof ClientEnv) {

clientEnv = (ClientEnv) obj; //还原Applet 端发送的对象

} else {

throw new IOException("Client input object is not ClientEnv");

}

} catch (IOException e) {

e.printStackTrace();

return;

} catch (ClassNotFoundException e) {

e.printStackTrace();

return;

}

Error 信息:

java.io.EOFException

at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2232)

at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2698)

at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:750)

at java.io.ObjectInputStream.<init>(ObjectInputStream.java:268)

at finance.servlets.TransmitServlet.doPost(Unknown Source)

at finance.servlets.TransmitServlet.doGet(Unknown Source)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)

at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)

at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)

at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)

at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)

at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)

at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)

at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:868)

at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:663)

at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)

at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)

at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)

at java.lang.Thread.run(Thread.java:595)

恳请你能帮我看看问题出在那里。

猜你喜欢
2Go 1 2 下一页