这是我第二次尝试解决这个问题。我的第一次尝试是here,但也许我对我的问题的解释是不够的,我的问题是applet收到了例外:
java.io.StreamCorruptedException: invalid stream header: 0A0A0A3C at
java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:783) at
java.io.ObjectInputStream.<init>(ObjectInputStream.java:280)
抱歉,如果我听起来像是破纪录的话:):
我正在尝试在同一台机器上的Applet和Servlet之间进行通信,我通过创建一个新项目 - Java Web - Web应用程序并选择Glassfish Server 3作为服务器,在Netbeans中创建了servlet。它确实创建了一个index.jsp,但我并不需要一个网页界面。
我从NetBeans运行servlet(按f6),它在我的浏览器中部署并打开servlet的index.jsp。然后我运行applet(来自Netbeans中的另一个项目)并尝试连接。我仍然收到好的“无效的流标题”,所以我猜测错误在于我在Netbeans中所做的事情。
我粘贴了一些我认为正在运行的代码(旧代码但未找到更新的完整示例)代码被公然从Link
中窃取所以最后,我想做的是当applet请求发送数组时,从servlet向applet发送一个二维Object数组。代码示例只是为了显示我收到的无效流标题。
我认为/猜测applet正在接收来自服务器的基于文本的响应,但我希望响应是序列化对象(在代码示例中只是一个字符串),稍后它将是一个Object [] [],如果我得到了一个线索。
感谢您的耐心,大师。 :)
Applet代码(随意忽略带有所有布局代码的init()):
package se.iot.recallapplet;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class RecallApplet extends Applet {
private TextField inputField = new TextField();
private TextField outputField = new TextField();
private TextArea exceptionArea = new TextArea();
public void init() {
// set new layout
setLayout(new GridBagLayout());
// add title
Label title = new Label("Echo Applet", Label.CENTER);
title.setFont(new Font("SansSerif", Font.BOLD, 14));
GridBagConstraints c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;
c.weightx = 1.0;
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(5, 5, 5, 5);
add(title, c);
// add input label, field and send button
c = new GridBagConstraints();
c.anchor = GridBagConstraints.EAST;
add(new Label("Input:", Label.RIGHT), c);
c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1.0;
add(inputField, c);
Button sendButton = new Button("Send");
c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;
add(sendButton, c);
sendButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
onSendData();
}
});
// add output label and non-editable field
c = new GridBagConstraints();
c.anchor = GridBagConstraints.EAST;
add(new Label("Output:", Label.RIGHT), c);
c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1.0;
add(outputField, c);
outputField.setEditable(false);
// add exception label and non-editable textarea
c = new GridBagConstraints();
c.anchor = GridBagConstraints.EAST;
add(new Label("Exception:", Label.RIGHT), c);
c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;
c.weighty = 1;
c.fill = GridBagConstraints.BOTH;
add(exceptionArea, c);
exceptionArea.setEditable(false);
}
/**
* Get a connection to the servlet.
*/
private URLConnection getServletConnection()
throws MalformedURLException, IOException {
// Connection zum Servlet ˆffnen
URL urlServlet = new URL("http://localhost:8080/Event_Servlet/");
URLConnection con = urlServlet.openConnection();
// konfigurieren
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setRequestProperty(
"Content-Type",
"application/x-java-serialized-object");
return con;
}
/**
* Send the inputField data to the servlet and show the result in the outputField.
*/
private void onSendData() {
try {
// get input data for sending
String input = inputField.getText();
// send data to the servlet
URLConnection con = getServletConnection();
OutputStream outstream = con.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(outstream);
oos.writeObject(input);
oos.flush();
oos.close();
// receive result from servlet
InputStream instr = con.getInputStream();
ObjectInputStream inputFromServlet = new ObjectInputStream(instr);
String result = (String) inputFromServlet.readObject();
inputFromServlet.close();
instr.close();
// show result
outputField.setText(result);
} catch (Exception ex) {
ex.printStackTrace();
exceptionArea.setText(ex.toString());
}
}
}
Servlet代码:
package se.iot.eventservlet;
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
public class Event_Servlet extends HttpServlet {
public void doPost(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
try {
response.setContentType("application/x-java-serialized-object");
// read a String-object from applet
// instead of a String-object, you can transmit any object, which
// is known to the servlet and to the applet
InputStream in = request.getInputStream();
ObjectInputStream inputFromApplet = new ObjectInputStream(in);
String echo = (String) inputFromApplet.readObject();
// echo it to the applet
OutputStream outstr = response.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(outstr);
oos.writeObject(echo);
oos.flush();
oos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
堆栈跟踪:
java.io.StreamCorruptedException: invalid stream header: 0A0A0A3C
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:783)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:280)
at se.iot.recallapplet.RecallApplet.onSendData(RecallApplet.java:114)
at se.iot.recallapplet.RecallApplet.access$000(RecallApplet.java:12)
at se.iot.recallapplet.RecallApplet$1.actionPerformed(RecallApplet.java:48)
at java.awt.Button.processActionEvent(Button.java:392)
at java.awt.Button.processEvent(Button.java:360)
at java.awt.Component.dispatchEventImpl(Component.java:4714)
at java.awt.Component.dispatchEvent(Component.java:4544)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:635)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
答案 0 :(得分:1)
问题是applet无法连接到servlet,因此可以忽略servlet中的代码。
我需要使用以下命令配置server.xml:
<Context path="/servletName" docBase="servletName" debug="0" reloadable="true"
crossContext="true">
</Context>