2010-10-25 94 views
1

这是我第二次尝试解决这个问题。我的第一次尝试是here但也许我的我的问题的解释是不够的,我的问题是,小程序接收到异常:是我在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) 

对不起,如果我听起来像一个破纪录:)

我想为了在同一台机器上的Applet和Servlet之间进行通信,我创建了一个新项目 - Java Web - Web应用程序并选择Glassfish Server 3作为服务器,从而在Netbeans中创建了Servlet。它确实创建了一个index.jsp,但我并不需要一个网页界面。

我从NetBeans运行servlet(按f6),它在我的浏览器中部署并打开servlet的index.jsp。然后我运行小程序(从Netbeans中的一个不同的项目中)并尝试连接。我仍然收到良好的'''流头'',所以我猜这个错误在我在Netbeans做的事情中。

我粘贴一些代码,我认为是工作(旧代码,但没有发现任何最近的全例子)的代码公然从Link

所以最后,我希望做的是偷来的当小应用程序请求发送数组时,从servlet发送一个二维对象数组到小应用程序。代码示例只是为了显示我收到的无效流标头。

我想/猜测小程序正在接收来自服务器的基于文本的响应,但我希望响应是序列化对象(代码示例中只是一个字符串),稍后它将成为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

如果你的行号匹配与你的小程序异常。 outputField.setText(结果);是你抛出异常的线。我会建议在你的onSendData()方法中设置一个断点。一次一行地浏览代码,以更多地了解问题的根源。 – Sean 2010-10-25 13:33:03

+0

你确定你正在打你的servlet吗?你可以在你的servlet.doPost方法中放置一个断点吗? – kaliatech 2010-10-25 14:45:39

+0

感谢您的回复,直到星期四我再也看不到代码,然后我会更新。 – 2010-10-25 18:31:30

回答

1

的问题在于applet无法连接到servlet,因此servlet中的代码可以被忽略。

我需要配置的server.xml这个:

<Context path="/servletName" docBase="servletName" debug="0" reloadable="true" 
    crossContext="true"> 
    </Context>