是我在Netbeans中创建的Java Servlet添加了一些奇怪的东西吗? [英] is the Java Servlet I've created in Netbeans adding something strange?

查看:204
本文介绍了是我在Netbeans中创建的Java Servlet添加了一些奇怪的东西吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第二次尝试解决这个问题。我的第一次尝试是这里,但也许我的解释我的问题不够,我的问题是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在我的浏览器中。然后我运行小程序(从Netbeans中的另一个项目)并尝试连接。我仍然收到好的无效的流标题,所以我猜这个错误在于我在Netbeans中所做的一切。



我粘贴了一些代码正在工作(旧代码,但还没有找到更多最近的完整例子)该代码被公然地从链接



所以最后,我想做的是将二维对象数组从servlet发送到applet,当applet请求发送数组。代码示例只是为了显示我收到的无效流标题。



我认为/猜测小程序正在从服务器接收基于文本的响应,但是我想要响应要成为一个序列化对象(在代码示例中只是一个String),如果我有一个线索,它将是一个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扩展Applet {
private TextField inputField = new TextField();
private TextField outputField = new TextField();
private TextArea exceptionArea = new TextArea();

public void init(){
//设置新布局
setLayout(new GridBagLayout());

//添加标题
标签标题=新标签(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);

//添加输入标签,字段和发送按钮
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);
按钮sendButton = new Button(Send);
c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;
add(sendButton,c);
sendButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
onSendData();
}
});

//添加输出标签和不可编辑的字段
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);

//添加异常标签和不可编辑的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);
}

/ **
*获取到servlet的连接。
* /
private URLConnection getServletConnection()
throws MalformedURLException,IOException {

// Connection zum Servlet ffnen
URL urlServlet = new URL ://本地主机: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;
}

/ **
*将inputField数据发送到servlet,并在outputField中显示结果。
* /
private void onSendData(){
try {
//获取用于发送
的输入数据String input = inputField.getText();

//将数据发送到servlet
URLConnection con = getServletConnection();
OutputStream outstream = con.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(outstream);
oos.writeObject(input);
oos.flush();
oos.close();

//从servlet接收结果
InputStream instr = con.getInputStream();
ObjectInputStream inputFromServlet = new ObjectInputStream(instr);
String result =(String)inputFromServlet.readObject();
inputFromServlet.close();
instr.close();

//显示结果
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);

//从applet
//读取一个String对象//而不是一个String对象,您可以传输任何对象,
//对于servlet和applet
InputStream in = request.getInputStream();
ObjectInputStream inputFromApplet = new ObjectInputStream(in);
String echo =(String)inputFromApplet.readObject();

//将其回显给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:无效的流标题:0A0A0A3C 
在java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:783)
在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)
在java.awt.Button.processActionEvent( Button.java:392)
在java.awt.Button.processEvent(Button.java:360)
在java.awt.Component.dispatchEventImpl(Component.java:4714)
在java .awt.Component.dispatchEvent(Component.java:4544)
在java.awt.EventQueue.dispatchEvent(EventQueue.java:635)
在java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296 )
在j ava.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
在java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
在java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java: 196)
在java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
在java.awt.EventDispatchThread.run(EventDispatchThread.java:122)


解决方案

问题是applet无法连接到servlet,所以这里的servlet中的代码可以被忽略。



我需要使用以下方式配置server.xml:

 < Context path =/ servletNamedocBase =servletNamedebug =0oadable =true
crossContext =true>
< / Context>


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)

解决方案

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:

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

这篇关于是我在Netbeans中创建的Java Servlet添加了一些奇怪的东西吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆