我在 Netbeans 中创建的 Java Servlet 是否添加了一些奇怪的东西?
这是我第二次尝试解决这个问题。我的第一次尝试是这里,但也许是我对问题的解释是不够的,我的问题是小程序收到了异常:
java.io.StreamCorruptedException: invalid stream header: 0A0A0A3C at
java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:783) at
java.io.ObjectInputStream.<init>(ObjectInputStream.java:280)
对不起,如果我听起来像破纪录:)
我试图在同一台机器上的小程序和Servlet之间进行通信,我已经在Netbeans中通过创建创建了Servlet一个新项目 - Java Web - Web 应用程序并选择 Glassfish Server 3 作为服务器。它确实创建了一个index.jsp,但我实际上并不需要网页界面。
我从 NetBeans 运行 servlet(按 f6),它会在我的浏览器中部署并打开 servlet 的 index.jsp。然后,我运行该小程序(来自 Netbeans 中的另一个项目)并尝试连接。我仍然收到“无效的流标头”,所以我猜测问题出在我在 Netbeans 中所做的事情中。
我粘贴了一些我认为有效的代码(旧代码,但尚未找到任何更新的完整示例)该代码是从 Link
所以最后,我想做的是当小程序请求发送数组时,将二维对象数组从 servlet 发送到小程序。代码示例只是为了显示我收到的无效流标头。
我认为/猜测小程序正在从服务器接收基于文本的响应,但我希望响应是一个序列化对象(在代码示例中只是一个字符串),稍后它将是一个对象[][],如果我得到一条线索。
感谢各位大师的耐心等待。 :)
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();
}
}
}
stackTrace:
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)
This is my second try to solve this problem. My first try was here but perhaps my explanation of my problem was insufficient, my problem was that the applet received the exception:
java.io.StreamCorruptedException: invalid stream header: 0A0A0A3C at
java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:783) at
java.io.ObjectInputStream.<init>(ObjectInputStream.java:280)
sorry if I sound like a broken record :)
I'm trying to communicate between an Applet and a Servlet on the same machine, I've created the servlet in Netbeans by creating a New project - Java Web - Web Application and Choosing Glassfish Server 3 as server. It does create an index.jsp but I don't really need a web page interface.
I run the servlet from NetBeans (pressing f6) and it deploys and opens the servlet's index.jsp in my browser. I then run the applet (from a different project in Netbeans) and try to connect. I still receive the good ol' "invalid stream header" so I'm guessing the fault lies within something I've done in Netbeans.
I pasted some code I assume is working (old code but haven't found any more recent full examples) The code is blatantly stolen from Link
So in the end, what i'd like do is to send a two dimensional Object array from the servlet to the applet when the applet requests the array to be sent. The code examples is just to show the Invalid stream header I'm receiving.
I think/guess the applet is receving a textbased response from the server but I want the response to be a serialized-object (Just a String in the code example), it will be an Object[ ][ ] later, if I ever get a clue.
Thanks for your patience, gurus. :)
Applet code (feel free to ignore init() with all the layout code):
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 code:
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();
}
}
}
stackTrace:
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是小程序无法连接到 servlet,因此可以忽略此处 servlet 中的代码。
我需要这样配置 server.xml:
The problem was that the applet couldn't connect to the servlet so the code in the servlet here can be ignored.
I needed to config server.xml with this: