2010-10-25 46 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>